Skip to content

Commit

Permalink
move weather data to different shared prefs
Browse files Browse the repository at this point in the history
  • Loading branch information
moehriegitt committed Sep 28, 2024
1 parent 98329bc commit 92cdc50
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 36 deletions.
24 changes: 14 additions & 10 deletions app/src/main/java/de/theiling/neatlauncher/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ import java.util.Date
import java.util.Locale
import kotlin.math.abs

// non-configurable timing settings for weather updates
const val weatherUpdateMillis = 60L * 60_000L
const val weatherTryLongMillis = 15L * 60_000L
const val weatherTryShortMillis = 5L * 60_000L
const val weatherTryMinMillis = 1_000L

class MainActivity:
AppCompatActivity(),
ItemAdapter.ClickListener,
Expand Down Expand Up @@ -137,14 +143,8 @@ class MainActivity:
private var searchStr: String = ""
private var clockValid = false
private var clockWeatherValid = false
private var weatherData: WeatherData? = null
private var weatherUpdateMillis = 60L * 60_000L // currently not configurable
private var weatherTryLongMillis = 15L * 60_000L
private var weatherTryShortMillis = 5L * 60_000L
private var weatherTryMinMillis = 1_000L
private var weatherTryLast = Date(0)
private var weatherTryNext = Date(0)
private var weatherCurrentUpdate = false
private var weatherData: WeatherData? = null
private lateinit var z: MainActivityBinding
private lateinit var searchEngine: SearchEngine
private lateinit var weather: WeatherEngine
Expand All @@ -158,6 +158,8 @@ class MainActivity:
private lateinit var contactChoice: BoolContact
private lateinit var weekStart: EnumWstart
private lateinit var weatherType: EnumTweath
private lateinit var weatherTryLast: StateWeatherTryLast
private lateinit var weatherTryNext: StateWeatherTryNext

