Skip to content

Commit

Permalink
use enums instead of constants for theme and api key state
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jul 22, 2023
1 parent d4bbf90 commit f7e8eb9
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 19 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/com/bnyro/translate/const/ApiKeyState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package com.bnyro.translate.const

object ApiKeyState {
const val DISABLED = 0
const val OPTIONAL = 1
const val REQUIRED = 2
enum class ApiKeyState {
DISABLED,
OPTIONAL,
REQUIRED
}
8 changes: 4 additions & 4 deletions app/src/main/java/com/bnyro/translate/const/ThemeMode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package com.bnyro.translate.const

object ThemeMode {
const val AUTO = 0
const val LIGHT = 1
const val DARK = 2
enum class ThemeMode(val value: Int) {
AUTO(0),
LIGHT(1),
DARK(2)
}
4 changes: 1 addition & 3 deletions app/src/main/java/com/bnyro/translate/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ import kotlinx.serialization.encodeToString

class MainActivity : ComponentActivity() {
private lateinit var mainModel: TranslationModel
var themeMode by mutableStateOf(
Preferences.getThemeMode()
)
var themeMode by mutableStateOf(Preferences.getThemeMode())
var accentColor by mutableStateOf(Preferences.getAccentColor())

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ fun ThemeModeDialog(
options = listOf(
ListPreferenceOption(
name = stringResource(R.string.theme_auto),
value = ThemeMode.AUTO,
value = ThemeMode.AUTO.value,
isSelected = activity.themeMode == ThemeMode.AUTO
),
ListPreferenceOption(
name = stringResource(R.string.theme_light),
value = ThemeMode.LIGHT,
value = ThemeMode.LIGHT.value,
isSelected = activity.themeMode == ThemeMode.LIGHT
),
ListPreferenceOption(
name = stringResource(R.string.theme_dark),
value = ThemeMode.DARK,
value = ThemeMode.DARK.value,
isSelected = activity.themeMode == ThemeMode.DARK
)
),
onOptionSelected = {
activity.themeMode = it.value
activity.themeMode = ThemeMode.values()[it.value]
}
)
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/translate/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const val defaultAccentColor = "d0bcff"

@Composable
fun TranslateYouTheme(
themeMode: Int = ThemeMode.AUTO,
themeMode: ThemeMode = ThemeMode.AUTO,
accentColor: Color? = null,
content: @Composable () -> Unit
) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/bnyro/translate/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object Preferences {
const val charCounterLimitKey = "charCountLimit"
const val tessLanguageKey = "tessLanguage"

const val themeModeKey = "themeMode"
const val themeModeKey = "themeModeKey"
const val accentColorKey = "accentColor"
const val sourceLanguage = "sourceLanguage"
const val targetLanguage = "targetLanguage"
Expand Down Expand Up @@ -71,7 +71,7 @@ object Preferences {
}
}

fun getThemeMode() = get(themeModeKey, ThemeMode.AUTO.toString()).toInt()
fun getThemeMode() = ThemeMode.values()[get(themeModeKey, ThemeMode.AUTO.value.toString()).toInt()]

fun getAccentColor() = prefs.getString(accentColorKey, null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.bnyro.translate.util

import com.bnyro.translate.const.ApiKeyState
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.obj.Translation
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
Expand All @@ -25,7 +26,7 @@ abstract class TranslationEngine(
val name: String,
val defaultUrl: String,
val urlModifiable: Boolean,
val apiKeyState: Int,
val apiKeyState: ApiKeyState,
val autoLanguageCode: String?,
var supportsSimTranslation: Boolean = true
) {
Expand Down

0 comments on commit f7e8eb9

Please sign in to comment.