From 9276f5f0373120179c13b8ad1b43fef8deff1593 Mon Sep 17 00:00:00 2001 From: Bhushan Malekar <84770287+xectrone@users.noreply.github.com> Date: Wed, 11 Sep 2024 23:01:54 +0530 Subject: [PATCH] version 29 Added Sorting Options and Bug Fixes ! --- .../domain/file_handling/DataStore.kt | 10 +++++ .../quickmark/ui/home_screen/HomeScreen.kt | 35 +++++++++++++-- .../quickmark/ui/home_screen/HomeViewModel.kt | 45 ++++++++++++++++++- .../quickmark/ui/home_screen/NoteListItem.kt | 4 +- .../quickmark/ui/home_screen/SortOptions.kt | 8 ++++ .../xectrone/quickmark/ui/theme/Constants.kt | 8 ++++ app/src/main/res/drawable/round_sort_24.xml | 5 +++ app/src/main/res/values-night/themes.xml | 1 + app/src/main/res/values/themes.xml | 1 + 9 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/xectrone/quickmark/ui/home_screen/SortOptions.kt create mode 100644 app/src/main/res/drawable/round_sort_24.xml diff --git a/app/src/main/java/com/xectrone/quickmark/domain/file_handling/DataStore.kt b/app/src/main/java/com/xectrone/quickmark/domain/file_handling/DataStore.kt index cc24e0f..167a2fe 100644 --- a/app/src/main/java/com/xectrone/quickmark/domain/file_handling/DataStore.kt +++ b/app/src/main/java/com/xectrone/quickmark/domain/file_handling/DataStore.kt @@ -13,4 +13,14 @@ object DataStore { val prefs = context.getSharedPreferences("QuickMarkPrefs", Context.MODE_PRIVATE) return prefs.getString("selected_directory_uri", null)?.let { Uri.parse(it) } } + + fun saveSelectedSort(context: Context, sortOption: Int) { + val prefs = context.getSharedPreferences("QuickMarkPrefs", Context.MODE_PRIVATE) + prefs.edit().putInt("selected_sort_option", sortOption).apply() + } + + fun getSavedSort(context: Context): Int? { + val prefs = context.getSharedPreferences("QuickMarkPrefs", Context.MODE_PRIVATE) + return prefs.getInt("selected_sort_option", 0) + } } \ No newline at end of file diff --git a/app/src/main/java/com/xectrone/quickmark/ui/home_screen/HomeScreen.kt b/app/src/main/java/com/xectrone/quickmark/ui/home_screen/HomeScreen.kt index 0714751..366c54b 100644 --- a/app/src/main/java/com/xectrone/quickmark/ui/home_screen/HomeScreen.kt +++ b/app/src/main/java/com/xectrone/quickmark/ui/home_screen/HomeScreen.kt @@ -1,9 +1,13 @@ package com.xectrone.quickmark.ui.home_screen import android.annotation.SuppressLint +import androidx.compose.animation.core.tween +import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items +import androidx.compose.material.DropdownMenu +import androidx.compose.material.DropdownMenuItem import androidx.compose.material.FloatingActionButton import androidx.compose.material.Icon import androidx.compose.material.IconButton @@ -15,12 +19,12 @@ import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.Add import androidx.compose.material.icons.rounded.Clear import androidx.compose.material.icons.rounded.Delete -import androidx.compose.material.icons.rounded.Menu import androidx.compose.material.icons.rounded.Settings import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier +import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.navigation.NavHostController @@ -32,6 +36,7 @@ import com.xectrone.quickmark.ui.theme.CustomTypography import com.xectrone.quickmark.ui.theme.LocalCustomColorPalette import com.xectrone.quickmark.ui.utility.MessageScreen +@OptIn(ExperimentalFoundationApi::class) @SuppressLint("UnusedMaterialScaffoldPaddingParameter") @Composable fun HomeScreen( @@ -41,9 +46,11 @@ fun HomeScreen( val markdownFilesList by viewModel.markdownFilesList.collectAsStateWithLifecycle(emptyList()) val selectionMode by viewModel.selectionMode val directoryUri by viewModel.directoryUri + val isExpanded by viewModel.isExpanded LaunchedEffect(key1 = navController.currentBackStackEntry) { viewModel.observeDirectoryUri() + viewModel.observeSortOption() } Scaffold( @@ -68,7 +75,7 @@ fun HomeScreen( // ) // { Icon(imageVector = Icons.Rounded.Menu, contentDescription = Constants.Labels.HomeScreen.MENU) } // }, - actions = { + actions = @androidx.compose.runtime.Composable { if(selectionMode) { IconButton( onClick = { viewModel.onDelete() } @@ -80,6 +87,25 @@ fun HomeScreen( ) { Icon(imageVector = Icons.Rounded.Clear, contentDescription = Constants.Labels.HomeScreen.CLEAR) } } + IconButton(onClick = {viewModel.showMenu()}) + { + Icon(painter = painterResource(id = R.drawable.round_sort_24), contentDescription = Constants.Labels.HomeScreen.CLEAR) + DropdownMenu(expanded = isExpanded, onDismissRequest = { viewModel.hideMenu() }) + { + DropdownMenuItem(onClick = { viewModel.onSort(SortOptions.nameASC) }) + { Text(text = Constants.Labels.SortOptions.nameASC) } + + DropdownMenuItem(onClick = { viewModel.onSort(SortOptions.nameDESC) }) + { Text(text = Constants.Labels.SortOptions.nameDESC) } + + DropdownMenuItem(onClick = { viewModel.onSort(SortOptions.lastModifiedASC) }) + { Text(text = Constants.Labels.SortOptions.lastModifiedASC) } + + DropdownMenuItem(onClick = { viewModel.onSort(SortOptions.lastModifiedDESC) }) + { Text(text = Constants.Labels.SortOptions.lastModifiedDESC) } + } + + } IconButton( onClick = @@ -122,9 +148,12 @@ fun HomeScreen( .padding(horizontal = Dimen.Padding.p4) ){ //region - List View - - items(markdownFilesList) + items(items = markdownFilesList, key ={it.fileName}) { NoteListItem( + modifier = Modifier.animateItemPlacement( + animationSpec = tween(durationMillis = 600) + ), item = NoteSelectionListItem(fileName = it.fileName, fileContent = it.fileContent, fileUri = it.fileUri, lastModified = it.lastModified, isSelected = it.isSelected), onClick = { diff --git a/app/src/main/java/com/xectrone/quickmark/ui/home_screen/HomeViewModel.kt b/app/src/main/java/com/xectrone/quickmark/ui/home_screen/HomeViewModel.kt index a545c48..601e486 100644 --- a/app/src/main/java/com/xectrone/quickmark/ui/home_screen/HomeViewModel.kt +++ b/app/src/main/java/com/xectrone/quickmark/ui/home_screen/HomeViewModel.kt @@ -7,6 +7,8 @@ import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.viewModelScope import com.xectrone.quickmark.domain.file_handling.DataStore.getSavedDirectoryUri +import com.xectrone.quickmark.domain.file_handling.DataStore.getSavedSort +import com.xectrone.quickmark.domain.file_handling.DataStore.saveSelectedSort import com.xectrone.quickmark.domain.file_handling.SAFFileHelper import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -17,6 +19,12 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) { private val _directoryUri = mutableStateOf(null) val directoryUri: State = _directoryUri + private val _sortOption = mutableStateOf(0) + val sortOption: State = _sortOption + + private val _isExpanded = mutableStateOf(false) + val isExpanded: State = _isExpanded + private val _markdownFilesList = MutableStateFlow>(emptyList()) val markdownFilesList: StateFlow> = _markdownFilesList @@ -25,6 +33,7 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) { init { observeDirectoryUri() + observeSortOption() } fun observeDirectoryUri() { @@ -38,9 +47,27 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) { } } + fun observeSortOption() { + viewModelScope.launch { + getSavedSort(getApplication())?.let{ + if(sortOption.value != it) { + _sortOption.value = it + refreshMarkdownFiles() + } + } + } + } + suspend fun refreshMarkdownFiles() { directoryUri.value?.let{uri -> - _markdownFilesList.value = SAFFileHelper.getMarkdownFilesFromDirectory(directoryUri = uri, context = getApplication()).sortedBy { it.lastModified }.reversed() + _markdownFilesList.value = when(sortOption.value){ + SortOptions.nameASC -> SAFFileHelper.getMarkdownFilesFromDirectory(directoryUri = uri, context = getApplication()).sortedBy { it.fileName } + SortOptions.nameDESC -> SAFFileHelper.getMarkdownFilesFromDirectory(directoryUri = uri, context = getApplication()).sortedBy { it.fileName }.reversed() + SortOptions.lastModifiedASC -> SAFFileHelper.getMarkdownFilesFromDirectory(directoryUri = uri, context = getApplication()).sortedBy { it.lastModified } + SortOptions.lastModifiedDESC -> SAFFileHelper.getMarkdownFilesFromDirectory(directoryUri = uri, context = getApplication()).sortedBy { it.lastModified }.reversed() + else -> SAFFileHelper.getMarkdownFilesFromDirectory(directoryUri = uri, context = getApplication()).sortedBy { it.lastModified }.reversed() + } + } } @@ -71,4 +98,20 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) { _selectionMode.value = false } } + + fun onSort(sortOption:Int){ + viewModelScope.launch { + saveSelectedSort(context = getApplication(), sortOption = sortOption) + } + observeSortOption() + hideMenu() + } + + fun showMenu(){ + _isExpanded.value = true + } + + fun hideMenu(){ + _isExpanded.value = false + } } \ No newline at end of file diff --git a/app/src/main/java/com/xectrone/quickmark/ui/home_screen/NoteListItem.kt b/app/src/main/java/com/xectrone/quickmark/ui/home_screen/NoteListItem.kt index b25027a..c8f761a 100644 --- a/app/src/main/java/com/xectrone/quickmark/ui/home_screen/NoteListItem.kt +++ b/app/src/main/java/com/xectrone/quickmark/ui/home_screen/NoteListItem.kt @@ -19,10 +19,10 @@ import com.xectrone.quickmark.ui.theme.LocalCustomColorPalette @OptIn(ExperimentalFoundationApi::class) @Composable -fun NoteListItem(item: NoteSelectionListItem, onClick:()->Unit, onLongClick:()->Unit) +fun NoteListItem(modifier: Modifier = Modifier, item: NoteSelectionListItem, onClick:()->Unit, onLongClick:()->Unit) { Card( - modifier = Modifier + modifier = modifier .combinedClickable( onClick = { onClick() }, onLongClick = { onLongClick() } diff --git a/app/src/main/java/com/xectrone/quickmark/ui/home_screen/SortOptions.kt b/app/src/main/java/com/xectrone/quickmark/ui/home_screen/SortOptions.kt new file mode 100644 index 0000000..436c0ac --- /dev/null +++ b/app/src/main/java/com/xectrone/quickmark/ui/home_screen/SortOptions.kt @@ -0,0 +1,8 @@ +package com.xectrone.quickmark.ui.home_screen + +object SortOptions { + const val nameASC = 0 + const val nameDESC = 1 + const val lastModifiedASC = 2 + const val lastModifiedDESC = 3 +} \ No newline at end of file diff --git a/app/src/main/java/com/xectrone/quickmark/ui/theme/Constants.kt b/app/src/main/java/com/xectrone/quickmark/ui/theme/Constants.kt index c5d326b..352340f 100644 --- a/app/src/main/java/com/xectrone/quickmark/ui/theme/Constants.kt +++ b/app/src/main/java/com/xectrone/quickmark/ui/theme/Constants.kt @@ -32,5 +32,13 @@ object Constants { const val SAVE = "Save" } + object SortOptions { + const val nameASC = "By Name ASC" + const val nameDESC = "By Name DESC" + const val lastModifiedASC = "By Date ASC" + const val lastModifiedDESC = "By Date DESC" + } } + + } \ No newline at end of file diff --git a/app/src/main/res/drawable/round_sort_24.xml b/app/src/main/res/drawable/round_sort_24.xml new file mode 100644 index 0000000..ec1a3d1 --- /dev/null +++ b/app/src/main/res/drawable/round_sort_24.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml index 14449ef..8a91b6f 100644 --- a/app/src/main/res/values-night/themes.xml +++ b/app/src/main/res/values-night/themes.xml @@ -3,6 +3,7 @@