diff --git a/app/src/main/java/com/edricchan/studybuddy/ui/modules/settings/fragment/TodoSettingsFragment.kt b/app/src/main/java/com/edricchan/studybuddy/ui/modules/settings/fragment/TodoSettingsFragment.kt index 210150fb..5d4079d3 100644 --- a/app/src/main/java/com/edricchan/studybuddy/ui/modules/settings/fragment/TodoSettingsFragment.kt +++ b/app/src/main/java/com/edricchan/studybuddy/ui/modules/settings/fragment/TodoSettingsFragment.kt @@ -1,11 +1,39 @@ package com.edricchan.studybuddy.ui.modules.settings.fragment import android.os.Bundle -import com.edricchan.studybuddy.R -import com.edricchan.studybuddy.ui.preference.MaterialPreferenceFragment +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.compose.foundation.layout.WindowInsets +import androidx.compose.foundation.layout.navigationBars +import androidx.compose.foundation.layout.windowInsetsPadding +import androidx.compose.ui.Modifier +import androidx.compose.ui.input.nestedscroll.nestedScroll +import androidx.compose.ui.platform.rememberNestedScrollInteropConnection +import androidx.fragment.app.viewModels +import androidx.fragment.compose.content +import com.edricchan.studybuddy.features.settings.task.ui.TaskSettingsScreen +import com.edricchan.studybuddy.features.settings.task.vm.TaskSettingsViewModel +import com.edricchan.studybuddy.ui.common.fragment.BaseFragment +import com.edricchan.studybuddy.ui.theming.compose.StudyBuddyTheme +import dagger.hilt.android.AndroidEntryPoint -class TodoSettingsFragment : MaterialPreferenceFragment() { - override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { - setPreferencesFromResource(R.xml.pref_todos, rootKey) +@AndroidEntryPoint +class TodoSettingsFragment : BaseFragment() { + private val viewModel by viewModels() + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ) = content { + val nestedScrollInterop = rememberNestedScrollInteropConnection() + StudyBuddyTheme { + TaskSettingsScreen( + modifier = Modifier + .nestedScroll(nestedScrollInterop) + .windowInsetsPadding(WindowInsets.navigationBars), + viewModel = viewModel + ) + } } } diff --git a/app/src/main/res/xml/pref_todos.xml b/app/src/main/res/xml/pref_todos.xml deleted file mode 100644 index 2cb91240..00000000 --- a/app/src/main/res/xml/pref_todos.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - diff --git a/features/settings/src/main/kotlin/com/edricchan/studybuddy/features/settings/task/model/TaskSortOptionCompat.kt b/features/settings/src/main/kotlin/com/edricchan/studybuddy/features/settings/task/model/TaskSortOptionCompat.kt new file mode 100644 index 00000000..82b0cf7d --- /dev/null +++ b/features/settings/src/main/kotlin/com/edricchan/studybuddy/features/settings/task/model/TaskSortOptionCompat.kt @@ -0,0 +1,35 @@ +package com.edricchan.studybuddy.features.settings.task.model + +import androidx.annotation.StringRes +import com.edricchan.studybuddy.features.settings.R +import com.edricchan.studybuddy.features.settings.task.model.TaskSortOptionCompat.entries + +enum class TaskSortOptionCompat( + @StringRes val stringResource: Int, + val value: String +) { + None( + stringResource = R.string.pref_task_default_sort_entry_none, + value = "none" + ), + TitleAsc( + stringResource = R.string.pref_task_default_sort_entry_title_asc, + value = "title_asc" + ), + TitleDesc( + stringResource = R.string.pref_task_default_sort_entry_title_desc, + value = "title_desc" + ), + DueDateNewToOld( + stringResource = R.string.pref_task_default_sort_entry_due_date_new_to_old, + value = "due_date_new_to_old" + ), + DueDateOldToNew( + stringResource = R.string.pref_task_default_sort_entry_due_date_old_to_new, + value = "due_date_old_to_new" + ); + + companion object { + fun fromValue(value: String): TaskSortOptionCompat = entries.first { it.value == value } + } +} diff --git a/features/settings/src/main/kotlin/com/edricchan/studybuddy/features/settings/task/ui/TaskSettings.kt b/features/settings/src/main/kotlin/com/edricchan/studybuddy/features/settings/task/ui/TaskSettings.kt new file mode 100644 index 00000000..0a14fc20 --- /dev/null +++ b/features/settings/src/main/kotlin/com/edricchan/studybuddy/features/settings/task/ui/TaskSettings.kt @@ -0,0 +1,83 @@ +package com.edricchan.studybuddy.features.settings.task.ui + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.Icon +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +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.res.painterResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.tooling.preview.Preview +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import com.edricchan.studybuddy.features.settings.R +import com.edricchan.studybuddy.features.settings.task.model.TaskSortOptionCompat +import com.edricchan.studybuddy.features.settings.task.vm.TaskSettingsViewModel +import com.edricchan.studybuddy.ui.preference.compose.ListDialogPreference +import com.edricchan.studybuddy.ui.theming.compose.StudyBuddyTheme + +@Composable +private fun TaskDefaultSortPreference( + modifier: Modifier = Modifier, + defaultSort: TaskSortOptionCompat, + onDefaultSortChange: (TaskSortOptionCompat) -> Unit +) { + ListDialogPreference( + modifier = modifier, + icon = { Icon(painterResource(R.drawable.ic_sort_24dp), contentDescription = null) }, + title = { Text(text = stringResource(R.string.pref_task_default_sort_title)) }, + subtitle = { Text(text = stringResource(defaultSort.stringResource)) }, + values = TaskSortOptionCompat.entries, + value = defaultSort, + onValueChanged = onDefaultSortChange, + valueLabel = { Text(text = stringResource(it.stringResource)) } + ) +} + +@Composable +fun TaskSettingsScreen( + modifier: Modifier = Modifier, + defaultSort: TaskSortOptionCompat, + onDefaultSortChange: (TaskSortOptionCompat) -> Unit +) = Column( + modifier = modifier.verticalScroll(rememberScrollState()) +) { + TaskDefaultSortPreference( + defaultSort = defaultSort, + onDefaultSortChange = onDefaultSortChange + ) +} + +@Composable +fun TaskSettingsScreen( + modifier: Modifier = Modifier, + viewModel: TaskSettingsViewModel +) { + val defaultSort by viewModel.prefDefaultSort.asFlow().collectAsStateWithLifecycle( + TaskSortOptionCompat.None + ) + + TaskSettingsScreen( + modifier = modifier, + defaultSort = defaultSort, + onDefaultSortChange = viewModel.prefDefaultSort::set + ) +} + +@Preview +@Composable +private fun TaskSettingsScreenPreview() { + var defaultSort by remember { mutableStateOf(TaskSortOptionCompat.None) } + + StudyBuddyTheme { + TaskSettingsScreen( + defaultSort = defaultSort, + onDefaultSortChange = { defaultSort = it } + ) + } +} diff --git a/features/settings/src/main/kotlin/com/edricchan/studybuddy/features/settings/task/vm/TaskSettingsViewModel.kt b/features/settings/src/main/kotlin/com/edricchan/studybuddy/features/settings/task/vm/TaskSettingsViewModel.kt new file mode 100644 index 00000000..71271645 --- /dev/null +++ b/features/settings/src/main/kotlin/com/edricchan/studybuddy/features/settings/task/vm/TaskSettingsViewModel.kt @@ -0,0 +1,29 @@ +package com.edricchan.studybuddy.features.settings.task.vm + +import android.content.Context +import androidx.lifecycle.ViewModel +import com.edricchan.studybuddy.core.settings.tasks.keyPrefTaskDefaultSort +import com.edricchan.studybuddy.exts.androidx.preference.defaultSharedPreferences +import com.edricchan.studybuddy.features.settings.task.model.TaskSortOptionCompat +import com.fredporciuncula.flow.preferences.FlowSharedPreferences +import com.fredporciuncula.flow.preferences.Preference +import com.fredporciuncula.flow.preferences.map +import dagger.hilt.android.lifecycle.HiltViewModel +import dagger.hilt.android.qualifiers.ApplicationContext +import javax.inject.Inject + +@HiltViewModel +class TaskSettingsViewModel @Inject constructor( + @ApplicationContext context: Context +) : ViewModel() { + private val appPreferences = FlowSharedPreferences( + context.defaultSharedPreferences + ) + + val prefDefaultSort: Preference = appPreferences.getString( + keyPrefTaskDefaultSort + ).map( + mapper = TaskSortOptionCompat::fromValue, + reverse = TaskSortOptionCompat::value + ) +} diff --git a/features/settings/src/main/res/values/task_strings.xml b/features/settings/src/main/res/values/task_strings.xml new file mode 100644 index 00000000..18745444 --- /dev/null +++ b/features/settings/src/main/res/values/task_strings.xml @@ -0,0 +1,14 @@ + + + + + + + Default sorting mode + + None (Default) + Title (A to Z) + Title (Z to A) + Due Date (Newest to Oldest) + Due Date (Oldest to Newest) +