Skip to content

Commit

Permalink
Merge pull request #244 from enaboapps/iss243
Browse files Browse the repository at this point in the history
Store scan method and mode as strings
  • Loading branch information
enaboapps authored Mar 21, 2024
2 parents 0dc7d25 + 91e60da commit b0b9bf0
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ class PreferenceManager(context: Context) {
}
}

fun setStringValue(key: String, value: String) {
with(sharedPreferences.edit()) {
putString(key, value)
apply()
}
}

fun getFloatValue(key: String, defaultValue: Float = 0f): Float {
return sharedPreferences.getFloat(key, defaultValue)
}
Expand All @@ -89,4 +96,13 @@ class PreferenceManager(context: Context) {
}
}

fun getStringValue(key: String, defaultValue: String = ""): String {
// Due to an old version of the app storing some values as different types, we need to do try/catch
return try {
sharedPreferences.getString(key, defaultValue) ?: defaultValue
} catch (e: ClassCastException) {
defaultValue
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun SettingsScreen(navController: NavController) {
val context = LocalContext.current
val settingsScreenModel = SettingsScreenModel(context)
val mode =
ScanMode.fromId(PreferenceManager(context).getIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_MODE))
ScanMode.fromId(PreferenceManager(context).getStringValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_MODE))
Scaffold(
topBar = {
NavBar(title = "Settings", navController = navController)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import com.enaboapps.switchify.widgets.NavBar
fun ScanMethodSelectionScreen(navController: NavController) {
val methods = listOf(ScanMethod.MethodType.CURSOR, ScanMethod.MethodType.ITEM_SCAN)
val preferenceManager = PreferenceManager(LocalContext.current)
val currentMethod = MutableLiveData<Int>()
val currentMethod = MutableLiveData<String>()
currentMethod.value =
preferenceManager.getIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_METHOD)
preferenceManager.getStringValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_METHOD)
val currentMethodState = currentMethod.observeAsState()
val setScanMethod = { method: Int ->
preferenceManager.setIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_METHOD, method)
val setScanMethod = { method: String ->
preferenceManager.setStringValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_METHOD, method)
currentMethod.value = method
}
Scaffold(
Expand Down Expand Up @@ -66,7 +66,7 @@ fun ScanMethodSelectionScreen(navController: NavController) {
}

@Composable
fun ScanMethodInfo(method: Int) {
fun ScanMethodInfo(method: String) {
Column(
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ fun ScanModeSelectionScreen(navController: NavController) {
val modes = ScanMode.modes
val preferenceManager = PreferenceManager(LocalContext.current)
val currentMode = MutableLiveData<ScanMode>()
currentMode.value = ScanMode.fromId(preferenceManager.getIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_MODE))
currentMode.value =
ScanMode.fromId(preferenceManager.getStringValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_MODE))
val currentModeState = currentMode.observeAsState()
val setScanMode = { mode: ScanMode ->
preferenceManager.setIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_MODE, mode.id)
preferenceManager.setStringValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_MODE, mode.id)
currentMode.value = mode
}
Scaffold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MenuManager {
/**
* The scan method to revert to when the menu is closed
*/
var scanMethodToRevertTo: Int = ScanMethod.MethodType.CURSOR
var scanMethodToRevertTo: String = ScanMethod.MethodType.CURSOR

/**
* The menu hierarchy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.enaboapps.switchify.preferences.PreferenceManager
* This interface is used to observe the changes in the scanning method
*/
interface ScanMethodObserver {
fun onScanMethodChanged(scanMethod: Int)
fun onScanMethodChanged(scanMethod: String)
}

/**
Expand All @@ -32,21 +32,21 @@ object ScanMethod {
/**
* This type represents the cursor
*/
const val CURSOR = 0
const val CURSOR = "cursor"

/**
* This type represents the item scan
* Sequentially scanning the items on the screen
*/
const val ITEM_SCAN = 1
const val ITEM_SCAN = "item_scan"
}

/**
* This function is used to get the type of the scanning method
*/
fun getType(): Int {
fun getType(): String {
preferenceManager?.let { preferenceManager ->
val storedType = preferenceManager.getIntegerValue(
val storedType = preferenceManager.getStringValue(
PreferenceManager.PREFERENCE_KEY_SCAN_METHOD
)
println("Stored type: $storedType")
Expand All @@ -64,7 +64,7 @@ object ScanMethod {
* @param type The type of the scanning method
* @return The name of the scanning method
*/
fun getName(type: Int): String {
fun getName(type: String): String {
return when (type) {
MethodType.CURSOR -> "Cursor"
MethodType.ITEM_SCAN -> "Item Scan"
Expand All @@ -77,7 +77,7 @@ object ScanMethod {
* @param type The type of the scanning method
* @return The description of the scanning method
*/
fun getDescription(type: Int): String {
fun getDescription(type: String): String {
return when (type) {
MethodType.CURSOR -> "Cursor allows you to select items by moving a set of crosshairs over the screen."
MethodType.ITEM_SCAN -> "Item Scan allows you to select items by scanning through them sequentially."
Expand All @@ -88,8 +88,8 @@ object ScanMethod {
/**
* This function is used to set the type of the scanning method
*/
fun setType(value: Int) {
preferenceManager?.setIntegerValue(
fun setType(value: String) {
preferenceManager?.setStringValue(
PreferenceManager.PREFERENCE_KEY_SCAN_METHOD,
value
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,35 @@ package com.enaboapps.switchify.service.scanning

/**
* This class represents a scanning mode.
* @property id The unique identifier of the scanning mode.
* @property id The id of the scanning mode.
*/
class ScanMode(val id: Int) {
class ScanMode(val id: String) {

/**
* This object holds the constants for the different scanning modes.
* This object represents the different modes available.
*/
object Modes {
const val MODE_AUTO = 0
const val MODE_MANUAL = 1
const val MODE_AUTO = "auto"
const val MODE_MANUAL = "manual"
}

/**
* This companion object holds an array of the different scanning modes and a function to get a scanning mode from its id.
*/
companion object {
// An array of the different scanning modes.
val modes = arrayOf(
/**
* This function returns the scanning modes.
* @return The scanning modes.
*/
val modes = listOf(
ScanMode(Modes.MODE_AUTO),
ScanMode(Modes.MODE_MANUAL)
)

/**
* This function returns a scanning mode from its id.
* This function returns the scanning mode with the given id.
* @param id The id of the scanning mode.
* @return The scanning mode with the given id.
*/
fun fromId(id: Int): ScanMode {
return when (id) {
Modes.MODE_AUTO -> ScanMode(Modes.MODE_AUTO)
Modes.MODE_MANUAL -> ScanMode(Modes.MODE_MANUAL)
else -> ScanMode(Modes.MODE_AUTO)
}
fun fromId(id: String): ScanMode {
return modes.firstOrNull { it.id == id } ?: modes[0]
}
}

Expand Down Expand Up @@ -79,6 +75,6 @@ class ScanMode(val id: Int) {
* @return The hash code of this scanning mode.
*/
override fun hashCode(): Int {
return id
return id.hashCode()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class ScanSettings(context: Context) {
* @return true if the scan mode is auto, false otherwise
*/
fun isAutoScanMode(): Boolean {
return ScanMode.fromId(preferenceManager.getIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_MODE)).id == ScanMode.Modes.MODE_AUTO
return ScanMode.fromId(preferenceManager.getStringValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_MODE)).id == ScanMode.Modes.MODE_AUTO
}

/**
* Check if the scan mode is manual
* @return true if the scan mode is manual, false otherwise
*/
fun isManualScanMode(): Boolean {
return ScanMode.fromId(preferenceManager.getIntegerValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_MODE)).id == ScanMode.Modes.MODE_MANUAL
return ScanMode.fromId(preferenceManager.getStringValue(PreferenceManager.Keys.PREFERENCE_KEY_SCAN_MODE)).id == ScanMode.Modes.MODE_MANUAL
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class ScanningManager(
*
* @param scanMethod the new scanning method.
*/
override fun onScanMethodChanged(scanMethod: Int) {
override fun onScanMethodChanged(scanMethod: String) {
when (scanMethod) {
ScanMethod.MethodType.CURSOR -> {
nodeScanner.cleanup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object KeyboardInfo {
var keyboardHeight = 0

// Track last scan type to go back to it after keyboard is dismissed
private var lastScanType: Int = ScanMethod.getType()
private var lastScanType: String = ScanMethod.getType()

// Track last update time to prevent multiple updates in a short time
private var lastUpdateTime: Long = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class SwitchEventStore(private val context: Context) {
fun isConfigInvalid(): String? {
val preferenceManager = PreferenceManager(context)
val mode =
ScanMode(preferenceManager.getIntegerValue(PreferenceManager.PREFERENCE_KEY_SCAN_MODE))
ScanMode(preferenceManager.getStringValue(PreferenceManager.PREFERENCE_KEY_SCAN_MODE))
val containsSelect =
switchEvents.any { it.containsAction(SwitchAction.Actions.ACTION_SELECT) }
val containsNext =
Expand Down

0 comments on commit b0b9bf0

Please sign in to comment.