merge in some changes
This commit is contained in:
@@ -87,6 +87,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
implementation project(':expo-bluesky-swiss-army')
|
||||||
implementation project(':expo-modules-core')
|
implementation project(':expo-modules-core')
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
|
||||||
implementation 'com.google.firebase:firebase-messaging-ktx:24.0.0'
|
implementation 'com.google.firebase:firebase-messaging-ktx:24.0.0'
|
||||||
|
|||||||
+2
-1
@@ -2,6 +2,7 @@ package expo.modules.backgroundnotificationhandler
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.google.firebase.messaging.RemoteMessage
|
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"),
|
Like("like"),
|
||||||
@@ -36,7 +37,7 @@ class BackgroundNotificationHandler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun mutateWithChatMessage(remoteMessage: RemoteMessage) {
|
private fun mutateWithChatMessage(remoteMessage: RemoteMessage) {
|
||||||
if (NotificationPrefs(context).getBoolean("playSoundChat")) {
|
if (Preferences(context).getBoolean("playSoundChat") == true) {
|
||||||
// If oreo or higher
|
// If oreo or higher
|
||||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||||||
remoteMessage.data["channelId"] = "chat-messages"
|
remoteMessage.data["channelId"] = "chat-messages"
|
||||||
|
|||||||
+23
-45
@@ -1,8 +1,20 @@
|
|||||||
package expo.modules.backgroundnotificationhandler
|
package expo.modules.backgroundnotificationhandler
|
||||||
|
|
||||||
|
import expo.modules.blueskyswissarmy.sharedprefs.Preferences
|
||||||
import expo.modules.kotlin.modules.Module
|
import expo.modules.kotlin.modules.Module
|
||||||
import expo.modules.kotlin.modules.ModuleDefinition
|
import expo.modules.kotlin.modules.ModuleDefinition
|
||||||
|
|
||||||
|
val DEFAULTS = mapOf<String, Any>(
|
||||||
|
"playSoundChat" to true,
|
||||||
|
"playSoundFollow" to false,
|
||||||
|
"playSoundLike" to false,
|
||||||
|
"playSoundMention" to false,
|
||||||
|
"playSoundQuote" to false,
|
||||||
|
"playSoundReply" to false,
|
||||||
|
"playSoundRepost" to false,
|
||||||
|
"mutedThreads" to mapOf<String, List<String>>()
|
||||||
|
)
|
||||||
|
|
||||||
class ExpoBackgroundNotificationHandlerModule : Module() {
|
class ExpoBackgroundNotificationHandlerModule : Module() {
|
||||||
companion object {
|
companion object {
|
||||||
var isForegrounded = false
|
var isForegrounded = false
|
||||||
@@ -12,7 +24,17 @@ class ExpoBackgroundNotificationHandlerModule : Module() {
|
|||||||
Name("ExpoBackgroundNotificationHandler")
|
Name("ExpoBackgroundNotificationHandler")
|
||||||
|
|
||||||
OnCreate {
|
OnCreate {
|
||||||
NotificationPrefs(appContext.reactContext).initialize()
|
val context = appContext.reactContext ?: throw Error("Context is null")
|
||||||
|
DEFAULTS.forEach { (key, value) ->
|
||||||
|
if (Preferences(context).hasValue(key)) {
|
||||||
|
return@forEach
|
||||||
|
}
|
||||||
|
|
||||||
|
Preferences(context)._(key, value)
|
||||||
|
|
||||||
|
when (value) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OnActivityEntersForeground {
|
OnActivityEntersForeground {
|
||||||
@@ -23,50 +45,6 @@ class ExpoBackgroundNotificationHandlerModule : Module() {
|
|||||||
isForegrounded = false
|
isForegrounded = false
|
||||||
}
|
}
|
||||||
|
|
||||||
AsyncFunction("getAllPrefsAsync") {
|
|
||||||
return@AsyncFunction NotificationPrefs(appContext.reactContext).getAllPrefs()
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncFunction("getBoolAsync") { forKey: String ->
|
|
||||||
return@AsyncFunction NotificationPrefs(appContext.reactContext).getBoolean(forKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncFunction("getStringAsync") { forKey: String ->
|
|
||||||
return@AsyncFunction NotificationPrefs(appContext.reactContext).getString(forKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncFunction("getStringArrayAsync") { forKey: String ->
|
|
||||||
return@AsyncFunction NotificationPrefs(appContext.reactContext).getStringArray(forKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncFunction("setBoolAsync") { forKey: String, value: Boolean ->
|
|
||||||
NotificationPrefs(appContext.reactContext).setBoolean(forKey, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncFunction("setStringAsync") { forKey: String, value: String ->
|
|
||||||
NotificationPrefs(appContext.reactContext).setString(forKey, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncFunction("setStringArrayAsync") { forKey: String, value: Array<String> ->
|
|
||||||
NotificationPrefs(appContext.reactContext).setStringArray(forKey, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncFunction("addToStringArrayAsync") { forKey: String, string: String ->
|
|
||||||
NotificationPrefs(appContext.reactContext).addToStringArray(forKey, string)
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncFunction("removeFromStringArrayAsync") { forKey: String, string: String ->
|
|
||||||
NotificationPrefs(appContext.reactContext).removeFromStringArray(forKey, string)
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncFunction("addManyToStringArrayAsync") { forKey: String, strings: Array<String> ->
|
|
||||||
NotificationPrefs(appContext.reactContext).addManyToStringArray(forKey, strings)
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncFunction("removeManyFromStringArrayAsync") { forKey: String, strings: Array<String> ->
|
|
||||||
NotificationPrefs(appContext.reactContext).removeManyFromStringArray(forKey, strings)
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncFunction("setBadgeCountAsync") { _: String, _: Int ->
|
AsyncFunction("setBadgeCountAsync") { _: String, _: Int ->
|
||||||
// This does nothing on Android
|
// This does nothing on Android
|
||||||
}
|
}
|
||||||
|
|||||||
-134
@@ -1,134 +0,0 @@
|
|||||||
package expo.modules.backgroundnotificationhandler
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
|
|
||||||
val DEFAULTS = mapOf<String, Any>(
|
|
||||||
"playSoundChat" to true,
|
|
||||||
"playSoundFollow" to false,
|
|
||||||
"playSoundLike" to false,
|
|
||||||
"playSoundMention" to false,
|
|
||||||
"playSoundQuote" to false,
|
|
||||||
"playSoundReply" to false,
|
|
||||||
"playSoundRepost" to false,
|
|
||||||
"mutedThreads" to mapOf<String, List<String>>()
|
|
||||||
)
|
|
||||||
|
|
||||||
class NotificationPrefs (private val context: Context?) {
|
|
||||||
private val prefs = context?.getSharedPreferences("xyz.blueskyweb.app", Context.MODE_PRIVATE)
|
|
||||||
?: throw Error("Context is null")
|
|
||||||
|
|
||||||
fun initialize() {
|
|
||||||
prefs
|
|
||||||
.edit()
|
|
||||||
.apply {
|
|
||||||
DEFAULTS.forEach { (key, value) ->
|
|
||||||
if (prefs.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 getAllPrefs(): MutableMap<String, *> {
|
|
||||||
return prefs.all
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getBoolean(key: String): Boolean {
|
|
||||||
return prefs.getBoolean(key, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getString(key: String): String? {
|
|
||||||
return prefs.getString(key, null)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getStringArray(key: String): Array<String>? {
|
|
||||||
return prefs.getStringSet(key, null)?.toTypedArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setBoolean(key: String, value: Boolean) {
|
|
||||||
prefs
|
|
||||||
.edit()
|
|
||||||
.apply {
|
|
||||||
putBoolean(key, value)
|
|
||||||
}
|
|
||||||
.apply()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setString(key: String, value: String) {
|
|
||||||
prefs
|
|
||||||
.edit()
|
|
||||||
.apply {
|
|
||||||
putString(key, value)
|
|
||||||
}
|
|
||||||
.apply()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setStringArray(key: String, value: Array<String>) {
|
|
||||||
prefs
|
|
||||||
.edit()
|
|
||||||
.apply {
|
|
||||||
putStringSet(key, value.toSet())
|
|
||||||
}
|
|
||||||
.apply()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addToStringArray(key: String, string: String) {
|
|
||||||
prefs
|
|
||||||
.edit()
|
|
||||||
.apply {
|
|
||||||
val set = prefs.getStringSet(key, null)?.toMutableSet() ?: mutableSetOf()
|
|
||||||
set.add(string)
|
|
||||||
putStringSet(key, set)
|
|
||||||
}
|
|
||||||
.apply()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeFromStringArray(key: String, string: String) {
|
|
||||||
prefs
|
|
||||||
.edit()
|
|
||||||
.apply {
|
|
||||||
val set = prefs.getStringSet(key, null)?.toMutableSet() ?: mutableSetOf()
|
|
||||||
set.remove(string)
|
|
||||||
putStringSet(key, set)
|
|
||||||
}
|
|
||||||
.apply()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addManyToStringArray(key: String, strings: Array<String>) {
|
|
||||||
prefs
|
|
||||||
.edit()
|
|
||||||
.apply {
|
|
||||||
val set = prefs.getStringSet(key, null)?.toMutableSet() ?: mutableSetOf()
|
|
||||||
set.addAll(strings.toSet())
|
|
||||||
putStringSet(key, set)
|
|
||||||
}
|
|
||||||
.apply()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeManyFromStringArray(key: String, strings: Array<String>) {
|
|
||||||
prefs
|
|
||||||
.edit()
|
|
||||||
.apply {
|
|
||||||
val set = prefs.getStringSet(key, null)?.toMutableSet() ?: mutableSetOf()
|
|
||||||
set.removeAll(strings.toSet())
|
|
||||||
putStringSet(key, set)
|
|
||||||
}
|
|
||||||
.apply()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+31
@@ -121,6 +121,19 @@ class Preferences(private val context: Context) {
|
|||||||
return safeInstance.getFloat(key, 0.0f)
|
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? {
|
fun getBoolean(key: String): Boolean? {
|
||||||
val safeInstance = getInstance(context)
|
val safeInstance = getInstance(context)
|
||||||
if (!safeInstance.contains(key)) {
|
if (!safeInstance.contains(key)) {
|
||||||
@@ -159,4 +172,22 @@ class Preferences(private val context: Context) {
|
|||||||
val set = safeInstance.getStringSet(key, setOf()) ?: setOf()
|
val set = safeInstance.getStringSet(key, setOf()) ?: setOf()
|
||||||
return set.contains(value)
|
return set.contains(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun hasValue(key: String): Boolean {
|
||||||
|
val safeInstance = getInstance(context)
|
||||||
|
return safeInstance.contains(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getValues(keys: Set<String>): Map<String, Any?> {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user