lateinit vars

pull/340/head
Lucas Nogueira 2 years ago
parent bf6e0b0ca7
commit e29c240c36
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1

@ -5,6 +5,7 @@ import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.app.NotificationManager import android.app.NotificationManager
import android.content.Context import android.content.Context
import android.content.Intent
import android.os.Build import android.os.Build
import android.webkit.WebView import android.webkit.WebView
import app.tauri.PermissionState import app.tauri.PermissionState
@ -28,9 +29,9 @@ const val LOCAL_NOTIFICATIONS = "permissionState"
) )
class NotificationPlugin(private val activity: Activity): Plugin(activity) { class NotificationPlugin(private val activity: Activity): Plugin(activity) {
private var webView: WebView? = null private var webView: WebView? = null
private var manager: TauriNotificationManager? = null private lateinit var manager: TauriNotificationManager
var notificationManager: NotificationManager? = null private lateinit var notificationManager: NotificationManager
private var notificationStorage: NotificationStorage? = null private lateinit var notificationStorage: NotificationStorage
private var channelManager = ChannelManager(activity) private var channelManager = ChannelManager(activity)
override fun load(webView: WebView) { override fun load(webView: WebView) {
@ -39,7 +40,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
notificationStorage = NotificationStorage(activity) notificationStorage = NotificationStorage(activity)
val manager = TauriNotificationManager( val manager = TauriNotificationManager(
notificationStorage!!, notificationStorage,
activity, activity,
activity, activity,
getConfig() getConfig()
@ -48,24 +49,24 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
this.manager = manager this.manager = manager
notificationManager = activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager? notificationManager = activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
} }
/*private fun handleOnNewIntent(data: Intent) { override fun onNewIntent(intent: Intent) {
super.handleOnNewIntent(data) super.onNewIntent(intent)
if (Intent.ACTION_MAIN != data.action) { if (Intent.ACTION_MAIN != intent.action) {
return return
} }
val dataJson = manager.handleNotificationActionPerformed(data, notificationStorage) val dataJson = manager.handleNotificationActionPerformed(intent, notificationStorage)
if (dataJson != null) { if (dataJson != null) {
// notifyListeners("localNotificationActionPerformed", dataJson, true) // TODO trigger("actionPerformed", dataJson, true)
} }
}*/ }
@Command @Command
fun show(invoke: Invoke) { fun show(invoke: Invoke) {
val notification = Notification.fromJSObject(invoke.data) val notification = Notification.fromJSObject(invoke.data)
val id = manager!!.schedule(notification) val id = manager.schedule(notification)
val returnVal = JSObject().put("id", id) val returnVal = JSObject().put("id", id)
invoke.resolve(returnVal) invoke.resolve(returnVal)
@ -93,8 +94,8 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
notifications.add(notification) notifications.add(notification)
} }
val ids = manager!!.schedule(notifications) val ids = manager.schedule(notifications)
notificationStorage?.appendNotifications(notifications) notificationStorage.appendNotifications(notifications)
val result = JSObject() val result = JSObject()
val jsArray = JSArray() val jsArray = JSArray()
@ -113,7 +114,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
fun cancel(invoke: Invoke) { fun cancel(invoke: Invoke) {
val notifications = invoke.getArray("notifications") val notifications = invoke.getArray("notifications")
if (notifications == null) { if (notifications == null) {
manager?.cancel(invoke) manager.cancel(invoke)
} else { } else {
try { try {
for (o in notifications.toList<Any>()) { for (o in notifications.toList<Any>()) {
@ -122,9 +123,9 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
val tag = notification.getString("tag") val tag = notification.getString("tag")
val id = notification.getInteger("id") val id = notification.getInteger("id")
if (tag.isEmpty()) { if (tag.isEmpty()) {
notificationManager!!.cancel(id!!) notificationManager.cancel(id!!)
} else { } else {
notificationManager!!.cancel(tag, id!!) notificationManager.cancel(tag, id!!)
} }
} else { } else {
invoke.reject("Unexpected notification type") invoke.reject("Unexpected notification type")
@ -139,7 +140,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
@Command @Command
fun getPending(invoke: Invoke) { fun getPending(invoke: Invoke) {
val notifications= notificationStorage!!.getSavedNotifications() val notifications= notificationStorage.getSavedNotifications()
val result = Notification.buildNotificationPendingList(notifications) val result = Notification.buildNotificationPendingList(notifications)
invoke.resolve(result) invoke.resolve(result)
} }
@ -148,7 +149,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
fun registerActionTypes(invoke: Invoke) { fun registerActionTypes(invoke: Invoke) {
val types = invoke.getArray("types", JSArray()) val types = invoke.getArray("types", JSArray())
val typesArray = NotificationAction.buildTypes(types) val typesArray = NotificationAction.buildTypes(types)
notificationStorage?.writeActionGroup(typesArray) notificationStorage.writeActionGroup(typesArray)
invoke.resolve() invoke.resolve()
} }
@ -157,7 +158,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
fun getDeliveredNotifications(invoke: Invoke) { fun getDeliveredNotifications(invoke: Invoke) {
val notifications = JSArray() val notifications = JSArray()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val activeNotifications = notificationManager!!.activeNotifications val activeNotifications = notificationManager.activeNotifications
for (activeNotification in activeNotifications) { for (activeNotification in activeNotifications) {
val jsNotification = JSObject() val jsNotification = JSObject()
jsNotification.put("id", activeNotification.id) jsNotification.put("id", activeNotification.id)
@ -187,7 +188,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
@Command @Command
fun cancelAll(invoke: Invoke) { fun cancelAll(invoke: Invoke) {
notificationManager!!.cancelAll() notificationManager.cancelAll()
invoke.resolve() invoke.resolve()
} }
@ -243,7 +244,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
} }
private fun getPermissionState(): String { private fun getPermissionState(): String {
return if (manager!!.areNotificationsEnabled()) { return if (manager.areNotificationsEnabled()) {
"granted" "granted"
} else { } else {
"denied" "denied"

@ -33,9 +33,7 @@ import { invoke } from '@tauri-apps/api/tauri'
*/ */
interface Options { interface Options {
/** /**
* The notification identifier to reference this object later. * The notification identifier to reference this object later. Must be a 32-bit integer.
*
* On Android, it is a 32 bit integer.
*/ */
id?: number id?: number
/** /**
@ -132,24 +130,24 @@ interface Options {
} }
type ScheduleInterval = { type ScheduleInterval = {
year?: number year?: number
month?: number month?: number
day?: number day?: number
/** /**
* 1 - Sunday * 1 - Sunday
* 2 - Monday * 2 - Monday
* 3 - Tuesday * 3 - Tuesday
* 4 - Wednesday * 4 - Wednesday
* 5 - Thursday * 5 - Thursday
* 6 - Friday * 6 - Friday
* 7 - Saturday * 7 - Saturday
*/ */
weekday?: number weekday?: number
hour?: number hour?: number
minute?: number minute?: number
second?: number second?: number
} }
enum ScheduleEvery { enum ScheduleEvery {
Year = 'Year', Year = 'Year',
Month = 'Month', Month = 'Month',
@ -190,7 +188,7 @@ class Schedule {
} }
static at(date: Date, repeating = false) { static at(date: Date, repeating = false) {
return new Schedule({ kind: 'At', data: { date, repeating }}) return new Schedule({ kind: 'At', data: { date, repeating } })
} }
static interval(interval: ScheduleInterval) { static interval(interval: ScheduleInterval) {
@ -198,7 +196,7 @@ class Schedule {
} }
static every(kind: ScheduleEvery) { static every(kind: ScheduleEvery) {
return new Schedule({ kind: 'Every', data: { interval: kind }}) return new Schedule({ kind: 'Every', data: { interval: kind } })
} }
} }

Loading…
Cancel
Save