Skip to content

Commit

Permalink
version 29
Browse files Browse the repository at this point in the history
Added Sorting Options and Bug Fixes !
  • Loading branch information
xectrone committed Sep 11, 2024
1 parent 435ac96 commit 9276f5f
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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() }
Expand All @@ -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 =
Expand Down Expand Up @@ -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 =
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +19,12 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
private val _directoryUri = mutableStateOf<Uri?>(null)
val directoryUri: State<Uri?> = _directoryUri

private val _sortOption = mutableStateOf<Int?>(0)
val sortOption: State<Int?> = _sortOption

private val _isExpanded = mutableStateOf<Boolean>(false)
val isExpanded: State<Boolean> = _isExpanded

private val _markdownFilesList = MutableStateFlow<List<NoteSelectionListItem>>(emptyList())
val markdownFilesList: StateFlow<List<NoteSelectionListItem>> = _markdownFilesList

Expand All @@ -25,6 +33,7 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {

init {
observeDirectoryUri()
observeSortOption()
}

fun observeDirectoryUri() {
Expand All @@ -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()
}

}
}

Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}


}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/round_sort_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M4,18h4c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1L4,16c-0.55,0 -1,0.45 -1,1s0.45,1 1,1zM3,7c0,0.55 0.45,1 1,1h16c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1L4,6c-0.55,0 -1,0.45 -1,1zM4,13h10c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1L4,11c-0.55,0 -1,0.45 -1,1s0.45,1 1,1z"/>

</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<style name="Theme.QuickMark" parent="android:Theme.Material.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>

<style name="Theme.TransparentActivity" parent="Theme.AppCompat.DayNight.Dialog">
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<style name="Theme.QuickMark" parent="android:Theme.Material.Light.NoActionBar" >
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowLightStatusBar">true</item>

</style>

<style name="Theme.TransparentActivity" parent="Theme.AppCompat.DayNight.Dialog">
Expand Down

0 comments on commit 9276f5f

Please sign in to comment.