-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow individual reminders to be deactivated
- Loading branch information
Showing
12 changed files
with
705 additions
and
6 deletions.
There are no files selected for viewing
600 changes: 600 additions & 0 deletions
600
app/schemas/io.github.janmalch.woroboro.data.AppDatabase/8.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 31 additions & 1 deletion
32
app/src/main/kotlin/io/github/janmalch/woroboro/ui/reminder/ReminderListViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,57 @@ | ||
package io.github.janmalch.woroboro.ui.reminder | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import io.github.janmalch.woroboro.business.ReminderRepository | ||
import io.github.janmalch.woroboro.business.activate | ||
import io.github.janmalch.woroboro.business.deactivate | ||
import io.github.janmalch.woroboro.models.Reminder | ||
import io.github.janmalch.woroboro.ui.Outcome | ||
import kotlinx.collections.immutable.persistentListOf | ||
import kotlinx.collections.immutable.toImmutableList | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import kotlinx.coroutines.flow.stateIn | ||
import kotlinx.coroutines.launch | ||
import java.util.UUID | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class ReminderListViewModel @Inject constructor( | ||
reminderRepository: ReminderRepository, | ||
private val reminderRepository: ReminderRepository, | ||
) : ViewModel() { | ||
|
||
|
||
private val _onToggleReminder = Channel<Outcome>() | ||
val onToggleReminder = _onToggleReminder.receiveAsFlow() | ||
|
||
private val toggleReminderExceptionHandler = CoroutineExceptionHandler { _, exception -> | ||
Log.e("ReminderListViewModel", "Error while toggling reminder active state.", exception) | ||
viewModelScope.launch { | ||
_onToggleReminder.send(Outcome.Failure) | ||
} | ||
} | ||
|
||
val reminders = reminderRepository.findAll() | ||
.map(List<Reminder>::toImmutableList) | ||
.stateIn( | ||
scope = viewModelScope, | ||
started = SharingStarted.WhileSubscribed(5_000), | ||
initialValue = persistentListOf(), | ||
) | ||
|
||
fun toggleReminderActive(reminderId: UUID, shouldActivate: Boolean) { | ||
viewModelScope.launch(toggleReminderExceptionHandler) { | ||
if (shouldActivate) { | ||
reminderRepository.activate(reminderId) | ||
} else { | ||
reminderRepository.deactivate(reminderId) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters