Skip to content

Commit

Permalink
Merge pull request #1 from Shingyx/mac-address-dialog-colons
Browse files Browse the repository at this point in the history
Auto insert MAC addr colons in dialog
  • Loading branch information
Shingyx authored Mar 31, 2024
2 parents 4306a11 + ea8c13d commit d11eecb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.shingyx.wakeonlan.ui

import android.text.Editable
import android.text.TextWatcher

class MacAddressTextWatcher : TextWatcher {
private var previousText: String = ""

override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
previousText = s.toString()
}

override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}

override fun afterTextChanged(editable: Editable) {
val text = editable.toString()
val formattedText = formatMacAddress(text)
if (formattedText != text) {
editable.replace(0, editable.length, formattedText)
}
}

private fun formatMacAddress(text: String): String {
val builder = StringBuilder()
var digits = 0
for (char in text) {
if (char.isLetterOrDigit()) {
if (digits % 2 == 0 && digits > 0 && digits < 12) {
builder.append(':')
}
builder.append(char)
digits++
}
}
if (text.lastOrNull() == ':') {
builder.append(':')
}
return builder.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ import com.github.shingyx.wakeonlan.data.Host
import com.github.shingyx.wakeonlan.data.WifiAddresses
import com.github.shingyx.wakeonlan.data.isMacAddressValid
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.configure
import kotlinx.android.synthetic.main.activity_main.mac_address
import kotlinx.android.synthetic.main.activity_main.name
import kotlinx.android.synthetic.main.activity_main.progress
import kotlinx.android.synthetic.main.activity_main.ssid
import kotlinx.android.synthetic.main.activity_main.turn_on
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import java.lang.Exception

class MainActivity : AppCompatActivity() {
private val scope = MainScope()
Expand Down Expand Up @@ -91,7 +95,9 @@ class MainActivity : AppCompatActivity() {

val view = layoutInflater.inflate(R.layout.dialog_host, null)
val nameField = view.findViewById<EditText>(R.id.edit_name)
val macAddressField = view.findViewById<EditText>(R.id.edit_mac_address)
val macAddressField = view.findViewById<EditText>(R.id.edit_mac_address).apply {
addTextChangedListener(MacAddressTextWatcher())
}
model.host.value?.let {
nameField.setText(it.name)
macAddressField.setText(it.macAddress)
Expand Down

0 comments on commit d11eecb

Please sign in to comment.