From 91cf6b72538117a3309b9950d8453c76a346cbf3 Mon Sep 17 00:00:00 2001 From: geeksville Date: Tue, 18 Feb 2020 10:40:02 -0800 Subject: [PATCH] begin letting users edit ownername --- .../java/com/geeksville/mesh/MainActivity.kt | 27 +++++++--------- .../java/com/geeksville/mesh/model/UIState.kt | 20 +++++++++++- .../com/geeksville/mesh/ui/BTScanScreen.kt | 4 ++- .../java/com/geeksville/mesh/ui/MeshApp.kt | 5 ++- .../java/com/geeksville/mesh/ui/Settings.kt | 32 +++++++++++++++++++ 5 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/com/geeksville/mesh/ui/Settings.kt diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index 382836f18..6cb1f5991 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -23,11 +23,9 @@ import com.geeksville.mesh.model.UIState import com.geeksville.mesh.service.* import com.geeksville.mesh.ui.MeshApp import com.geeksville.mesh.ui.ScanState -import com.geeksville.mesh.ui.getInitials import com.geeksville.util.exceptionReporter import com.google.android.gms.auth.api.signin.GoogleSignIn import com.google.android.gms.auth.api.signin.GoogleSignInAccount -import com.google.android.gms.common.api.ApiException import com.google.android.gms.tasks.Task import java.nio.charset.Charset @@ -153,12 +151,6 @@ class MainActivity : AppCompatActivity(), Logging, } - private fun setOwner() { - // Note: we are careful to not set a new unique ID - val name = UIState.ownerName.value - UIState.meshService!!.setOwner(null, name, getInitials(name)) - } - private fun sendTestPackets() { exceptionReporter { val m = UIState.meshService!! @@ -182,9 +174,8 @@ class MainActivity : AppCompatActivity(), Logging, override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContent { - MeshApp() - } + val prefs = getSharedPreferences("ui-prefs", Context.MODE_PRIVATE) + UIState.ownerName = prefs.getString("owner", "Unknown Owner")!! // Ensures Bluetooth is available on the device and it is enabled. If not, // displays a dialog requesting user permission to enable Bluetooth. @@ -200,6 +191,10 @@ class MainActivity : AppCompatActivity(), Logging, requestPermission() + setContent { + MeshApp() + } + /* not yet working // Configure sign-in to request the user's ID, email address, and basic // profile. ID and basic profile are included in DEFAULT_SIGN_IN. @@ -229,7 +224,7 @@ class MainActivity : AppCompatActivity(), Logging, // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...); // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...); - if (requestCode === RC_SIGN_IN) { + if (requestCode == RC_SIGN_IN) { // The Task returned from this call is always completed, no need to attach // a listener. val task: Task = @@ -239,16 +234,16 @@ class MainActivity : AppCompatActivity(), Logging, } private fun handleSignInResult(completedTask: Task) { + /* try { - val account = - completedTask.getResult(ApiException::class.java) + val account = completedTask.getResult(ApiException::class.java) // Signed in successfully, show authenticated UI. //updateUI(account) } catch (e: ApiException) { // The ApiException status code indicates the detailed failure reason. // Please refer to the GoogleSignInStatusCodes class reference for more information. warn("signInResult:failed code=" + e.statusCode) //updateUI(null) - } + } */ } private var receiverRegistered = false @@ -289,7 +284,7 @@ class MainActivity : AppCompatActivity(), Logging, readRadioConfig() // everytime the radio reconnects, we slam in our current owner data, the radio is smart enough to only broadcast if needed - setOwner() + UIState.setOwner(this) } } diff --git a/app/src/main/java/com/geeksville/mesh/model/UIState.kt b/app/src/main/java/com/geeksville/mesh/model/UIState.kt index a28f327c2..8d6766821 100644 --- a/app/src/main/java/com/geeksville/mesh/model/UIState.kt +++ b/app/src/main/java/com/geeksville/mesh/model/UIState.kt @@ -1,9 +1,12 @@ package com.geeksville.mesh.model +import android.content.Context import android.util.Base64 import androidx.compose.mutableStateOf +import androidx.core.content.edit import com.geeksville.mesh.IMeshService import com.geeksville.mesh.MeshProtos +import com.geeksville.mesh.ui.getInitials /// FIXME - figure out how to merge this staate with the AppStatus Model object UIState { @@ -21,7 +24,7 @@ object UIState { /// our name in hte radio /// Note, we generate owner initials automatically for now - val ownerName = mutableStateOf("fixme readfromprefs") + var ownerName: String = "fixme readfromprefs" /// Return an URL that represents the current channel values val channelUrl @@ -31,4 +34,19 @@ object UIState { return "https://www.meshtastic.org/c/$enc" } + + // clean up all this nasty owner state management FIXME + fun setOwner(context: Context, s: String? = null) { + + if (s != null) { + ownerName = s + val prefs = context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE) + prefs.edit(commit = true) { + putString("owner", s) + } + } + + // Note: we are careful to not set a new unique ID + meshService!!.setOwner(null, ownerName, getInitials(ownerName)) + } } diff --git a/app/src/main/java/com/geeksville/mesh/ui/BTScanScreen.kt b/app/src/main/java/com/geeksville/mesh/ui/BTScanScreen.kt index b596ee67e..bca938f8b 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/BTScanScreen.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/BTScanScreen.kt @@ -42,6 +42,7 @@ object ScanState : Logging { if (callback != null) { debug("stopping scan") scanner!!.stopScan(callback) + callback = null } else debug("not stopping bt scanner") } @@ -59,7 +60,7 @@ fun BTScanScreen() { /// Note: may be null on platforms without a bluetooth driver (ie. the emulator) val bluetoothAdapter = - (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager)?.adapter + (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter onActive { ScanUIState.selectedMacAddr = RadioInterfaceService.getBondedDeviceAddress(context) @@ -67,6 +68,7 @@ fun BTScanScreen() { val scanCallback = object : ScanCallback() { override fun onScanFailed(errorCode: Int) { val msg = "Unexpected bluetooth scan failure: $errorCode" + // error code2 seeems to be indicate hung bluetooth stack ScanUIState.errorText = msg ScanState.reportError(msg) } diff --git a/app/src/main/java/com/geeksville/mesh/ui/MeshApp.kt b/app/src/main/java/com/geeksville/mesh/ui/MeshApp.kt index f53f7d24e..dfe0b5b3a 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/MeshApp.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/MeshApp.kt @@ -130,9 +130,12 @@ private fun AppContent(openDrawer: () -> Unit) { // VerticalScroller breaks flexible layouts - because verticalscrollers have 'infinite' height // VerticalScroller(modifier = LayoutFlexible(1f)) { + //if (screen != Screen.settings) + // ScanState.stopScan() // Nasty hack to teardown the bt scanner + when (screen) { Screen.messages -> MessagesContent() - Screen.settings -> BTScanScreen() + Screen.settings -> SettingsContent() Screen.users -> HomeContent() Screen.channel -> ChannelContent() else -> TODO() diff --git a/app/src/main/java/com/geeksville/mesh/ui/Settings.kt b/app/src/main/java/com/geeksville/mesh/ui/Settings.kt new file mode 100644 index 000000000..804a607b6 --- /dev/null +++ b/app/src/main/java/com/geeksville/mesh/ui/Settings.kt @@ -0,0 +1,32 @@ +package com.geeksville.mesh.ui + +import androidx.compose.Composable +import androidx.ui.layout.Column +import androidx.ui.layout.LayoutPadding +import androidx.ui.layout.LayoutSize +import androidx.ui.material.MaterialTheme +import androidx.ui.tooling.preview.Preview +import androidx.ui.unit.dp +import com.geeksville.android.Logging + + +object SettingsLog : Logging + +@Composable +fun SettingsContent() { + val typography = MaterialTheme.typography() + + Column(modifier = LayoutSize.Fill + LayoutPadding(16.dp)) { + + } +} + + +@Preview +@Composable +fun previewSettings() { + // another bug? It seems modaldrawerlayout not yet supported in preview + MaterialTheme(colors = palette) { + SettingsContent() + } +}