[O] Preference by delegate
This commit is contained in:
+4
-1
@@ -42,6 +42,10 @@ dependencies {
|
|||||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||||
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
|
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
|
||||||
implementation 'androidx.navigation:navigation-ui-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
|
// Logger
|
||||||
implementation 'com.jakewharton.timber:timber:5.0.1'
|
implementation 'com.jakewharton.timber:timber:5.0.1'
|
||||||
@@ -51,7 +55,6 @@ dependencies {
|
|||||||
|
|
||||||
// Database library
|
// Database library
|
||||||
implementation "com.influxdb:influxdb-client-kotlin:6.3.0"
|
implementation "com.influxdb:influxdb-client-kotlin:6.3.0"
|
||||||
implementation 'androidx.preference:preference:1.2.0'
|
|
||||||
|
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ class ActivityScan : AppCompatActivity() {
|
|||||||
println("OnCreate called, Initializing...")
|
println("OnCreate called, Initializing...")
|
||||||
|
|
||||||
// Device exists
|
// Device exists
|
||||||
chosenDevice?.let { addr ->
|
prefs.chosenDevice?.let { addr ->
|
||||||
ble.connectAddress(addr) { connected(addr) }
|
ble.connectAddress(addr) { connected(addr) }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -86,7 +86,7 @@ class ActivityScan : AppCompatActivity() {
|
|||||||
fun connected(address: String)
|
fun connected(address: String)
|
||||||
{
|
{
|
||||||
view.snack("✅ Connected.")
|
view.snack("✅ Connected.")
|
||||||
chosenDevice = address
|
prefs.chosenDevice = address
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,12 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
import com.google.android.material.snackbar.Snackbar
|
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)
|
fun View.snack(msg: String) = Snackbar.make(this, msg, Snackbar.LENGTH_LONG)
|
||||||
.setAction("Action", null).show()
|
.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
|
inline fun <reified T> Context.getSysServ() = getSystemService(T::class.java) as T
|
||||||
fun Context.blueMan() = getSysServ<BluetoothManager>()
|
fun Context.blueMan() = getSysServ<BluetoothManager>()
|
||||||
|
|
||||||
val Context.pref get() = getSharedPreferences("settings", Context.MODE_PRIVATE)
|
|
||||||
var Context.chosenDevice: String?
|
|
||||||
get() = pref.getString("mac", null)
|
interface Prefs {
|
||||||
set(value) = pref.edit { putString("mac", value) }
|
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))
|
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 android.view.MenuItem
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import com.influxdb.client.domain.WritePrecision
|
||||||
|
import com.influxdb.client.kotlin.InfluxDBClientKotlinFactory
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.flow.consumeAsFlow
|
import kotlinx.coroutines.flow.consumeAsFlow
|
||||||
import org.hydev.wearsync.ActivityPermissions.Companion.hasPermissions
|
import org.hydev.wearsync.ActivityPermissions.Companion.hasPermissions
|
||||||
@@ -23,15 +25,9 @@ class MainActivity : AppCompatActivity()
|
|||||||
{
|
{
|
||||||
lateinit var binding: ActivityMainBinding
|
lateinit var binding: ActivityMainBinding
|
||||||
|
|
||||||
val enableBluetoothRequest =
|
val enableBluetoothRequest = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
||||||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
if (it.resultCode != RESULT_OK) ensureBluetooth()
|
||||||
if (it.resultCode == RESULT_OK) {
|
}
|
||||||
// Bluetooth has been enabled
|
|
||||||
} else {
|
|
||||||
// Bluetooth has not been enabled, try again
|
|
||||||
ensureBluetooth()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ensureBluetooth() {
|
fun ensureBluetooth() {
|
||||||
if (blueMan().adapter?.isEnabled == false)
|
if (blueMan().adapter?.isEnabled == false)
|
||||||
@@ -73,7 +69,10 @@ class MainActivity : AppCompatActivity()
|
|||||||
{
|
{
|
||||||
return when (item.itemId)
|
return when (item.itemId)
|
||||||
{
|
{
|
||||||
R.id.action_settings -> true
|
R.id.action_settings -> {
|
||||||
|
act<ActivitySettings>()
|
||||||
|
true
|
||||||
|
}
|
||||||
R.id.action_scan -> {
|
R.id.action_scan -> {
|
||||||
act<ActivityScan>()
|
act<ActivityScan>()
|
||||||
true
|
true
|
||||||
|
|||||||
Reference in New Issue
Block a user