-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve widget & preview layouts * Small widget opens current day list page * Widget logic
- Loading branch information
Showing
14 changed files
with
392 additions
and
169 deletions.
There are no files selected for viewing
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
88 changes: 88 additions & 0 deletions
88
app/src/main/java/ru/beryukhov/coffeegram/pages/AppWidgetViewModel.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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package ru.beryukhov.coffeegram.pages | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.ViewModel | ||
import kotlinx.collections.immutable.PersistentList | ||
import kotlinx.collections.immutable.persistentListOf | ||
import kotlinx.collections.immutable.toPersistentList | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.todayIn | ||
import ru.beryukhov.coffeegram.data.CoffeeType | ||
import ru.beryukhov.coffeegram.data.CoffeeTypeWithCount | ||
import ru.beryukhov.coffeegram.data.DayCoffee | ||
import ru.beryukhov.coffeegram.model.DaysCoffeesIntent | ||
import ru.beryukhov.coffeegram.model.DaysCoffeesState | ||
import ru.beryukhov.coffeegram.model.DaysCoffeesStore | ||
|
||
interface AppWidgetViewModel { | ||
fun getCurrentDayCupsCount(): Int | ||
fun getCurrentDayMostPopularWithCount(): CoffeeTypeWithCount | ||
fun getCurrentDayList(): PersistentList<CoffeeTypeWithCount> | ||
|
||
fun currentDayIncrement(coffeeType: CoffeeType) | ||
fun currentDayDecrement(coffeeType: CoffeeType) | ||
} | ||
|
||
class AppWidgetViewModelStub : AppWidgetViewModel { | ||
override fun getCurrentDayCupsCount(): Int = 0 | ||
|
||
override fun getCurrentDayMostPopularWithCount(): CoffeeTypeWithCount = | ||
CoffeeTypeWithCount(CoffeeType.Cappuccino, 0) | ||
|
||
override fun getCurrentDayList(): PersistentList<CoffeeTypeWithCount> = | ||
persistentListOf(CoffeeTypeWithCount(CoffeeType.Cappuccino, 0)) | ||
|
||
override fun currentDayIncrement(coffeeType: CoffeeType) = Unit | ||
|
||
override fun currentDayDecrement(coffeeType: CoffeeType) = Unit | ||
} | ||
|
||
class AppWidgetViewModelImpl( | ||
private val daysCoffeesStore: DaysCoffeesStore, | ||
) : ViewModel(), AppWidgetViewModel { | ||
|
||
private fun getCurrentDay() = Clock.System.todayIn(TimeZone.currentSystemDefault()) | ||
|
||
override fun getCurrentDayCupsCount(): Int { | ||
return getCurrentDayList().sumOf { it.count } | ||
} | ||
|
||
override fun getCurrentDayMostPopularWithCount(): CoffeeTypeWithCount { | ||
return getCurrentDayList().first() | ||
} | ||
|
||
// coffee list does not contains 0 values and sorted by count | ||
override fun getCurrentDayList(): PersistentList<CoffeeTypeWithCount> { | ||
val dayCoffeeState: DaysCoffeesState = daysCoffeesStore.state.value | ||
val dayCoffee = dayCoffeeState.value[getCurrentDay()] ?: DayCoffee() | ||
val list = dayCoffee.coffeeCountMap.toList() | ||
.sortedByDescending { it.second } | ||
.map { CoffeeTypeWithCount(it.first, it.second) } | ||
val emptyListMock = listOf(CoffeeTypeWithCount(CoffeeType.Cappuccino, 0)) | ||
return list.ifEmpty { emptyListMock }.toPersistentList() | ||
} | ||
|
||
override fun currentDayDecrement(coffeeType: CoffeeType) { | ||
newIntent( | ||
DaysCoffeesIntent.MinusCoffee( | ||
localDate = getCurrentDay(), | ||
coffeeType = coffeeType | ||
) | ||
) | ||
} | ||
|
||
override fun currentDayIncrement(coffeeType: CoffeeType) { | ||
Log.d("TEST||", "currentDayIncrement: coffeeType = $coffeeType") | ||
newIntent( | ||
DaysCoffeesIntent.PlusCoffee( | ||
localDate = getCurrentDay(), | ||
coffeeType = coffeeType | ||
) | ||
) | ||
} | ||
|
||
private fun newIntent(intent: DaysCoffeesIntent) { | ||
daysCoffeesStore.newIntent(intent) | ||
} | ||
} |
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
Oops, something went wrong.