// override on....()
override fun onCreate(
Expand All @@ -181,6 +183,8 @@ class MainActivity:
ttypeChoice = EnumTtype(c) { weatherRedraw() }
weekStart = EnumWstart(c) { weatherRedraw() }
weatherType = EnumTweath(c) { onWeatherData() }
weatherTryLast = StateWeatherTryLast(c)
weatherTryNext = StateWeatherTryNext(c)

contactChoice = BoolContact(c) {
if (it >= 0) { // -1 resets but does not trigger itemsNotifyChange()
Expand Down Expand Up @@ -1174,8 +1178,8 @@ class MainActivity:
onWeatherData()
}
weatherCurrentUpdate = false
weatherTryLast = now
weatherTryNext = Date(now.time + weatherTryLongMillis)
weatherTryLast.time = now.time
weatherTryNext.time = now.time + weatherTryLongMillis

if (loc.isCurrent)
locTryRequest()
Expand All @@ -1198,7 +1202,7 @@ class MainActivity:
}
} catch (e: UnknownHostException) {
// ignore: we have no internet access, but try again sooner
weatherTryNext = Date(Date().time + weatherTryShortMillis)
weatherTryNext.time = Date().time + weatherTryShortMillis
} catch (e: Exception) {
runOnUiThread {
longToast(getString(R.string.error_msg, "$e"))
Expand Down
74 changes: 49 additions & 25 deletions app/src/main/java/de/theiling/neatlauncher/Pref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package de.theiling.neatlauncher
import android.content.Context
import android.content.SharedPreferences
import org.json.JSONObject
import java.util.Date
import java.util.Locale

const val PREF_MAIN = "de.theiling.neatlauncher.PREF_MAIN"

fun pref(c: Context): SharedPreferences =
c.getSharedPreferences(PREF_MAIN, Context.MODE_PRIVATE)
c.getSharedPreferences("de.theiling.neatlauncher.PREF_MAIN", Context.MODE_PRIVATE)

fun prefPutBool(c: Context, key: String, v: Boolean, def: Boolean) =
with (pref(c).edit()) {
fun state(c: Context): SharedPreferences =
c.getSharedPreferences("de.theiling.neatlauncher.STATE_MAIN", Context.MODE_PRIVATE)

fun prefPutBool(p: SharedPreferences, key: String, v: Boolean, def: Boolean) =
with (p.edit()) {
if (v == def) {
remove(key)
} else {
Expand All @@ -20,8 +22,18 @@ fun prefPutBool(c: Context, key: String, v: Boolean, def: Boolean) =
apply()
}

fun prefPutString(c: Context, key: String, v: String, def: String) =
with (pref(c).edit()) {
fun prefPutLong(p: SharedPreferences, key: String, v: Long, def: Long) =
with (p.edit()) {
if (v == def) {
remove(key)
} else {
putLong(key, v)
}
apply()
}

fun prefPutString(p: SharedPreferences, key: String, v: String, def: String) =
with (p.edit()) {
if (v == def) {
remove(key)
} else {
Expand All @@ -30,16 +42,16 @@ fun prefPutString(c: Context, key: String, v: String, def: String) =
apply()
}

fun prefPutEnum(c: Context, arrId: Int, key: String, i: Int, def: Int) =
prefPutString(c, key,
fun prefPutEnum(c: Context, p: SharedPreferences, arrId: Int, key: String, i: Int, def: Int) =
prefPutString(p, key,
(try {
if (i == def) "" else c.resources.getTextArray(arrId)[i].toString()
} catch (_: Exception) { "" }),
"")

fun prefGetEnum(c: Context, arrId: Int, key: String, def: Int): Int {
fun prefGetEnum(c: Context, p: SharedPreferences, arrId: Int, key: String, def: Int): Int {
try {
val s = pref(c).getString(key, null)!!
val s = p.getString(key, null)!!
c.resources.getTextArray(arrId).forEachIndexed { i, v ->
if (v.toString() == s) {
return i
Expand All @@ -55,29 +67,24 @@ fun getItemInfo(c: Context, type: String, pack: String, klass: String): String?
pref(c).getString(keyItemInfo(type, pack, klass), null)

fun setItemInfo(c: Context, type: String, pack: String, klass: String, v: String, def: String) =
prefPutString(c, keyItemInfo(type, pack, klass), v, def)
prefPutString(pref(c), keyItemInfo(type, pack, klass), v, def)

fun getSearchEngine(c: Context) = pref(c).getString("searchEngine", "")!!
fun setSearchEngine(c: Context, s: String) = prefPutString(c, "searchEngine", s, "")
fun setSearchEngine(c: Context, s: String) = prefPutString(pref(c), "searchEngine", s, "")

fun getWeatherLoc(c: Context) = pref(c).getString("weatherLoc", "")!!
fun setWeatherLoc(c: Context, s: String) = prefPutString(c, "weatherLoc", s, "")

fun getWeatherData(c: Context) = pref(c).getString("weatherData", "")!!
fun setWeatherData(c: Context, s: String) = prefPutString(c, "weatherData", s, "")
fun setWeatherLoc(c: Context, s: String) = prefPutString(pref(c), "weatherLoc", s, "")

fun prefDoSave(s: String) = when (s) {
"weatherData" -> false
else -> true
}
fun getWeatherData(c: Context) = state(c).getString("weatherData", "")!!
fun setWeatherData(c: Context, s: String) = prefPutString(state(c), "weatherData", s, "")

fun prefToJson(c: Context) = JSONObject().apply {
put("pack", c.packageName)
put("ver", c.packageManager.getPackageInfo(c.packageName, 0).versionCode)
put("lang", Locale.getDefault().toString())
put("pref", JSONObject().apply {
for ((k,v) in pref(c).all) {
if (prefDoSave(k)) put(k, v)
put(k, v)
}
})
}
Expand Down Expand Up @@ -128,11 +135,11 @@ abstract class PrefEnum(
prefKey: String, defVal: Int, onChange: (Int) -> Unit): this (
c, titleId, { c.resources.getStringArray(nameArrId) }, keyArrId, prefKey, defVal, onChange)

override var x = prefGetEnum(c, keyArrId, prefKey, defVal)
override var x = prefGetEnum(c, pref(c), keyArrId, prefKey, defVal)
set(new) {
if (field != new) {
field = new
prefPutEnum(c, keyArrId, prefKey, field, defVal)
prefPutEnum(c, pref(c), keyArrId, prefKey, field, defVal)
onChange(field)
}
}
Expand All @@ -159,12 +166,26 @@ abstract class PrefBool(
val newI = if (new > 0) 1 else 0
if (field != newI) {
field = newI
prefPutBool(c, prefKey, field > 0, defVal)
prefPutBool(pref(c), prefKey, field > 0, defVal)
onChange(new) // pass exact int value
}
}
}

abstract class StateDate(
private val c: Context,
private val prefKey: String)
{
var date = Date(state(c).getLong(prefKey, 0) ?: 0)
set(new) {
field = new
prefPutLong(state(c), prefKey, new.time, 0)
}
var time: Long
get() = date.time
set(new) { date = Date(new) }
}

class EnumDate(c: Context, onChange: (Int) -> Unit): PrefEnum(c, R.string.date_choice_title,
R.array.date_choice, R.array.date_choice_key, "dateChoice", date_yyyy, onChange)

Expand Down Expand Up @@ -194,3 +215,6 @@ class BoolContact(c: Context, onChange: (Int) -> Unit): PrefBool(c,

class EnumWstart(c: Context, onChange: (Int) -> Unit): PrefWeekday(c,
R.string.wstart_choice_title, "weekStart", weekday_mon, onChange)

class StateWeatherTryNext(c: Context): StateDate(c, "weathTryNext")
class StateWeatherTryLast(c: Context): StateDate(c, "weathTryLast")
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ class WeatherData(
val step = mutableListOf<WeatherStep>()
var k: String? = null
var c = WeatherCode(0)
var update = Date()
var update = Date(0)
var s = Date(0)
var sRise = Date(0)
var sSet = Date(0)
Expand Down

0 comments on commit 92cdc50

Please sign in to comment.