Enable Time Zones

This commit is contained in:
Sad Ellie 2023-10-02 21:31:19 +03:00
parent fbe980ca94
commit 1279a8acad
6 changed files with 54 additions and 79 deletions

View File

@ -47,10 +47,10 @@ import com.sadellie.unitto.core.ui.common.open
import com.sadellie.unitto.core.ui.common.rememberUnittoDrawerState
import com.sadellie.unitto.core.ui.model.DrawerItems
import com.sadellie.unitto.core.ui.pushDynamicShortcut
import com.sadellie.unitto.core.ui.theme.TypographySystem
import com.sadellie.unitto.core.ui.theme.TypographyUnitto
import com.sadellie.unitto.core.ui.theme.DarkThemeColors
import com.sadellie.unitto.core.ui.theme.LightThemeColors
import com.sadellie.unitto.core.ui.theme.TypographySystem
import com.sadellie.unitto.core.ui.theme.TypographyUnitto
import com.sadellie.unitto.data.userprefs.AppPreferences
import io.github.sadellie.themmo.Themmo
import io.github.sadellie.themmo.rememberThemmoController
@ -79,23 +79,15 @@ internal fun UnittoApp(prefs: AppPreferences) {
val shortcutsScope = rememberCoroutineScope()
val tabs by remember(prefs.enableToolsExperiment) {
derivedStateOf {
if (prefs.enableToolsExperiment) {
listOf(
DrawerItems.Calculator,
DrawerItems.Converter,
DrawerItems.DateDifference,
DrawerItems.TimeZones,
)
} else {
listOf(
DrawerItems.Calculator,
DrawerItems.Converter,
DrawerItems.DateDifference,
)
}
}
val tabs by remember {
mutableStateOf(
listOf(
DrawerItems.Calculator,
DrawerItems.Converter,
DrawerItems.DateDifference,
DrawerItems.TimeZones
)
)
}
val navBackStackEntry by navController.currentBackStackEntryAsState()

View File

@ -106,15 +106,15 @@ val TOP_LEVEL_DESTINATIONS by lazy {
TopLevelDestinations.Calculator,
TopLevelDestinations.Converter,
TopLevelDestinations.DateCalculator,
// TopLevelDestinations.TimeZone,
TopLevelDestinations.TimeZone,
)
}
// Only routes, not graphs!
val TOP_LEVEL_START_ROUTES by lazy {
listOf(
CONVERTER_START,
CALCULATOR_START,
CONVERTER_START,
DATE_CALCULATOR_START,
TIME_ZONE_START,
)

View File

@ -45,17 +45,16 @@ class UnittoDatabaseModule {
return unittoDatabase.calculatorHistoryDao()
}
// For some reason this fucks up the migration
// /**
// * Tells Hilt to use this method to get [TimeZoneDao]
// *
// * @param unittoDatabase Database for which we need DAO
// * @return Singleton of [TimeZoneDao]
// */
// @Provides
// fun provideTimeZoneDao(unittoDatabase: UnittoDatabase): TimeZoneDao {
// return unittoDatabase.timeZoneDao()
// }
/**
* Tells Hilt to use this method to get [TimeZoneDao]
*
* @param unittoDatabase Database for which we need DAO
* @return Singleton of [TimeZoneDao]
*/
@Provides
fun provideTimeZoneDao(unittoDatabase: UnittoDatabase): TimeZoneDao {
return unittoDatabase.timeZoneDao()
}
@Provides
fun provideCurrencyRatesDao(unittoDatabase: UnittoDatabase): CurrencyRatesDao {

View File

@ -19,62 +19,48 @@
package com.sadellie.unitto.data.timezone
import com.sadellie.unitto.data.common.lev
import com.sadellie.unitto.data.database.TimeZoneDao
import com.sadellie.unitto.data.database.TimeZoneEntity
import com.sadellie.unitto.data.model.UnittoTimeZone
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class TimeZonesRepository @Inject constructor() {
class TimeZonesRepository @Inject constructor(
private val dao: TimeZoneDao
) {
private val allTimeZones: HashMap<String, UnittoTimeZone> = hashMapOf(
"zulu_time_zone" to UnittoTimeZone(id = "zulu_time_zone", nameRes = "Zulu Time Zone", offsetSeconds = 0)
)
val favoriteTimeZones: MutableStateFlow<List<UnittoTimeZone>> = MutableStateFlow(emptyList())
val favoriteTimeZones: Flow<List<UnittoTimeZone>> = dao
.getAll()
.map { list ->
val favorites = mutableListOf<UnittoTimeZone>()
list.forEach { entity ->
val foundTimeZone = allTimeZones[entity.id] ?: return@forEach
val mapped = foundTimeZone.copy(
position = entity.position
)
favorites.add(mapped)
}
// UNCOMMENT FOR RELEASE
// val favoriteTimeZones: Flow<List<UnittoTimeZone>> = dao
// .getAll()
// .map { list ->
// val favorites = mutableListOf<UnittoTimeZone>()
// list.forEach { entity ->
// val foundTimeZone = allTimeZones[entity.id] ?: return@forEach
// val mapped = foundTimeZone.copy(
// position = entity.position
// )
// favorites.add(mapped)
// }
//
// favorites
// }
favorites
}
suspend fun swapTimeZones(from: String, to: String) = withContext(Dispatchers.IO) {
// UNCOMMENT FOR RELEASE
// dao.swap(from, to)
favoriteTimeZones.update {
val fromIndex = it.indexOfFirst { it.id == from }
val toIndex = it.indexOfFirst { it.id == to }
it
.toMutableList()
.apply {
add(toIndex, removeAt(fromIndex))
}
}
dao.swap(from, to)
return@withContext
}
suspend fun delete(timeZone: UnittoTimeZone) = withContext(Dispatchers.IO) {
// UNCOMMENT FOR RELEASE
// // Only PrimaryKey is needed
// dao.remove(TimeZoneEntity(id = timeZone.id, position = 0))
favoriteTimeZones.update { it.minus(timeZone) }
// Only PrimaryKey is needed
dao.remove(TimeZoneEntity(id = timeZone.id, position = 0))
}
suspend fun filterAllTimeZones(searchQuery: String): List<UnittoTimeZone> =
@ -118,12 +104,11 @@ class TimeZonesRepository @Inject constructor() {
suspend fun addToFavorites(timeZone: UnittoTimeZone) {
// UNCOMMENT FOR RELEASE
// dao.insert(
// TimeZoneEntity(
// id = timeZone.id,
// position = System.currentTimeMillis().toInt()
// )
// )
favoriteTimeZones.update { it.plus(timeZone) }
dao.insert(
TimeZoneEntity(
id = timeZone.id,
position = System.currentTimeMillis().toInt()
)
)
}
}

View File

@ -32,8 +32,8 @@ import com.sadellie.unitto.feature.timezone.TimeZoneRoute
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
private val graph = TopLevelDestinations.TimeZone.start
private val start: String = TopLevelDestinations.TimeZone.graph
private val graph = TopLevelDestinations.TimeZone.graph
private val start = TopLevelDestinations.TimeZone.start
private const val ADD_TIME_ZONE_ROUTE = "ADD_TIME_ZONE_ROUTE"
private const val USER_TIME_ARG = "USER_TIME_ARG"

View File

@ -28,7 +28,6 @@ include(":feature:timezone")
include(":feature:settings")
include(":data:userprefs")
include(":data:licenses")
// include(":data:epoch")
include(":data:calculator")
include(":data:database")
include(":data:model")