feat: adopt Kotlin 1.9 Enum entries feature

This commit is contained in:
Harsh Shandilya
2023-06-15 16:39:24 +05:30
parent 5dac84c3c8
commit e875047899
6 changed files with 13 additions and 18 deletions

View File

@@ -33,6 +33,7 @@ import logcat.LogPriority.VERBOSE
import logcat.LogcatLogger import logcat.LogcatLogger
import logcat.logcat import logcat.logcat
@OptIn(ExperimentalStdlibApi::class)
@Suppress("Unused") @Suppress("Unused")
@HiltAndroidApp @HiltAndroidApp
class Application : android.app.Application(), SharedPreferences.OnSharedPreferenceChangeListener { class Application : android.app.Application(), SharedPreferences.OnSharedPreferenceChangeListener {
@@ -68,7 +69,7 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere
Sentry.configureScope { scope -> Sentry.configureScope { scope ->
val user = User() val user = User()
user.data = user.data =
Feature.VALUES.associate { feature -> Feature.entries.associate { feature ->
"features.${feature.configKey}" to features.isEnabled(feature).toString() "features.${feature.configKey}" to features.isEnabled(feature).toString()
} }
scope.user = user scope.user = user

View File

@@ -37,6 +37,7 @@ import kotlinx.coroutines.flow.onEach
import reactivecircus.flowbinding.android.widget.afterTextChanges import reactivecircus.flowbinding.android.widget.afterTextChanges
import reactivecircus.flowbinding.android.widget.checkedChanges import reactivecircus.flowbinding.android.widget.checkedChanges
@OptIn(ExperimentalStdlibApi::class)
class PasswordGeneratorDialogFragment : DialogFragment() { class PasswordGeneratorDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
@@ -138,10 +139,9 @@ class PasswordGeneratorDialogFragment : DialogFragment() {
*/ */
private fun setPrefs(ctx: Context, options: List<PasswordOption>, targetLength: Int): Boolean { private fun setPrefs(ctx: Context, options: List<PasswordOption>, targetLength: Int): Boolean {
ctx.getSharedPreferences("PasswordGenerator", Context.MODE_PRIVATE).edit { ctx.getSharedPreferences("PasswordGenerator", Context.MODE_PRIVATE).edit {
for (possibleOption in PasswordOption.values()) putBoolean( for (possibleOption in PasswordOption.entries) {
possibleOption.key, putBoolean(possibleOption.key, possibleOption in options)
possibleOption in options }
)
putInt("length", targetLength) putInt("length", targetLength)
} }
return true return true

View File

@@ -25,9 +25,4 @@ enum class Feature(
/** Opt into a cache layer for PGP passphrases. */ /** Opt into a cache layer for PGP passphrases. */
EnablePGPPassphraseCache(false, "enable_gpg_passphrase_cache"), EnablePGPPassphraseCache(false, "enable_gpg_passphrase_cache"),
;
companion object {
@JvmField val VALUES = values()
}
} }

View File

@@ -26,10 +26,9 @@ enum class Protocol(val pref: String) {
companion object { companion object {
private val map = values().associateBy(Protocol::pref) @OptIn(ExperimentalStdlibApi::class)
fun fromString(type: String?): Protocol { fun fromString(type: String?): Protocol {
return map[type ?: return Ssh] return entries.associateBy(Protocol::pref)[type ?: return Ssh]
?: throw IllegalArgumentException("$type is not a valid Protocol") ?: throw IllegalArgumentException("$type is not a valid Protocol")
} }
} }
@@ -43,10 +42,9 @@ enum class AuthMode(val pref: String) {
companion object { companion object {
private val map = values().associateBy(AuthMode::pref) @OptIn(ExperimentalStdlibApi::class)
fun fromString(type: String?): AuthMode { fun fromString(type: String?): AuthMode {
return map[type ?: return SshKey] return entries.associateBy(AuthMode::pref)[type ?: return SshKey]
?: throw IllegalArgumentException("$type is not a valid AuthMode") ?: throw IllegalArgumentException("$type is not a valid AuthMode")
} }
} }

View File

@@ -26,7 +26,7 @@ class KotlinCommonPlugin : Plugin<Project> {
withType<KotlinCompile>().configureEach task@{ withType<KotlinCompile>().configureEach task@{
compilerOptions { compilerOptions {
allWarningsAsErrors.set(true) allWarningsAsErrors.set(true)
languageVersion.set(KotlinVersion.KOTLIN_1_8) languageVersion.set(KotlinVersion.KOTLIN_1_9)
freeCompilerArgs.addAll(ADDITIONAL_COMPILER_ARGS) freeCompilerArgs.addAll(ADDITIONAL_COMPILER_ARGS)
if (!this@task.name.contains("test", ignoreCase = true) && !isAppModule) { if (!this@task.name.contains("test", ignoreCase = true) && !isAppModule) {
freeCompilerArgs.add("-Xexplicit-api=strict") freeCompilerArgs.add("-Xexplicit-api=strict")

View File

@@ -7,6 +7,7 @@ package app.passwordstore.passgen.random
import app.passwordstore.passgen.random.util.clearFlag import app.passwordstore.passgen.random.util.clearFlag
import app.passwordstore.passgen.random.util.hasFlag import app.passwordstore.passgen.random.util.hasFlag
@OptIn(ExperimentalStdlibApi::class)
public object PasswordGenerator { public object PasswordGenerator {
public const val DEFAULT_LENGTH: Int = 16 public const val DEFAULT_LENGTH: Int = 16
@@ -39,7 +40,7 @@ public object PasswordGenerator {
var phonemes = true var phonemes = true
var pwgenFlags = DIGITS or UPPERS or LOWERS var pwgenFlags = DIGITS or UPPERS or LOWERS
for (option in PasswordOption.values()) { for (option in PasswordOption.entries) {
if (option in passwordOptions) { if (option in passwordOptions) {
when (option) { when (option) {
PasswordOption.NoDigits -> pwgenFlags = pwgenFlags.clearFlag(DIGITS) PasswordOption.NoDigits -> pwgenFlags = pwgenFlags.clearFlag(DIGITS)