Improve code, still not completely working.
This commit is contained in:
parent
b32701b138
commit
8ff9b5ff4d
7 changed files with 33 additions and 34 deletions
|
@ -6,6 +6,9 @@ import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
|
import kotlinx.serialization.decodeFromString
|
||||||
|
import kotlinx.serialization.encodeToString
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
abstract class BasePreferenceManager(
|
abstract class BasePreferenceManager(
|
||||||
|
@ -18,13 +21,16 @@ abstract class BasePreferenceManager(
|
||||||
private fun getInt(key: String, defaultValue: Int) = prefs.getInt(key, defaultValue)
|
private fun getInt(key: String, defaultValue: Int) = prefs.getInt(key, defaultValue)
|
||||||
private fun getFloat(key: String, defaultValue: Float) = prefs.getFloat(key, defaultValue)
|
private fun getFloat(key: String, defaultValue: Float) = prefs.getFloat(key, defaultValue)
|
||||||
private fun getColor(key: String, defaultValue: Color): Color {
|
private fun getColor(key: String, defaultValue: Color): Color {
|
||||||
val c = prefs.getString(key, null)
|
val color = prefs.getString(key, null)
|
||||||
return if (c == null) defaultValue else Color(c.toULong())
|
return if (color == null) defaultValue else Color(color.toULong())
|
||||||
}
|
}
|
||||||
|
|
||||||
protected inline fun <reified E : Enum<E>> getEnum(key: String, defaultValue: E) =
|
protected inline fun <reified E : Enum<E>> getEnum(key: String, defaultValue: E) =
|
||||||
enumValueOf<E>(getString(key, defaultValue.name))
|
enumValueOf<E>(getString(key, defaultValue.name))
|
||||||
|
|
||||||
|
protected inline fun <reified T> getJson(key: String, defaultValue: T) =
|
||||||
|
Json.decodeFromString<T>(getString(key, null)) ?: defaultValue
|
||||||
|
|
||||||
protected fun putString(key: String, value: String?) = prefs.edit { putString(key, value) }
|
protected fun putString(key: String, value: String?) = prefs.edit { putString(key, value) }
|
||||||
private fun putBoolean(key: String, value: Boolean) = prefs.edit { putBoolean(key, value) }
|
private fun putBoolean(key: String, value: Boolean) = prefs.edit { putBoolean(key, value) }
|
||||||
private fun putInt(key: String, value: Int) = prefs.edit { putInt(key, value) }
|
private fun putInt(key: String, value: Int) = prefs.edit { putInt(key, value) }
|
||||||
|
@ -35,6 +41,9 @@ abstract class BasePreferenceManager(
|
||||||
protected inline fun <reified E : Enum<E>> putEnum(key: String, value: E) =
|
protected inline fun <reified E : Enum<E>> putEnum(key: String, value: E) =
|
||||||
putString(key, value.name)
|
putString(key, value.name)
|
||||||
|
|
||||||
|
protected inline fun <reified T> putJson(key: String, value: T) =
|
||||||
|
putString(key, Json.encodeToString(value))
|
||||||
|
|
||||||
protected class Preference<T>(
|
protected class Preference<T>(
|
||||||
private val key: String,
|
private val key: String,
|
||||||
defaultValue: T,
|
defaultValue: T,
|
||||||
|
@ -109,4 +118,14 @@ abstract class BasePreferenceManager(
|
||||||
getter = ::getEnum,
|
getter = ::getEnum,
|
||||||
setter = ::putEnum
|
setter = ::putEnum
|
||||||
)
|
)
|
||||||
|
|
||||||
|
protected inline fun <reified T> jsonPreference(
|
||||||
|
key: String,
|
||||||
|
defaultValue: T
|
||||||
|
) = Preference(
|
||||||
|
key = key,
|
||||||
|
defaultValue = defaultValue,
|
||||||
|
getter = ::getJson,
|
||||||
|
setter = ::putJson
|
||||||
|
)
|
||||||
}
|
}
|
|
@ -31,7 +31,8 @@ class QWeatherActivity : ComponentActivity() {
|
||||||
Theme.LIGHT -> false
|
Theme.LIGHT -> false
|
||||||
Theme.DARK -> true
|
Theme.DARK -> true
|
||||||
}
|
}
|
||||||
val isLocationSet = location.getLocations().isNotEmpty()
|
val locations = location.locations
|
||||||
|
val isLocationSet = locations.isNotEmpty()
|
||||||
|
|
||||||
WeatherAppTheme(darkTheme = isDark, monet = prefs.monet) {
|
WeatherAppTheme(darkTheme = isDark, monet = prefs.monet) {
|
||||||
Surface(modifier = Modifier.fillMaxSize()) {
|
Surface(modifier = Modifier.fillMaxSize()) {
|
||||||
|
|
|
@ -26,7 +26,7 @@ fun LocationsDrawer(drawerState: DrawerState, children: @Composable () -> Unit)
|
||||||
ModalNavigationDrawer(drawerContent = {
|
ModalNavigationDrawer(drawerContent = {
|
||||||
ModalDrawerSheet {
|
ModalDrawerSheet {
|
||||||
Column(modifier = Modifier.padding(16.dp)) {
|
Column(modifier = Modifier.padding(16.dp)) {
|
||||||
val locations = location.getLocations()
|
val locations = location.locations
|
||||||
|
|
||||||
Text(
|
Text(
|
||||||
text = stringResource(id = R.string.locations),
|
text = stringResource(id = R.string.locations),
|
||||||
|
@ -36,10 +36,10 @@ fun LocationsDrawer(drawerState: DrawerState, children: @Composable () -> Unit)
|
||||||
locations.forEachIndexed { index, data ->
|
locations.forEachIndexed { index, data ->
|
||||||
NavigationDrawerItem(
|
NavigationDrawerItem(
|
||||||
label = { Text(text = data.location) },
|
label = { Text(text = data.location) },
|
||||||
selected = index == location.selectedLocation,
|
selected = index == location.selectedIndex,
|
||||||
onClick = { location.selectedLocation = index },
|
onClick = { location.selectedIndex = index },
|
||||||
badge = {
|
badge = {
|
||||||
IconButton(onClick = { location.removeLocation(data) }) {
|
IconButton(onClick = { location.locations -= data }) {
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = Icons.Default.Delete,
|
imageVector = Icons.Default.Delete,
|
||||||
contentDescription = stringResource(
|
contentDescription = stringResource(
|
||||||
|
|
|
@ -43,7 +43,7 @@ class LocationPickerScreen : Screen {
|
||||||
floatingActionButton = {
|
floatingActionButton = {
|
||||||
FloatingActionButton(onClick = {
|
FloatingActionButton(onClick = {
|
||||||
location?.let {
|
location?.let {
|
||||||
screenModel.prefs.addLocation(it)
|
screenModel.prefs.locations += it
|
||||||
navigator?.push(MainScreen())
|
navigator?.push(MainScreen())
|
||||||
} ?: kotlin.run { isAboutOpen = true }
|
} ?: kotlin.run { isAboutOpen = true }
|
||||||
}) {
|
}) {
|
||||||
|
|
|
@ -23,7 +23,7 @@ class DailyWeatherScreenModel(
|
||||||
) : ScreenModel {
|
) : ScreenModel {
|
||||||
var state by mutableStateOf(DailyWeatherState())
|
var state by mutableStateOf(DailyWeatherState())
|
||||||
private set
|
private set
|
||||||
val location = locationPreferenceManager.getSelectedLocation()
|
val location = locationPreferenceManager.locations[locationPreferenceManager.selectedIndex]
|
||||||
|
|
||||||
fun loadWeatherInfo(cache: Boolean = true) {
|
fun loadWeatherInfo(cache: Boolean = true) {
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
|
|
|
@ -24,7 +24,7 @@ class HourlyWeatherScreenModel(
|
||||||
var state by mutableStateOf(HourlyWeatherState())
|
var state by mutableStateOf(HourlyWeatherState())
|
||||||
private set
|
private set
|
||||||
|
|
||||||
val location = locationPreferenceManager.getSelectedLocation()
|
val location = locationPreferenceManager.locations[locationPreferenceManager.selectedIndex]
|
||||||
|
|
||||||
fun loadWeatherInfo(cache: Boolean = true) {
|
fun loadWeatherInfo(cache: Boolean = true) {
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
|
|
|
@ -11,9 +11,6 @@ import com.henryhiles.qweather.domain.manager.BasePreferenceManager
|
||||||
import com.henryhiles.qweather.domain.repository.GeocodingRepository
|
import com.henryhiles.qweather.domain.repository.GeocodingRepository
|
||||||
import com.henryhiles.qweather.domain.util.Resource
|
import com.henryhiles.qweather.domain.util.Resource
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.serialization.decodeFromString
|
|
||||||
import kotlinx.serialization.encodeToString
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
|
|
||||||
data class LocationPickerState(
|
data class LocationPickerState(
|
||||||
val locations: List<GeocodingData>? = null,
|
val locations: List<GeocodingData>? = null,
|
||||||
|
@ -23,29 +20,11 @@ data class LocationPickerState(
|
||||||
|
|
||||||
class LocationPreferenceManager(context: Context) :
|
class LocationPreferenceManager(context: Context) :
|
||||||
BasePreferenceManager(context.getSharedPreferences("location", Context.MODE_PRIVATE)) {
|
BasePreferenceManager(context.getSharedPreferences("location", Context.MODE_PRIVATE)) {
|
||||||
private var locations by stringPreference(
|
var locations by jsonPreference<List<GeocodingData>>(
|
||||||
"locations",
|
"locations",
|
||||||
Json.encodeToString(value = listOf<GeocodingData>())
|
listOf()
|
||||||
)
|
)
|
||||||
var selectedLocation by intPreference("selected_location", 0)
|
var selectedIndex by intPreference("selected_location", 0)
|
||||||
|
|
||||||
fun getSelectedLocation(): GeocodingData {
|
|
||||||
return getLocations()[selectedLocation]
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getLocations(): List<GeocodingData> {
|
|
||||||
return Json.decodeFromString(string = locations)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addLocation(location: GeocodingData) {
|
|
||||||
val currentLocations = getLocations()
|
|
||||||
locations = Json.encodeToString(value = currentLocations + location)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeLocation(location: GeocodingData) {
|
|
||||||
val currentLocations = getLocations()
|
|
||||||
locations = Json.encodeToString(value = currentLocations - location)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class LocationPickerScreenModel(
|
class LocationPickerScreenModel(
|
||||||
|
|
Reference in a new issue