Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Introduced theme toggle settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
thatsmanmeet committed Jul 8, 2023
1 parent f4456c3 commit 72eb13b
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 34 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "com.thatsmanmeet.taskyapp"
minSdk 26
targetSdk 34
versionCode 19
versionName "2.3.5"
versionCode 20
versionName "2.3.6"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
Expand Down Expand Up @@ -77,7 +77,7 @@ dependencies {
annotationProcessor "androidx.room:room-compiler:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation 'androidx.core:core-splashscreen:1.0.1'
implementation "androidx.navigation:navigation-compose:2.7.0-beta01"
implementation "androidx.navigation:navigation-compose:2.7.0-beta02"
implementation "androidx.datastore:datastore-preferences:1.0.0"
def lottieVersion = "6.0.0"
implementation "com.airbnb.android:lottie-compose:$lottieVersion"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.App.Starting"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden|uiMode">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.thatsmanmeet.taskyapp.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.DateRange
Expand All @@ -21,8 +20,7 @@ fun DateHeader(
Row(
modifier = modifier
.padding(bottom = 10.dp, top = 0.dp)
.fillMaxWidth()
.background(MaterialTheme.colorScheme.surface),
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fun TaskCompleteAnimations(
progressAnimation
},
modifier = modifier.fillMaxSize(),
contentScale = ContentScale.Fit
contentScale = ContentScale.Crop
)

}
125 changes: 125 additions & 0 deletions app/src/main/java/com/thatsmanmeet/taskyapp/components/ThemeDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.thatsmanmeet.taskyapp.components


import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.thatsmanmeet.taskyapp.R
import com.thatsmanmeet.taskyapp.misc.AppTheme


@Composable
fun ThemeChangerDialog(
modifier: Modifier = Modifier,
selectedItem:MutableState<String>,
isShowing:MutableState<Boolean>,
onClick : (String) -> Unit
) {
if(isShowing.value){
AlertDialog(
onDismissRequest = {
isShowing.value = false
},
title = {
Text(text = stringResource(R.string.select_app_theme_text))
},
text = {
Column(
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.Center
) {
val list = mutableListOf(
AppTheme("0","System Default",false, R.drawable.ic_phone),
AppTheme("1","Light",false,R.drawable.ic_light),
AppTheme("2","Dark",false,R.drawable.ic_dark)
)
LazyColumn(modifier = modifier.padding(2.dp)){
items(list){theme->
ThemeItem(appTheme = theme, selectedThemeId = selectedItem) { selectedThemeId ->
selectedItem.value = selectedThemeId
}
}
}
}
},
confirmButton = {
Button(
onClick = {
onClick(selectedItem.value)
isShowing.value = false
},
colors = ButtonColors(containerColor = Color(0xFF229E28), contentColor = Color.White, disabledContentColor = Color.White, disabledContainerColor = Color.Gray)
) {
Text(text = "OK")
}
},
dismissButton = {
Button(
onClick = {
isShowing.value = false
},
colors = ButtonColors(containerColor = Color(0xFFDB4C41), contentColor = Color.White, disabledContentColor = Color.White, disabledContainerColor = Color.Gray)
) {
Text(text = "Cancel")
}
}
)
}
}

@Composable
fun ThemeItem(
modifier: Modifier = Modifier,
appTheme: AppTheme,
selectedThemeId: MutableState<String>,
onThemeItemSelected: (String) -> Unit
) {
val isSelected = remember(appTheme.id == selectedThemeId.value) {
mutableStateOf(appTheme.id == selectedThemeId.value)
}

Row(
modifier = modifier.padding(10.dp).fillMaxWidth().clickable {
onThemeItemSelected(appTheme.id)
},
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(painter = painterResource(id = appTheme.icon), contentDescription = null)
Spacer(modifier = modifier.width(10.dp))
Text(text = appTheme.mode)
}
if (isSelected.value) {
Icon(
imageVector = Icons.Default.Check,
contentDescription = null,
tint = Color(0xFF009688)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand All @@ -18,6 +19,7 @@ class SettingsStore(
val ANIMATION_SHOW_KEY = booleanPreferencesKey("animation_list_preference")
val SHOW_24_HOUR_CLOCK_KEY = booleanPreferencesKey("show_24_hour_clock_preference")
val TASK_COMPLETION_SOUNDS = booleanPreferencesKey("sound_list_preference")
val THEME_MODE_KEY = stringPreferencesKey("theme_mode_preference")
}

val getTaskListKey : Flow<Boolean?> = context.dataStore.data.map {preference->
Expand All @@ -35,6 +37,10 @@ class SettingsStore(
preference[TASK_COMPLETION_SOUNDS] ?: true
}

val getThemeModeKey : Flow<String?> = context.dataStore.data.map {preference ->
preference[THEME_MODE_KEY] ?: ""
}

suspend fun saveTaskListKey(isEnabled:Boolean) {
context.dataStore.edit {preferences->
preferences[TASK_LIST_KEY] = isEnabled
Expand All @@ -57,4 +63,10 @@ class SettingsStore(
preference[SHOW_24_HOUR_CLOCK_KEY] = isEnabled
}
}

suspend fun saveThemeModeKey(mode:String){
context.dataStore.edit { preference->
preference[THEME_MODE_KEY] = mode
}
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/thatsmanmeet/taskyapp/misc/AppTheme.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.thatsmanmeet.taskyapp.misc

data class AppTheme(
val id:String,
val mode:String,
var isSelected:Boolean,
val icon:Int
)
58 changes: 34 additions & 24 deletions app/src/main/java/com/thatsmanmeet/taskyapp/screens/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Context
import android.content.Intent
import android.media.AudioAttributes
import android.net.Uri
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.*
import androidx.compose.material.icons.Icons
Expand Down Expand Up @@ -92,7 +93,15 @@ fun MyApp(
val settingsStore = SettingsStore(context)
val savedTaskKey = settingsStore.getTaskListKey.collectAsState(initial = true)
val savedAnimationKey = settingsStore.getAnimationKey.collectAsState(initial = true)
TaskyTheme {
val savedThemeKey = settingsStore.getThemeModeKey.collectAsState(initial = "")
TaskyTheme(darkTheme = when (savedThemeKey.value) {
"0" -> {
isSystemInDarkTheme()
}
"1" -> {false}
else -> {true}
}
) {
Scaffold(
topBar = {
TopAppBar(
Expand Down Expand Up @@ -152,7 +161,9 @@ fun MyApp(
color = MaterialTheme.colorScheme.background
) {
if(todoListFromFlow.isEmpty()){
Box(modifier = modifier.fillMaxSize(),
Box(modifier = modifier
.fillMaxSize()
.padding(paddingValues),
contentAlignment = Alignment.Center) {
Column(
modifier = modifier,
Expand All @@ -176,28 +187,27 @@ fun MyApp(
}
}
}else {
if(savedTaskKey.value == null || savedTaskKey.value == true){
TaskList(
state = listState,
list = todoListFromFlow,
todoViewModel = todoViewModel,
onClick = {index->
selectedItem.value = index
openEditDialog.value = true
}
)
}else{
LegacyTaskList(
state = listState,
list = todoListFromFlow,
todoViewModel = todoViewModel,
onClick = {index->
selectedItem.value = index
openEditDialog.value = true
}
)
}

if(savedTaskKey.value == null || savedTaskKey.value == true){
TaskList(
state = listState,
list = todoListFromFlow,
todoViewModel = todoViewModel,
onClick = {index->
selectedItem.value = index
openEditDialog.value = true
}
)
}else{
LegacyTaskList(
state = listState,
list = todoListFromFlow,
todoViewModel = todoViewModel,
onClick = {index->
selectedItem.value = index
openEditDialog.value = true
}
)
}
}
if (openEditDialog.value){
OpenEditTodoDialog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.thatsmanmeet.taskyapp.screens

import android.app.Activity
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
Expand All @@ -23,6 +24,7 @@ import androidx.navigation.NavHostController
import com.thatsmanmeet.taskyapp.MainActivity
import com.thatsmanmeet.taskyapp.R
import com.thatsmanmeet.taskyapp.components.SettingsComponent
import com.thatsmanmeet.taskyapp.components.ThemeChangerDialog
import com.thatsmanmeet.taskyapp.datastore.SettingsStore
import com.thatsmanmeet.taskyapp.ui.theme.TaskyTheme
import com.thatsmanmeet.taskyapp.viewmodels.MainViewModel
Expand All @@ -43,6 +45,7 @@ fun SettingsScreen(
val context = LocalContext.current
val scope = rememberCoroutineScope()
val settingStore = SettingsStore(context)
val savedThemeKey = settingStore.getThemeModeKey.collectAsState(initial = "")
val isCheckedState = remember {
mutableStateOf(isChecked.value)
}
Expand All @@ -58,10 +61,20 @@ fun SettingsScreen(
var isDialogShowingState by rememberSaveable {
mutableStateOf(false)
}
val isThemeChangerShowing = rememberSaveable {
mutableStateOf(false)
}
val mainViewModel = MainViewModel()
val activity = LocalContext.current as Activity
val dbPath = activity.getDatabasePath("todo_database").absolutePath
TaskyTheme{
TaskyTheme(darkTheme = when (savedThemeKey.value) {
"0" -> {
isSystemInDarkTheme()
}
"1" -> {false}
else -> {true}
}
){
Scaffold(
modifier = modifier.fillMaxSize(),
topBar = {
Expand Down Expand Up @@ -110,6 +123,14 @@ fun SettingsScreen(
)
.verticalScroll(rememberScrollState()),
) {
SettingsComponent(
settingHeaderText = stringResource(R.string.set_app_theme_settings_text),
settingText = stringResource(R.string.set_app_theme_info_text),
painterResourceID = R.drawable.ic_phone
) {
isThemeChangerShowing.value = true
}
Spacer(modifier = modifier.height(12.dp))
Card(
modifier = modifier
.clip(RoundedCornerShape(15.dp))
Expand Down Expand Up @@ -298,5 +319,16 @@ fun SettingsScreen(
}
)
}
// Backup dialog ends here
ThemeChangerDialog(
selectedItem = remember {
mutableStateOf(savedThemeKey.value.toString())
},
isShowing = isThemeChangerShowing
){mode->
scope.launch {
settingStore.saveThemeModeKey(mode)
}
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_dark.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,3c-4.97,0 -9,4.03 -9,9s4.03,9 9,9s9,-4.03 9,-9c0,-0.46 -0.04,-0.92 -0.1,-1.36c-0.98,1.37 -2.58,2.26 -4.4,2.26c-2.98,0 -5.4,-2.42 -5.4,-5.4c0,-1.81 0.89,-3.42 2.26,-4.4C12.92,3.04 12.46,3 12,3L12,3z"/>
</vector>
Loading

0 comments on commit 72eb13b

Please sign in to comment.