mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor: consolidate extensions
This commit is contained in:
parent
f17f9e1ec5
commit
5a28649a08
7 changed files with 66 additions and 76 deletions
|
|
@ -32,6 +32,7 @@ import com.geeksville.mesh.model.Channel
|
|||
import com.geeksville.mesh.model.ChannelOption
|
||||
import com.geeksville.mesh.model.ChannelSet
|
||||
import com.geeksville.mesh.model.UIViewModel
|
||||
import com.geeksville.mesh.util.onEditorAction
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import com.google.protobuf.ByteString
|
||||
|
|
@ -234,7 +235,7 @@ class ChannelFragment : ScreenFragment("Channel"), Logging {
|
|||
.show()
|
||||
}
|
||||
|
||||
binding.channelNameEdit.on(EditorInfo.IME_ACTION_DONE) {
|
||||
binding.channelNameEdit.onEditorAction(EditorInfo.IME_ACTION_DONE) {
|
||||
requireActivity().hideKeyboard()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,23 +26,13 @@ import com.geeksville.mesh.database.entity.QuickChatAction
|
|||
import com.geeksville.mesh.databinding.AdapterMessageLayoutBinding
|
||||
import com.geeksville.mesh.databinding.MessagesFragmentBinding
|
||||
import com.geeksville.mesh.model.UIViewModel
|
||||
import com.geeksville.mesh.util.onEditorAction
|
||||
import com.google.android.material.chip.Chip
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import java.text.DateFormat
|
||||
import java.util.*
|
||||
|
||||
// Allows usage like email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })
|
||||
fun EditText.on(actionId: Int, func: () -> Unit) {
|
||||
setOnEditorActionListener { _, receivedActionId, _ ->
|
||||
|
||||
if (actionId == receivedActionId) {
|
||||
func()
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
@AndroidEntryPoint
|
||||
class MessagesFragment : Fragment(), Logging {
|
||||
|
||||
|
|
@ -59,17 +49,6 @@ class MessagesFragment : Fragment(), Logging {
|
|||
|
||||
private var isConnected = false
|
||||
|
||||
// Allows textMultiline with IME_ACTION_SEND
|
||||
private fun EditText.onActionSend(func: () -> Unit) {
|
||||
setOnEditorActionListener { _, actionId, _ ->
|
||||
|
||||
if (actionId == EditorInfo.IME_ACTION_SEND) {
|
||||
func()
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
// Provide a direct reference to each of the views within a data item
|
||||
// Used to cache the views within the item layout for fast access
|
||||
class ViewHolder(itemView: AdapterMessageLayoutBinding) :
|
||||
|
|
@ -276,8 +255,8 @@ class MessagesFragment : Fragment(), Logging {
|
|||
// requireActivity().hideKeyboard()
|
||||
}
|
||||
|
||||
binding.messageInputText.onActionSend {
|
||||
debug("did IME action")
|
||||
binding.messageInputText.onEditorAction(EditorInfo.IME_ACTION_SEND) {
|
||||
debug("received IME_ACTION_SEND")
|
||||
|
||||
val str = binding.messageInputText.text.toString().trim()
|
||||
if (str.isNotEmpty()) model.sendMessage(str, contactKey)
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ import com.geeksville.mesh.repository.usb.UsbRepository
|
|||
import com.geeksville.mesh.service.MeshService
|
||||
import com.geeksville.mesh.service.SoftwareUpdateService
|
||||
import com.geeksville.mesh.util.exceptionToSnackbar
|
||||
import com.geeksville.mesh.util.onEditorAction
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
|
@ -369,11 +370,10 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
|
|||
.show()
|
||||
}
|
||||
|
||||
binding.usernameEditText.on(EditorInfo.IME_ACTION_DONE) {
|
||||
debug("did IME action")
|
||||
binding.usernameEditText.onEditorAction(EditorInfo.IME_ACTION_DONE) {
|
||||
debug("received IME_ACTION_DONE")
|
||||
val n = binding.usernameEditText.text.toString().trim()
|
||||
if (n.isNotEmpty())
|
||||
model.setOwner(n)
|
||||
if (n.isNotEmpty()) model.setOwner(n)
|
||||
requireActivity().hideKeyboard()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,27 +0,0 @@
|
|||
package com.geeksville.mesh.util
|
||||
|
||||
import com.geeksville.mesh.BuildConfig
|
||||
|
||||
/// A toString that makes sure all newlines are removed (for nice logging).
|
||||
fun Any.toOneLineString() = this.toString().replace('\n', ' ')
|
||||
|
||||
/// Return a one line string version of an object (but if a release build, just say 'might be PII)
|
||||
fun Any.toPIIString() =
|
||||
if (!BuildConfig.DEBUG)
|
||||
"<PII?>"
|
||||
else
|
||||
this.toOneLineString()
|
||||
|
||||
fun formatAgo(lastSeenUnix: Int): String {
|
||||
val currentTime = (System.currentTimeMillis() / 1000).toInt()
|
||||
val diffMin = (currentTime - lastSeenUnix) / 60;
|
||||
if (diffMin < 1)
|
||||
return "now";
|
||||
if (diffMin < 100)
|
||||
return diffMin.toString() + "m"
|
||||
if (diffMin < 6000)
|
||||
return (diffMin / 60).toString() + "h"
|
||||
if (diffMin < 144000)
|
||||
return (diffMin / (60 * 24)).toString() + "d";
|
||||
return "?";
|
||||
}
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
package com.geeksville.mesh.util
|
||||
|
||||
fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }
|
||||
57
app/src/main/java/com/geeksville/mesh/util/Extensions.kt
Normal file
57
app/src/main/java/com/geeksville/mesh/util/Extensions.kt
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package com.geeksville.mesh.util
|
||||
|
||||
import android.widget.EditText
|
||||
import com.geeksville.mesh.BuildConfig
|
||||
|
||||
/**
|
||||
* When printing strings to logs sometimes we want to print useful debugging information about users
|
||||
* or positions. But we don't want to leak things like usernames or locations. So this function
|
||||
* if given a string, will return a string which is a maximum of three characters long, taken from the tail
|
||||
* of the string. Which should effectively hide real usernames and locations,
|
||||
* but still let us see if values were zero, empty or different.
|
||||
*/
|
||||
val Any?.anonymize: String
|
||||
get() = this.anonymize()
|
||||
|
||||
/**
|
||||
* A version of anonymize that allows passing in a custom minimum length
|
||||
*/
|
||||
fun Any?.anonymize(maxLen: Int = 3) =
|
||||
if (this != null) ("..." + this.toString().takeLast(maxLen)) else "null"
|
||||
|
||||
/// A toString that makes sure all newlines are removed (for nice logging).
|
||||
fun Any.toOneLineString() = this.toString().replace('\n', ' ')
|
||||
|
||||
/// Return a one line string version of an object (but if a release build, just say 'might be PII)
|
||||
fun Any.toPIIString() =
|
||||
if (!BuildConfig.DEBUG)
|
||||
"<PII?>"
|
||||
else
|
||||
this.toOneLineString()
|
||||
|
||||
fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }
|
||||
|
||||
fun formatAgo(lastSeenUnix: Int): String {
|
||||
val currentTime = (System.currentTimeMillis() / 1000).toInt()
|
||||
val diffMin = (currentTime - lastSeenUnix) / 60
|
||||
if (diffMin < 1)
|
||||
return "now"
|
||||
if (diffMin < 100)
|
||||
return diffMin.toString() + "m"
|
||||
if (diffMin < 6000)
|
||||
return (diffMin / 60).toString() + "h"
|
||||
if (diffMin < 144000)
|
||||
return (diffMin / (60 * 24)).toString() + "d"
|
||||
return "?"
|
||||
}
|
||||
|
||||
/// Allows usage like email.onEditorAction(EditorInfo.IME_ACTION_NEXT, { confirm() })
|
||||
fun EditText.onEditorAction(actionId: Int, func: () -> Unit) {
|
||||
setOnEditorActionListener { _, receivedActionId, _ ->
|
||||
|
||||
if (actionId == receivedActionId) {
|
||||
func()
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package com.geeksville.mesh.util
|
||||
|
||||
/**
|
||||
* When printing strings to logs sometimes we want to print useful debugging information about users
|
||||
* or positions. But we don't want to leak things like usernames or locations. So this function
|
||||
* if given a string, will return a string which is a maximum of three characters long, taken from the tail
|
||||
* of the string. Which should effectively hide real usernames and locations,
|
||||
* but still let us see if values were zero, empty or different.
|
||||
*/
|
||||
val Any?.anonymize: String
|
||||
get() = this.anonymize()
|
||||
|
||||
/**
|
||||
* A version of anonymize that allows passing in a custom minimum length
|
||||
*/
|
||||
fun Any?.anonymize(maxLen: Int = 3) =
|
||||
if (this != null) ("..." + this.toString().takeLast(maxLen)) else "null"
|
||||
Loading…
Add table
Add a link
Reference in a new issue