[O] Preference by delegate

This commit is contained in:
Azalea Gui
2023-01-23 20:39:13 -05:00
parent 64879b6428
commit 7a61b871a9
4 changed files with 51 additions and 17 deletions
+4 -1
View File
@@ -42,6 +42,10 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'androidx.preference:preference:1.2.0'
// https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-reflect
implementation "org.jetbrains.kotlin:kotlin-reflect:1.8.0"
// Logger
implementation 'com.jakewharton.timber:timber:5.0.1'
@@ -51,7 +55,6 @@ dependencies {
// Database library
implementation "com.influxdb:influxdb-client-kotlin:6.3.0"
implementation 'androidx.preference:preference:1.2.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
@@ -31,7 +31,7 @@ class ActivityScan : AppCompatActivity() {
println("OnCreate called, Initializing...")
// Device exists
chosenDevice?.let { addr ->
prefs.chosenDevice?.let { addr ->
ble.connectAddress(addr) { connected(addr) }
return
}
@@ -86,7 +86,7 @@ class ActivityScan : AppCompatActivity() {
fun connected(address: String)
{
view.snack("✅ Connected.")
chosenDevice = address
prefs.chosenDevice = address
finish()
}
}
@@ -6,7 +6,12 @@ import android.content.Context
import android.content.Intent
import android.view.View
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import com.google.android.material.snackbar.Snackbar
import com.influxdb.client.kotlin.InfluxDBClientKotlin
import com.influxdb.client.kotlin.InfluxDBClientKotlinFactory
import kotlin.reflect.KProperty
fun View.snack(msg: String) = Snackbar.make(this, msg, Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
@@ -14,9 +19,36 @@ fun View.snack(msg: String) = Snackbar.make(this, msg, Snackbar.LENGTH_LONG)
inline fun <reified T> Context.getSysServ() = getSystemService(T::class.java) as T
fun Context.blueMan() = getSysServ<BluetoothManager>()
val Context.pref get() = getSharedPreferences("settings", Context.MODE_PRIVATE)
var Context.chosenDevice: String?
get() = pref.getString("mac", null)
set(value) = pref.edit { putString("mac", value) }
interface Prefs {
var chosenDevice: String?
var infUrl: String?
var infOrg: String?
var infBucket: String?
var infToken: String?
fun createInflux(): InfluxDBClientKotlin
}
val Context.pref get() = PreferenceManager.getDefaultSharedPreferences(this)
val Context.prefs get() = object : Prefs {
inner class PrefDelegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>) = pref.getString(property.name, null)
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String?) {
pref.edit { putString(property.name, value) }
}
}
override var chosenDevice by PrefDelegate()
override var infUrl by PrefDelegate()
override var infOrg by PrefDelegate()
override var infBucket by PrefDelegate()
override var infToken by PrefDelegate()
override fun createInflux() = InfluxDBClientKotlinFactory
.create(infUrl ?: "", (infToken ?: "").toCharArray(), infOrg ?: "", infBucket ?: "")
}
inline fun <reified T : Activity> Context.act() = startActivity(Intent(this, T::class.java))
@@ -8,6 +8,8 @@ import android.view.Menu
import android.view.MenuItem
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.influxdb.client.domain.WritePrecision
import com.influxdb.client.kotlin.InfluxDBClientKotlinFactory
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.consumeAsFlow
import org.hydev.wearsync.ActivityPermissions.Companion.hasPermissions
@@ -23,15 +25,9 @@ class MainActivity : AppCompatActivity()
{
lateinit var binding: ActivityMainBinding
val enableBluetoothRequest =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == RESULT_OK) {
// Bluetooth has been enabled
} else {
// Bluetooth has not been enabled, try again
ensureBluetooth()
}
}
val enableBluetoothRequest = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode != RESULT_OK) ensureBluetooth()
}
fun ensureBluetooth() {
if (blueMan().adapter?.isEnabled == false)
@@ -73,7 +69,10 @@ class MainActivity : AppCompatActivity()
{
return when (item.itemId)
{
R.id.action_settings -> true
R.id.action_settings -> {
act<ActivitySettings>()
true
}
R.id.action_scan -> {
act<ActivityScan>()
true