2019-10-21 20:58:09 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
// This is example code for the 'Simplified Example' section of
|
|
|
|
// /docs/architecture-overview.md
|
|
|
|
class ContactsStore(
|
2022-11-03 02:37:21 +00:00
|
|
|
private val initialState: ContactsState,
|
2019-10-21 20:58:09 +00:00
|
|
|
) : Store<ContactsState, Reducer<ContactState, ContactsAction>>(initialState, ::reducer)
|
|
|
|
|
|
|
|
sealed class ContactsAction {
|
|
|
|
data class ContactRenamed(val contactId: Int, val newName: String) : ContactsAction
|
|
|
|
data class ThemeChanged(val newTheme: Theme) : ContactsAction
|
|
|
|
}
|
|
|
|
|
|
|
|
data class ContactsState(
|
|
|
|
val contacts: List<Contact>,
|
2022-11-03 02:37:21 +00:00
|
|
|
val theme: Theme,
|
2019-10-21 20:58:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
data class Contact(
|
|
|
|
val name: String,
|
|
|
|
val id: Int,
|
2022-11-03 02:37:21 +00:00
|
|
|
val imageUrl: Uri,
|
2019-10-21 20:58:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
enum class Theme {
|
|
|
|
ORANGE, DARK
|
|
|
|
}
|
|
|
|
|
|
|
|
fun reducer(oldState: ContactsState, action: ContactsAction): ContactsState = when (action) {
|
|
|
|
is ContactsAction.ThemeChanged -> oldState.copy(theme = action.newTheme)
|
|
|
|
is ContactsAction.ContactRenamed -> {
|
|
|
|
val newContacts = oldState.contacts.map { contact ->
|
|
|
|
// If this is the contact we want to change...
|
|
|
|
if (contact.id == action.contactId) {
|
|
|
|
// Update its name, but keep other values the same
|
|
|
|
contact.copy(name = newName)
|
|
|
|
} else {
|
|
|
|
// Otherwise return the original contact
|
|
|
|
return@map contact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldState.copy(contacts = newContacts)
|
|
|
|
}
|
|
|
|
}
|