Skip to content

Commit

Permalink
Cleanup + dependency updates
Browse files Browse the repository at this point in the history
  • Loading branch information
farmerbb committed Sep 22, 2024
1 parent d1daffc commit 4c9fda7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.SelectAll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.LayoutDirection
import com.farmerbb.notepad.R

Expand Down Expand Up @@ -141,24 +137,16 @@ fun ExportButton(onClick: () -> Unit = {}) {
}
}


var searchTerm by mutableStateOf("")

@Composable
fun SearchTextField() {
SearchTextFieldChild(searchTerm) { newSearchTerm ->
searchTerm = newSearchTerm
}
}

@Composable
fun SearchTextFieldChild(searchTerm: String,
onSearchTermChanged: (String) -> Unit) {
fun SearchTextField(
searchTerm: String,
onSearchTermChanged: (String) -> Unit,
) {
val focusRequester = remember { FocusRequester() }

TextField(
value = searchTerm,
onValueChange = { newSearchTerm ->
onSearchTermChanged(newSearchTerm) },
onValueChange = { newSearchTerm -> onSearchTermChanged(newSearchTerm) },
leadingIcon = {
Icon(
imageVector = Icons.Filled.Search,
Expand All @@ -180,6 +168,7 @@ fun SearchTextFieldChild(searchTerm: String,
unfocusedLabelColor = Color.White
)
)

LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fun NoteListContent(
val filteredNotes = notes.filter {
foundNotes.getOrDefault(it.metadataId, false)
}

when (filteredNotes.size) {
0 -> Column(
modifier = Modifier.fillMaxSize(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ import com.farmerbb.notepad.ui.components.SaveDialog
import com.farmerbb.notepad.ui.components.SearchNotesButton
import com.farmerbb.notepad.ui.components.SearchTextField
import com.farmerbb.notepad.ui.components.SelectAllButton
import com.farmerbb.notepad.ui.components.searchTerm
import com.farmerbb.notepad.ui.content.EditNoteContent
import com.farmerbb.notepad.ui.content.NoteListContent
import com.farmerbb.notepad.ui.content.ViewNoteContent
Expand Down Expand Up @@ -199,6 +198,8 @@ private fun NotepadComposeApp(
var backButton: @Composable (() -> Unit)? = null
var actions: @Composable RowScope.() -> Unit = {}
val content: @Composable BoxScope.() -> Unit

val (searchTerm, onSearchTermChanged) = rememberSaveable { mutableStateOf("") }

/*********************** Callbacks ***********************/

Expand Down Expand Up @@ -258,7 +259,7 @@ private fun NotepadComposeApp(
searchNotesEnabled -> {
searchNotesEnabled = false
vm.setAllNotesAsFound(notes)
searchTerm = "";
onSearchTermChanged("")
}
navState is Edit && text.isNotEmpty() -> {
onSaveClick(false, updateNavState)
Expand Down Expand Up @@ -458,7 +459,7 @@ private fun NotepadComposeApp(
SearchNotesButton {
vm.showToastIf(notes.isEmpty(), R.string.no_notes_to_search) {
searchNotesEnabled = true
searchTerm = "";
onSearchTermChanged("")
}
}
MultiSelectButton {
Expand Down Expand Up @@ -486,7 +487,8 @@ private fun NotepadComposeApp(
)
}
}
if(!searchNotesEnabled){

if (!searchNotesEnabled) {
vm.setAllNotesAsFound(notes)
}

Expand Down Expand Up @@ -533,8 +535,9 @@ private fun NotepadComposeApp(
}
}

if(searchNotesEnabled)
searchNotesEnabled = false;
if (searchNotesEnabled) {
searchNotesEnabled = false
}

content = {
Printable(printController) {
Expand Down Expand Up @@ -583,8 +586,9 @@ private fun NotepadComposeApp(
}
}

if(searchNotesEnabled)
searchNotesEnabled = false;
if (searchNotesEnabled) {
searchNotesEnabled = false
}

content = {
Printable(printController) {
Expand Down Expand Up @@ -631,18 +635,15 @@ private fun NotepadComposeApp(
}
}

/*********************** Search-Notes ***********************/
/*********************** Search ***********************/

if (searchNotesEnabled) {
title = ""
backButton = { BackButton(onBack) }
actions = {
SearchTextField()
}
vm.setSomeNotesAsNotFound(notes)
actions = { SearchTextField(searchTerm, onSearchTermChanged) }
vm.setSomeNotesAsNotFound(notes, searchTerm)
}


/*********************** Scaffold ***********************/

Scaffold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import com.farmerbb.notepad.model.FilenameFormat
import com.farmerbb.notepad.model.Note
import com.farmerbb.notepad.model.NoteMetadata
import com.farmerbb.notepad.model.PrefKeys
import com.farmerbb.notepad.ui.components.searchTerm
import com.farmerbb.notepad.usecase.ArtVandelay
import com.farmerbb.notepad.usecase.DataMigrator
import com.farmerbb.notepad.usecase.KeyboardShortcuts
Expand Down Expand Up @@ -98,8 +97,6 @@ class NotepadViewModel(
)
val foundNotesFlow: SharedFlow<Map<Long, Boolean>> = _foundNotesFlow



val noteMetadata get() = prefs.sortOrder.flatMapConcat(repo::noteMetadataFlow)
val prefs = dataStoreManager.prefs(viewModelScope, systemTheme)

Expand Down Expand Up @@ -138,11 +135,6 @@ class NotepadViewModel(
_selectedNotesFlow.tryEmit(selectedNotes.filterValues { it })
}

fun toggleFoundNote(id: Long) {
foundNotes[id] = !foundNotes.getOrDefault(id, false)
_foundNotesFlow.tryEmit(foundNotes.filterValues { it })
}

fun setAllNotesAsFound(notes: List<NoteMetadata>) {
notes.forEach {
foundNotes[it.metadataId] = true
Expand All @@ -151,11 +143,14 @@ class NotepadViewModel(
_foundNotesFlow.tryEmit(foundNotes.filterValues { it })
}

fun setSomeNotesAsNotFound(notes: List<NoteMetadata>) {
//This is where the actual search is done.
setAllNotesAsFound(notes);
fun setSomeNotesAsNotFound(
notes: List<NoteMetadata>,
searchTerm: String,
) {
// This is where the actual search is done
setAllNotesAsFound(notes)
repo.getNotes(notes).forEach {
if(!it.text.contains(searchTerm, ignoreCase = true)){
if(!it.text.contains(searchTerm, ignoreCase = true)) {
foundNotes[it.id] = false
}
}
Expand Down Expand Up @@ -375,7 +370,6 @@ class NotepadViewModel(
}
}


private fun parseDateFromFileName(filePath: String): Date? {
// Extracting the filename from the full path
val fileName = filePath.substring(filePath.lastIndexOf('/') + 1)
Expand All @@ -388,7 +382,7 @@ class NotepadViewModel(
return matchResult?.value?.let { dateString ->
try {
SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.getDefault()).parse(dateString)
} catch (e: Exception) {
} catch (_: Exception) {
null // Return null if the date cannot be parsed
}
}
Expand All @@ -402,7 +396,7 @@ class NotepadViewModel(
val text = it.readUtf8()
if (text.isNotEmpty()) {
val modifiedDate = parseDateFromFileName(filePath)
//if the modifiedDate couldn't be parsed, use current date
// If the modified date couldn't be parsed, use current date
val nonNullModifiedDate: Date = modifiedDate ?: Date()
repo.saveNote(text = text, date = nonNullModifiedDate)
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ android-targetSdk = "34" # TODO need to properly handle window insets before bum
androidx-activity = "1.9.2"
androidx-core = "1.13.1"
androidx-datastore = "1.0.0" # TODO newer versions make the app flash between light/dark mode
androidx-lifecycle = "2.8.5"
androidx-lifecycle = "2.8.6"
androidx-preference = "1.2.1"

# Other dependencies
accompanist = "0.36.0"
compose = "1.7.0"
compose = "1.7.2"
composePreferences = "0.1.5-9"
coroutines = "1.8.1"
fsaf = "v1.1.3"
Expand Down

0 comments on commit 4c9fda7

Please sign in to comment.