From 62aa42f07352747e4d0c732e0c28b5f20e6938d1 Mon Sep 17 00:00:00 2001 From: Hailey Date: Fri, 12 Jul 2024 16:26:12 -0700 Subject: [PATCH] lint android --- .../BackgroundNotificationHandler.kt | 4 +- ...ExpoBackgroundNotificationHandlerModule.kt | 63 +++--- .../ExpoBlueskyDevicePrefsModule.kt | 71 ------- .../sharedprefs/Preferences.kt | 193 ------------------ 4 files changed, 35 insertions(+), 296 deletions(-) delete mode 100644 modules/expo-bluesky-swiss-army/android/src/main/java/expo/modules/blueskyswissarmy/sharedprefs/ExpoBlueskyDevicePrefsModule.kt delete mode 100644 modules/expo-bluesky-swiss-army/android/src/main/java/expo/modules/blueskyswissarmy/sharedprefs/Preferences.kt diff --git a/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/BackgroundNotificationHandler.kt b/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/BackgroundNotificationHandler.kt index aa107e8ec0..8ab0ce3dad 100644 --- a/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/BackgroundNotificationHandler.kt +++ b/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/BackgroundNotificationHandler.kt @@ -4,7 +4,9 @@ import android.content.Context import com.google.firebase.messaging.RemoteMessage import expo.modules.blueskyswissarmy.sharedprefs.Preferences -enum class NotificationType(val type: String) { +enum class NotificationType( + val type: String, +) { Like("like"), Repost("repost"), Follow("follow"), diff --git a/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/ExpoBackgroundNotificationHandlerModule.kt b/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/ExpoBackgroundNotificationHandlerModule.kt index a76ede9ed6..2a8ce16913 100644 --- a/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/ExpoBackgroundNotificationHandlerModule.kt +++ b/modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/ExpoBackgroundNotificationHandlerModule.kt @@ -4,16 +4,17 @@ import expo.modules.blueskyswissarmy.sharedprefs.Preferences import expo.modules.kotlin.modules.Module import expo.modules.kotlin.modules.ModuleDefinition -val DEFAULTS = mapOf( - "playSoundChat" to true, - "playSoundFollow" to false, - "playSoundLike" to false, - "playSoundMention" to false, - "playSoundQuote" to false, - "playSoundReply" to false, - "playSoundRepost" to false, - "mutedThreads" to mapOf>() -) +val DEFAULTS = + mapOf( + "playSoundChat" to true, + "playSoundFollow" to false, + "playSoundLike" to false, + "playSoundMention" to false, + "playSoundQuote" to false, + "playSoundReply" to false, + "playSoundRepost" to false, + "mutedThreads" to mapOf>(), + ) class ExpoBackgroundNotificationHandlerModule : Module() { companion object { @@ -24,15 +25,15 @@ class ExpoBackgroundNotificationHandlerModule : Module() { ModuleDefinition { Name("ExpoBackgroundNotificationHandler") - OnCreate { - val context = appContext.reactContext ?: throw Error("Context is null") - DEFAULTS.forEach { (key, value) -> - if (Preferences(context).hasValue(key)) { - return@forEach + OnCreate { + val context = appContext.reactContext ?: throw Error("Context is null") + DEFAULTS.forEach { (key, value) -> + if (Preferences(context).hasValue(key)) { + return@forEach + } + Preferences(context)._setAnyValue(key, value) } - Preferences(context)._setAnyValue(key, value) } - } OnActivityEntersForeground { isForegrounded = true @@ -42,22 +43,22 @@ class ExpoBackgroundNotificationHandlerModule : Module() { isForegrounded = false } - AsyncFunction("getPrefsAsync") { - val context = appContext.reactContext ?: throw Error("Context is null") - val keys = DEFAULTS.keys - return@AsyncFunction Preferences(context).getValues(keys) - } + AsyncFunction("getPrefsAsync") { + val context = appContext.reactContext ?: throw Error("Context is null") + val keys = DEFAULTS.keys + return@AsyncFunction Preferences(context).getValues(keys) + } - AsyncFunction("resetGenericCountAsync") { - // Not implemented - } + AsyncFunction("resetGenericCountAsync") { + // Not implemented + } - AsyncFunction("maybeIncrementMessagesCountAsync") { - // Not implemented - } + AsyncFunction("maybeIncrementMessagesCountAsync") { + // Not implemented + } - AsyncFunction("maybeDecrementMessagesCountAsync") { - // Not implemented + AsyncFunction("maybeDecrementMessagesCountAsync") { + // Not implemented + } } - } } diff --git a/modules/expo-bluesky-swiss-army/android/src/main/java/expo/modules/blueskyswissarmy/sharedprefs/ExpoBlueskyDevicePrefsModule.kt b/modules/expo-bluesky-swiss-army/android/src/main/java/expo/modules/blueskyswissarmy/sharedprefs/ExpoBlueskyDevicePrefsModule.kt deleted file mode 100644 index 6d81449e50..0000000000 --- a/modules/expo-bluesky-swiss-army/android/src/main/java/expo/modules/blueskyswissarmy/sharedprefs/ExpoBlueskyDevicePrefsModule.kt +++ /dev/null @@ -1,71 +0,0 @@ -package expo.modules.blueskyswissarmy.sharedprefs - -import android.content.Context -import android.util.Log -import expo.modules.kotlin.Promise -import expo.modules.kotlin.jni.JavaScriptValue -import expo.modules.kotlin.modules.Module -import expo.modules.kotlin.modules.ModuleDefinition - -class ExpoBlueskySharedPrefsModule : Module() { - private fun getContext(): Context { - val context = appContext.reactContext ?: throw Error("Context is null") - return context - } - - override fun definition() = ModuleDefinition { - Name("ExpoBlueskySharedPrefs") - - Function("setString") { key: String, value: String -> - return@Function SharedPrefs(getContext()).setValue(key, value) - } - - Function("setValue") { key: String, value: JavaScriptValue -> - val context = getContext() - Log.d("ExpoBlueskySharedPrefs", "Setting value for key: $key") - try { - if (value.isNumber()) { - SharedPrefs(context).setValue(key, value.getFloat()) - } else if (value.isBool()) { - SharedPrefs(context).setValue(key, value.getBool()) - } else if (value.isNull() || value.isUndefined()) { - SharedPrefs(context).removeValue(key) - } else { - Log.d(NAME, "Unsupported type: ${value.kind()}") - promise.reject("UNSUPPORTED_TYPE_ERROR", "Attempted to set an unsupported type", null) - } - } catch (e: Error) { - Log.d(NAME, "Error setting value: $e") - promise.reject("SET_VALUE_ERROR", "Error setting value", e) - } - } - - Function("removeValue") { key: String -> - return@Function SharedPrefs(getContext()).removeValue(key) - } - - Function("getString") { key: String -> - return@Function SharedPrefs(getContext()).getString(key) - } - - Function("getNumber") { key: String -> - return@Function SharedPrefs(getContext()).getFloat(key) - } - - Function("getBool") { key: String -> - return@Function SharedPrefs(getContext()).getBoolean(key) - } - - Function("addToSet") { key: String, value: String -> - return@Function SharedPrefs(getContext()).addToSet(key, value) - } - - Function("removeFromSet") { key: String, value: String -> - return@Function SharedPrefs(getContext()).removeFromSet(key, value) - } - - Function("setContains") { key: String, value: String -> - return@Function SharedPrefs(getContext()).setContains(key, value) - } - } -} diff --git a/modules/expo-bluesky-swiss-army/android/src/main/java/expo/modules/blueskyswissarmy/sharedprefs/Preferences.kt b/modules/expo-bluesky-swiss-army/android/src/main/java/expo/modules/blueskyswissarmy/sharedprefs/Preferences.kt deleted file mode 100644 index e91ec654a3..0000000000 --- a/modules/expo-bluesky-swiss-army/android/src/main/java/expo/modules/blueskyswissarmy/sharedprefs/Preferences.kt +++ /dev/null @@ -1,193 +0,0 @@ -package expo.modules.blueskyswissarmy.sharedprefs - -import android.content.Context -import android.content.SharedPreferences -import android.util.Log - -val DEFAULTS = mapOf( - "playSoundChat" to true, - "playSoundFollow" to false, - "playSoundLike" to false, - "playSoundMention" to false, - "playSoundQuote" to false, - "playSoundReply" to false, - "playSoundRepost" to false, - "badgeCount" to 0, -) - -const val NAME = "SharedPrefs" - -class Preferences(private val context: Context) { - companion object { - private var hasInitialized = false - - private var instance: SharedPreferences? = null - - fun getInstance(context: Context, info: String? = "(no info)"): SharedPreferences { - if (instance == null) { - Log.d(NAME, "No preferences instance found, creating one.") - instance = context.getSharedPreferences("xyz.blueskyweb.app", Context.MODE_PRIVATE) - } - - val safeInstance = instance ?: throw Error("Preferences is null: $info") - - if (!hasInitialized) { - Log.d(NAME, "Preferences instance has not been initialized yet.") - initialize(safeInstance) - hasInitialized = true - Log.d(NAME, "Preferences instance has been initialized.") - } - - return safeInstance - } - - private fun initialize(instance: SharedPreferences) { - instance - .edit() - .apply { - DEFAULTS.forEach { (key, value) -> - if (instance.contains(key)) { - return@forEach - } - - when (value) { - is Boolean -> { - putBoolean(key, value) - } - - is String -> { - putString(key, value) - } - - is Array<*> -> { - putStringSet(key, value.map { it.toString() }.toSet()) - } - - is Map<*, *> -> { - putStringSet(key, value.map { it.toString() }.toSet()) - } - } - } - } - .apply() - } - } - - fun setValue(key: String, value: String) { - val safeInstance = getInstance(context) - safeInstance.edit().apply { - putString(key, value) - }.apply() - } - - fun setValue(key: String, value: Float) { - val safeInstance = getInstance(context) - safeInstance.edit().apply { - putFloat(key, value) - }.apply() - } - - fun setValue(key: String, value: Boolean) { - val safeInstance = getInstance(context) - safeInstance.edit().apply { - putBoolean(key, value) - }.apply() - } - - fun setValue(key: String, value: Set) { - val safeInstance = getInstance(context) - safeInstance.edit().apply { - putStringSet(key, value) - }.apply() - } - - fun removeValue(key: String) { - val safeInstance = getInstance(context) - safeInstance.edit().apply { - remove(key) - }.apply() - } - - fun getString(key: String): String? { - val safeInstance = getInstance(context) - return safeInstance.getString(key, null) - } - - fun getFloat(key: String): Float? { - val safeInstance = getInstance(context) - if (!safeInstance.contains(key)) { - return null - } - return safeInstance.getFloat(key, 0.0f) - } - - fun _setAnyValue(key: String, value: Any) { - val safeInstance = getInstance(context) - safeInstance.edit().apply { - when (value) { - is String -> putString(key, value) - is Float -> putFloat(key, value) - is Boolean -> putBoolean(key, value) - is Set<*> -> putStringSet(key, value.map { it.toString() }.toSet()) - else -> throw Error("Unsupported type: ${value::class.java}") - } - }.apply() - } - - fun getBoolean(key: String): Boolean? { - val safeInstance = getInstance(context) - if (!safeInstance.contains(key)) { - return null - } - Log.d(NAME, "Getting boolean for key: $key") - val res = safeInstance.getBoolean(key, false) - Log.d(NAME, "Got boolean for key: $key, value: $res") - return res - } - - fun addToSet(key: String, value: String) { - val safeInstance = getInstance(context) - val set = safeInstance.getStringSet(key, setOf()) ?: setOf() - val newSet = set.toMutableSet().apply { - add(value) - } - safeInstance.edit().apply { - putStringSet(key, newSet) - }.apply() - } - - fun removeFromSet(key: String, value: String) { - val safeInstance = getInstance(context) - val set = safeInstance.getStringSet(key, setOf()) ?: setOf() - val newSet = set.toMutableSet().apply { - remove(value) - } - safeInstance.edit().apply { - putStringSet(key, newSet) - }.apply() - } - - fun setContains(key: String, value: String): Boolean { - val safeInstance = getInstance(context) - val set = safeInstance.getStringSet(key, setOf()) ?: setOf() - return set.contains(value) - } - - fun hasValue(key: String): Boolean { - val safeInstance = getInstance(context) - return safeInstance.contains(key) - } - - fun getValues(keys: Set): Map { - val safeInstance = getInstance(context) - return keys.associateWith { key -> - when (val value = safeInstance.all[key]) { - is String -> value - is Float -> value - is Boolean -> value - is Set<*> -> value - else -> null - } - } - } -}