mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-25 11:46:06 +02:00
feat: update to alpha.17, typed mobile plugin IPC arguments (#676)
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
This commit is contained in:
committed by
GitHub
parent
76cfdc32b4
commit
e438e0a62d
@@ -6,23 +6,23 @@ package app.tauri.notification
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import app.tauri.plugin.JSObject
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import org.json.JSONException
|
||||
import java.text.ParseException
|
||||
import java.lang.Exception
|
||||
|
||||
// Key for private preferences
|
||||
private const val NOTIFICATION_STORE_ID = "NOTIFICATION_STORE"
|
||||
// Key used to save action types
|
||||
private const val ACTION_TYPES_ID = "ACTION_TYPE_STORE"
|
||||
|
||||
class NotificationStorage(private val context: Context) {
|
||||
class NotificationStorage(private val context: Context, private val jsonMapper: ObjectMapper) {
|
||||
fun appendNotifications(localNotifications: List<Notification>) {
|
||||
val storage = getStorage(NOTIFICATION_STORE_ID)
|
||||
val editor = storage.edit()
|
||||
for (request in localNotifications) {
|
||||
if (request.isScheduled) {
|
||||
val key: String = request.id.toString()
|
||||
editor.putString(key, request.source.toString())
|
||||
editor.putString(key, request.sourceJson.toString())
|
||||
}
|
||||
}
|
||||
editor.apply()
|
||||
@@ -43,57 +43,29 @@ class NotificationStorage(private val context: Context) {
|
||||
val notifications = ArrayList<Notification>()
|
||||
for (key in all.keys) {
|
||||
val notificationString = all[key] as String?
|
||||
val jsNotification = getNotificationFromJSONString(notificationString)
|
||||
if (jsNotification != null) {
|
||||
try {
|
||||
val notification =
|
||||
Notification.fromJSObject(jsNotification)
|
||||
notifications.add(notification)
|
||||
} catch (_: ParseException) {
|
||||
}
|
||||
}
|
||||
try {
|
||||
val notification = jsonMapper.readValue(notificationString, Notification::class.java)
|
||||
notifications.add(notification)
|
||||
} catch (_: Exception) { }
|
||||
}
|
||||
return notifications
|
||||
}
|
||||
return ArrayList()
|
||||
}
|
||||
|
||||
private fun getNotificationFromJSONString(notificationString: String?): JSObject? {
|
||||
if (notificationString == null) {
|
||||
return null
|
||||
}
|
||||
val jsNotification = try {
|
||||
JSObject(notificationString)
|
||||
} catch (ex: JSONException) {
|
||||
return null
|
||||
}
|
||||
return jsNotification
|
||||
}
|
||||
|
||||
fun getSavedNotificationAsJSObject(key: String?): JSObject? {
|
||||
fun getSavedNotification(key: String): Notification? {
|
||||
val storage = getStorage(NOTIFICATION_STORE_ID)
|
||||
val notificationString = try {
|
||||
storage.getString(key, null)
|
||||
} catch (ex: ClassCastException) {
|
||||
return null
|
||||
} ?: return null
|
||||
|
||||
val jsNotification = try {
|
||||
JSObject(notificationString)
|
||||
} catch (ex: JSONException) {
|
||||
return null
|
||||
}
|
||||
return jsNotification
|
||||
}
|
||||
|
||||
fun getSavedNotification(key: String?): Notification? {
|
||||
val jsNotification = getSavedNotificationAsJSObject(key) ?: return null
|
||||
val notification = try {
|
||||
Notification.fromJSObject(jsNotification)
|
||||
} catch (ex: ParseException) {
|
||||
return null
|
||||
return try {
|
||||
jsonMapper.readValue(notificationString, Notification::class.java)
|
||||
} catch (ex: JSONException) {
|
||||
null
|
||||
}
|
||||
return notification
|
||||
}
|
||||
|
||||
fun deleteNotification(id: String?) {
|
||||
@@ -106,15 +78,16 @@ class NotificationStorage(private val context: Context) {
|
||||
return context.getSharedPreferences(key, Context.MODE_PRIVATE)
|
||||
}
|
||||
|
||||
fun writeActionGroup(typesMap: Map<String, List<NotificationAction>>) {
|
||||
for ((id, notificationActions) in typesMap) {
|
||||
val editor = getStorage(ACTION_TYPES_ID + id).edit()
|
||||
fun writeActionGroup(actions: List<ActionType>) {
|
||||
for (type in actions) {
|
||||
val i = type.id
|
||||
val editor = getStorage(ACTION_TYPES_ID + type.id).edit()
|
||||
editor.clear()
|
||||
editor.putInt("count", notificationActions.size)
|
||||
for (i in notificationActions.indices) {
|
||||
editor.putString("id$i", notificationActions[i].id)
|
||||
editor.putString("title$i", notificationActions[i].title)
|
||||
editor.putBoolean("input$i", notificationActions[i].input)
|
||||
editor.putInt("count", type.actions.size)
|
||||
for (action in type.actions) {
|
||||
editor.putString("id$i", action.id)
|
||||
editor.putString("title$i", action.title)
|
||||
editor.putBoolean("input$i", action.input ?: false)
|
||||
}
|
||||
editor.apply()
|
||||
}
|
||||
@@ -128,7 +101,12 @@ class NotificationStorage(private val context: Context) {
|
||||
val id = storage.getString("id$i", "")
|
||||
val title = storage.getString("title$i", "")
|
||||
val input = storage.getBoolean("input$i", false)
|
||||
actions[i] = NotificationAction(id, title, input)
|
||||
|
||||
val action = NotificationAction()
|
||||
action.id = id ?: ""
|
||||
action.title = title
|
||||
action.input = input
|
||||
actions[i] = action
|
||||
}
|
||||
return actions
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user