From 81b1fe743ea89c054bbd9b673567f2d0f4526bd5 Mon Sep 17 00:00:00 2001 From: SvenLindstrom Date: Sat, 9 Nov 2024 09:52:51 +0100 Subject: [PATCH] fixed bug no longer crashes when toggling bookmarks --- .../app/tumble/data/repository/realm/RealmManager.kt | 7 +++++++ .../tumble/presentation/viewmodels/SettingsViewModel.kt | 9 +++++++-- .../views/Settings/Bookmarks/BookmarksSettings.kt | 8 +++++--- .../views/Settings/Bookmarks/BookmarksSettingsRow.kt | 9 +++------ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/tumble/app/tumble/data/repository/realm/RealmManager.kt b/app/src/main/java/tumble/app/tumble/data/repository/realm/RealmManager.kt index 3372b40..3afec44 100644 --- a/app/src/main/java/tumble/app/tumble/data/repository/realm/RealmManager.kt +++ b/app/src/main/java/tumble/app/tumble/data/repository/realm/RealmManager.kt @@ -73,6 +73,13 @@ class RealmManager { } } + suspend fun updateScheduleVisibility(scheduleId: String, visibility: Boolean){ + realm.write { + val schedule: Schedule? = this.query("scheduleId == $0", scheduleId).first().find() + schedule?.toggled = visibility + } + } + fun getCourseColors(): MutableMap { val courses: RealmResults = realm.query().find() val courseColors = mutableMapOf() diff --git a/app/src/main/java/tumble/app/tumble/presentation/viewmodels/SettingsViewModel.kt b/app/src/main/java/tumble/app/tumble/presentation/viewmodels/SettingsViewModel.kt index 88a2af5..ccf2243 100644 --- a/app/src/main/java/tumble/app/tumble/presentation/viewmodels/SettingsViewModel.kt +++ b/app/src/main/java/tumble/app/tumble/presentation/viewmodels/SettingsViewModel.kt @@ -3,19 +3,18 @@ package tumble.app.tumble.presentation.viewmodels import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel +import io.realm.kotlin.ext.copyFromRealm import kotlinx.coroutines.Job import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.launch -import tumble.app.tumble.data.api.Endpoint import tumble.app.tumble.data.notifications.NotificationManager import tumble.app.tumble.data.repository.preferences.CombinedData import tumble.app.tumble.data.repository.preferences.DataStoreManager import tumble.app.tumble.data.repository.realm.RealmManager import tumble.app.tumble.datasource.SchoolManager import tumble.app.tumble.domain.enums.Types.AppearanceType -import tumble.app.tumble.domain.enums.ViewType import tumble.app.tumble.domain.models.realm.Event import tumble.app.tumble.domain.models.realm.Schedule import tumble.app.tumble.presentation.views.Settings.Notifications.NotificationOffset @@ -110,6 +109,12 @@ class SettingsViewModel @Inject constructor( //TODO } + fun updateBookmarkVisibility(visibility: Boolean, schedule: Schedule){ + viewModelScope.launch { + realmManager.updateScheduleVisibility(schedule.scheduleId, visibility) + } + } + fun deleteAllSchedules(){ //TODO } diff --git a/app/src/main/java/tumble/app/tumble/presentation/views/Settings/Bookmarks/BookmarksSettings.kt b/app/src/main/java/tumble/app/tumble/presentation/views/Settings/Bookmarks/BookmarksSettings.kt index c7e6c05..0e711d0 100644 --- a/app/src/main/java/tumble/app/tumble/presentation/views/Settings/Bookmarks/BookmarksSettings.kt +++ b/app/src/main/java/tumble/app/tumble/presentation/views/Settings/Bookmarks/BookmarksSettings.kt @@ -40,12 +40,14 @@ fun BookmarksSettings( if (schedules.value.isNotEmpty()) { SettingsList { - schedules.value.forEachIndexed { index, schedule -> + schedules.value.forEach{ schedule -> BookmarkSettingsRow( schedule = schedule, - index = index, - onDelete = { offsets, id -> + onDelete = { offsets -> parentViewModel.deleteBookmark(schedule) + }, + onToggle = {visibility -> + parentViewModel.updateBookmarkVisibility(visibility, schedule) } ) } diff --git a/app/src/main/java/tumble/app/tumble/presentation/views/Settings/Bookmarks/BookmarksSettingsRow.kt b/app/src/main/java/tumble/app/tumble/presentation/views/Settings/Bookmarks/BookmarksSettingsRow.kt index 671d1ab..a24588a 100644 --- a/app/src/main/java/tumble/app/tumble/presentation/views/Settings/Bookmarks/BookmarksSettingsRow.kt +++ b/app/src/main/java/tumble/app/tumble/presentation/views/Settings/Bookmarks/BookmarksSettingsRow.kt @@ -36,8 +36,8 @@ import tumble.app.tumble.domain.models.realm.Schedule @Composable fun BookmarkSettingsRow( schedule: Schedule, - index: Int, - onDelete: (Int, String) -> Unit + onDelete: (String) -> Unit, + onToggle: (Boolean) -> Unit, ) { val scope = rememberCoroutineScope() @@ -60,15 +60,12 @@ fun BookmarkSettingsRow( checked = isToggled, onCheckedChange = { checked -> isToggled = checked - schedule.toggled = checked + onToggle(checked) // Trigger the widget update when the toggle changes }, colors = SwitchDefaults.colors(checkedThumbColor = MaterialTheme.colors.primary), modifier = Modifier.scale(2f) ) - - - } }