Skip to content

Commit

Permalink
Merge pull request #38 from omer358/dev
Browse files Browse the repository at this point in the history
Resolves #5 #31 #37
  • Loading branch information
omer358 authored Aug 6, 2024
2 parents b08950c + 0324b77 commit 3bba7f5
Show file tree
Hide file tree
Showing 18 changed files with 292 additions and 71 deletions.
8 changes: 0 additions & 8 deletions app/src/main/java/com/example/rememberme/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.getValue
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.example.rememberme.presentation.settings.SettingsEvent
import com.example.rememberme.presentation.settings.SettingsViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -27,9 +22,6 @@ class MainActivity : ComponentActivity() {
// }
// }
setContent {
val viewModel: SettingsViewModel = hiltViewModel()
val themeMode by viewModel.themeMode.collectAsStateWithLifecycle()
viewModel.onEvent(SettingsEvent.GetTheme)
RememberMeApp()
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/example/rememberme/RememberMeApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ private const val TAG = "RememberMeApp"
fun RememberMeApp() {
val viewModel: SettingsViewModel = hiltViewModel()

val themeMode by viewModel.themeMode.collectAsStateWithLifecycle()
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
val themeMode = uiState.theme
val isDarkTheme = when (themeMode) {
ThemeMode.LIGHT -> false
ThemeMode.DARK -> true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.example.rememberme.domain.manager.SettingsManager
import com.example.rememberme.domain.model.RemindersRepetition
import com.example.rememberme.domain.model.ThemeMode
import com.example.rememberme.utils.Constants
import com.example.rememberme.utils.Constants.REMEMBER_ME_SETTING_NAME
Expand All @@ -20,14 +21,15 @@ class SettingsManagerImpl @Inject constructor(
) : SettingsManager {
override suspend fun getThemeMode(): Flow<ThemeMode> {
Log.d(TAG, "getThemeMode: ")
return context.dataStore.data.map { preferences ->
val theme = context.dataStore.data.map { preferences ->
val ordinal = preferences[PreferencesKeys.UI_MODE] ?: ThemeMode.SYSTEM.ordinal
ThemeMode.entries[ordinal]
}
return theme
}

override suspend fun setThemeMode(themeMode: ThemeMode) {
Log.d(TAG, "setThemeMode: ")
Log.d(TAG, "setThemeMode: the new theme is: $themeMode ")
context.dataStore.edit { preferences ->
preferences[PreferencesKeys.UI_MODE] = when (themeMode) {
ThemeMode.LIGHT -> ThemeMode.LIGHT.ordinal
Expand All @@ -44,11 +46,30 @@ class SettingsManagerImpl @Inject constructor(
}
}

override suspend fun getSchedules(): Flow<RemindersRepetition> {
return context.dataStore.data.map {preferences ->
val ordinal = preferences[PreferencesKeys.SCHEDULE] ?: RemindersRepetition.OnceADay.ordinal
RemindersRepetition.entries[ordinal]
}
}

override suspend fun setSchedules(schedules: RemindersRepetition) {
context.dataStore.edit { preferences ->
preferences[PreferencesKeys.SCHEDULE] = when (schedules) {
RemindersRepetition.OnceADay -> RemindersRepetition.OnceADay.ordinal
RemindersRepetition.ThreeADay -> RemindersRepetition.ThreeADay.ordinal
RemindersRepetition.FiveADay -> RemindersRepetition.FiveADay.ordinal

}
}
}

companion object {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = REMEMBER_ME_SETTING_NAME)

private object PreferencesKeys {
val UI_MODE = intPreferencesKey(Constants.UI_MODE)
val SCHEDULE = intPreferencesKey(Constants.SCHEDULE)
}

private const val TAG = "SettingsManagerImpl"
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/example/rememberme/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import com.example.rememberme.domain.usecases.people.GetPersonById
import com.example.rememberme.domain.usecases.people.InsertNewPerson
import com.example.rememberme.domain.usecases.people.PeopleUseCases
import com.example.rememberme.domain.usecases.people.UpdatePerson
import com.example.rememberme.domain.usecases.reminders.GetSchedule
import com.example.rememberme.domain.usecases.reminders.ReminderUseCases
import com.example.rememberme.domain.usecases.reminders.SetSchedule
import com.example.rememberme.domain.usecases.theme.GetThemeMode
import com.example.rememberme.domain.usecases.theme.IsDarkModeEnabled
import com.example.rememberme.domain.usecases.theme.SetThemeMode
Expand Down Expand Up @@ -107,4 +110,15 @@ object AppModule {
isDarkModeEnabled = IsDarkModeEnabled(settingsManager)
)
}

@Provides
@Singleton
fun provideReminderUseCases(
settingsManager: SettingsManager
): ReminderUseCases{
return ReminderUseCases(
getSchedule = GetSchedule(settingsManager),
setSchedule = SetSchedule(settingsManager)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.example.rememberme.domain.manager

import com.example.rememberme.domain.model.RemindersRepetition
import com.example.rememberme.domain.model.ThemeMode
import kotlinx.coroutines.flow.Flow

interface SettingsManager {
suspend fun getThemeMode(): Flow<ThemeMode>
suspend fun setThemeMode(themeMode: ThemeMode)
suspend fun isDarkModeEnabled(): Flow<Boolean>
suspend fun getSchedules(): Flow<RemindersRepetition>
suspend fun setSchedules(schedules: RemindersRepetition)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.rememberme.domain.model

enum class RemindersRepetition {
OnceADay,
ThreeADay,
FiveADay
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.rememberme.domain.usecases.reminders

import com.example.rememberme.domain.manager.SettingsManager
import com.example.rememberme.domain.model.RemindersRepetition
import kotlinx.coroutines.flow.Flow

class GetSchedule(private val settingsManager: SettingsManager) {
suspend operator fun invoke(): Flow<RemindersRepetition> {
return settingsManager.getSchedules()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.rememberme.domain.usecases.reminders

data class ReminderUseCases(
val setSchedule: SetSchedule,
val getSchedule: GetSchedule
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.rememberme.domain.usecases.reminders

import com.example.rememberme.domain.manager.SettingsManager
import com.example.rememberme.domain.model.RemindersRepetition

class SetSchedule(private val settingsManager: SettingsManager) {
suspend operator fun invoke(remindersRepetition: RemindersRepetition) {
settingsManager.setSchedules(remindersRepetition)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package com.example.rememberme.presentation.addperson
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import android.util.Log
import android.view.ContextThemeWrapper
import android.widget.DatePicker
import android.widget.TimePicker
Expand Down Expand Up @@ -80,10 +79,10 @@ fun AddPersonScreen(
var showBottomSheet by remember { mutableStateOf(false) }
var selectedAvatarResId by remember {
if (personId == null) {
Log.i(TAG, "AddPersonScreen: Person is null")

mutableIntStateOf(R.drawable.ic_m1)
} else {
Log.i(TAG, "AddPersonScreen: Person is not null")

mutableIntStateOf(uiState.avatar)
}
}
Expand All @@ -93,10 +92,8 @@ fun AddPersonScreen(

LaunchedEffect(personId) {
if (personId != null) {
Log.i(TAG,"LoadPersonDetails on the addPerson screen")
viewModel.loadPersonDetails(personId)
} else {
Log.i(TAG,"creating new person screen")
viewModel.resetForm()
}
}
Expand Down Expand Up @@ -245,7 +242,6 @@ fun DateTimePicker(
val calendar = Calendar.getInstance()
val selectedDateTime = remember { mutableStateOf("") }
selectedDateTime.value = uiState.time
Log.i(TAG,"currentDate from the UiState: ${uiState.time}")

if (uiState.time.isBlank()) {
calendar.timeInMillis = System.currentTimeMillis()
Expand Down Expand Up @@ -361,7 +357,7 @@ fun GenderRadioButton(
modifier: Modifier = Modifier,
errorMessage: String?
) {
Log.d(TAG, "GenderRadioButtonError: $errorMessage")

val genderOptions = listOf("Male", "Female")
Column(modifier = modifier) {
Text("Gender", modifier = Modifier.padding(bottom = 4.dp))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.rememberme.presentation.navgraph

import android.util.Log
import androidx.compose.animation.AnimatedContentTransitionScope
import androidx.compose.animation.core.EaseIn
import androidx.compose.animation.core.EaseOut
Expand Down Expand Up @@ -101,7 +100,6 @@ fun NavGraph(
navController.navigateUp()
},
navigateToEditScreen = {id ->
Log.i(TAG, "NavGraph: NavigateToEditScreen triggered")
navController.navigate("${Routes.AddPersonScreen.route}/$id")
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.rememberme.presentation.peopleList

import android.content.res.Configuration
import android.util.Log
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
Expand Down Expand Up @@ -111,15 +110,11 @@ fun PeopleScreenContent(
label = "PeopleScreen animatedContent"
) { peopleState ->
if (peopleState.isLoading) {
Log.d(TAG, "PeopleScreenContent: Loading")
LoadingStateScreen(modifier = modifier.padding(paddingValues = it))
} else {
Log.d(TAG, "PeopleScreenContent: ${peopleState.people}")
if (peopleState.people.isEmpty()) {
Log.d(TAG, "PeopleScreenContent: Empty")
EmptyStateScreen(modifier = modifier.padding(paddingValues = it))
} else {
Log.d(TAG, "PeopleScreenContent: ${peopleState.people}")
LazyColumn(
modifier = modifier
.padding(it)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.example.rememberme.presentation.settings

import com.example.rememberme.domain.model.RemindersRepetition
import com.example.rememberme.domain.model.ThemeMode

sealed class SettingsEvent {
data class ChangeTheme(val theme: ThemeMode) : SettingsEvent()
data object GetTheme : SettingsEvent()

data class ChangeRemindersRepetition(val repetition: RemindersRepetition) : SettingsEvent()
data object GetRemindersRepetition : SettingsEvent()
}
Loading

0 comments on commit 3bba7f5

Please sign in to comment.