Skip to content

Commit

Permalink
Disallow new-lines in device labels
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Sep 13, 2024
1 parent c102cdd commit d136892
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package eu.darken.octi.common.preferences

import android.content.Context
import android.text.InputFilter
import android.util.AttributeSet
import android.view.inputmethod.EditorInfo
import androidx.preference.EditTextPreference

class CleanInputEditTextPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = androidx.preference.R.attr.editTextPreferenceStyle
) : EditTextPreference(context, attrs, defStyleAttr) {

init {
setOnBindEditTextListener { editText ->
editText.apply {
isSingleLine = true
maxLines = 1
imeOptions = EditorInfo.IME_ACTION_DONE
filters = arrayOf(
InputFilter { source, start, end, dest, dstart, dend ->
for (i in start until end) {
if (source[i] == '\n' || source[i] == '\r') {
return@InputFilter ""
}
}
null
}
)
}
}
}

override fun persistString(value: String?): Boolean {
val trimmedValue = value?.trim()
return super.persistString(trimmedValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.preference.Preference
import dagger.hilt.android.AndroidEntryPoint
import eu.darken.octi.R
import eu.darken.octi.common.uix.PreferenceFragment3
import eu.darken.octi.sync.core.SyncSettings
import javax.inject.Inject

@Keep
Expand All @@ -17,8 +18,8 @@ class SyncSettingsFragment : PreferenceFragment3() {

override val vm: SyncSettingsVM by viewModels()

@Inject lateinit var _syncSettings: eu.darken.octi.sync.core.SyncSettings
override val settings: eu.darken.octi.sync.core.SyncSettings by lazy { _syncSettings }
@Inject lateinit var _syncSettings: SyncSettings
override val settings: SyncSettings by lazy { _syncSettings }
override val preferenceFile: Int = R.xml.preferences_sync

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/preferences_sync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<EditTextPreference
<eu.darken.octi.common.preferences.CleanInputEditTextPreference
android:dialogMessage="@string/sync_setting_devicelabel_hint"
android:icon="@drawable/ic_changelog_onsurface"
android:key="sync.device.self.label"
Expand Down

0 comments on commit d136892

Please sign in to comment.