Skip to content

Commit

Permalink
Implementation of joptionspane for not supported tray notification
Browse files Browse the repository at this point in the history
  • Loading branch information
mirzemehdi committed Jul 7, 2024
1 parent 66e0e96 commit 05155dc
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mmk.kmpnotifier.di

import com.mmk.kmpnotifier.firebase.FirebaseDesktopPushNotifier
import com.mmk.kmpnotifier.notification.DesktopNotifier
import com.mmk.kmpnotifier.notification.DesktopNotifierFactory
import com.mmk.kmpnotifier.notification.Notifier
import com.mmk.kmpnotifier.notification.PushNotifier
import com.mmk.kmpnotifier.notification.configuration.NotificationPlatformConfiguration
Expand All @@ -18,7 +18,7 @@ internal actual val platformModule: Module = module {
factory {
val configuration =
get<NotificationPlatformConfiguration>() as NotificationPlatformConfiguration.Desktop
DesktopNotifier(desktopNotificationConfiguration = configuration)
DesktopNotifierFactory.getNotifier(configuration = configuration)
} bind Notifier::class
factoryOf(::DesktopPermissionUtil) bind PermissionUtil::class
factoryOf(::FirebaseDesktopPushNotifier) bind PushNotifier::class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.mmk.kmpnotifier.extensions

import java.io.File

internal sealed interface DesktopPlatform {
data object Linux:DesktopPlatform
data object Windows:DesktopPlatform
data object MacOs:DesktopPlatform
data object Linux : DesktopPlatform
data object Windows : DesktopPlatform
data object MacOs : DesktopPlatform
}

internal fun getDesktopPlatformType(): DesktopPlatform? {
Expand All @@ -14,4 +16,12 @@ internal fun getDesktopPlatformType(): DesktopPlatform? {
name?.contains("Mac") == true -> DesktopPlatform.MacOs
else -> null
}
}

public fun composeDesktopResourcesPath(): String? {
return runCatching {
val resourcesDirectory = File(System.getProperty("compose.application.resources.dir"))
return resourcesDirectory.canonicalPath
}.getOrNull()

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mmk.kmpnotifier.notification

import com.mmk.kmpnotifier.notification.configuration.NotificationPlatformConfiguration
import com.mmk.kmpnotifier.notification.impl.JOptionPaneNotifier
import com.mmk.kmpnotifier.notification.impl.TrayNotifier

internal object DesktopNotifierFactory {
fun getNotifier(configuration: NotificationPlatformConfiguration.Desktop): Notifier {
return when {
TrayNotifier.isSupported -> TrayNotifier(configuration = configuration)
//TODO for now return JOptionPaneNotifier for not supported platforms
else -> JOptionPaneNotifier(configuration = configuration)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mmk.kmpnotifier.notification.impl

import com.mmk.kmpnotifier.notification.Notifier
import com.mmk.kmpnotifier.notification.configuration.NotificationPlatformConfiguration
import javax.swing.ImageIcon
import javax.swing.JOptionPane

internal class JOptionPaneNotifier(private val configuration: NotificationPlatformConfiguration.Desktop) :
Notifier {

override fun notify(title: String, body: String, payloadData: Map<String, String>): Int {
val id = -1
notify(id = id, title = title, body = body, payloadData)
return id
}

override fun notify(id: Int, title: String, body: String, payloadData: Map<String, String>) {
JOptionPane.showMessageDialog(
null,
body,
title,
JOptionPane.INFORMATION_MESSAGE,
ImageIcon(configuration.notificationIconPath)
)
}

override fun remove(id: Int) {
println("No remove functionality")
}

override fun removeAll() {
println("No removeAll functionality")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.mmk.kmpnotifier.notification.impl

import com.mmk.kmpnotifier.notification.Notifier
import com.mmk.kmpnotifier.notification.configuration.NotificationPlatformConfiguration
import java.awt.SystemTray
import java.awt.Toolkit
import java.awt.TrayIcon
import kotlin.random.Random

internal class TrayNotifier(private val configuration: NotificationPlatformConfiguration.Desktop) :
Notifier {

private val trayIcons: MutableMap<Int, TrayIcon> = mutableMapOf()

companion object {
val isSupported by lazy {
SystemTray.isSupported().also {
if (it.not()) System.err.println(
"Tray is not supported on the current platform. "
)
}
}
}

override fun notify(title: String, body: String, payloadData: Map<String, String>): Int {
if (isSupported.not()) return -1
val notificationID = Random.nextInt(0, Int.MAX_VALUE)
notify(notificationID, title, body, payloadData)
return notificationID
}

override fun notify(
id: Int,
title: String,
body: String,
payloadData: Map<String, String>
) {
if (isSupported.not()) return
val icon = Toolkit.getDefaultToolkit().getImage(configuration.notificationIconPath)
val trayIcon = TrayIcon(icon).apply {
isImageAutoSize = true
}
SystemTray.getSystemTray().add(trayIcon)
.also { trayIcons[id] = trayIcon }
trayIcon.displayMessage(title, body, TrayIcon.MessageType.INFO)
}

override fun remove(id: Int) {
val systemTray = SystemTray.getSystemTray()
val trayIcon = trayIcons.getOrDefault(id, null)
trayIcon?.let { systemTray.remove(it) }
}

override fun removeAll() {
val systemTray = SystemTray.getSystemTray()
systemTray.trayIcons.forEach { systemTray.remove(it) }
}
}
Binary file modified sample/resources/common/ic_notification.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mmk.kmpnotifier.sample

import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberTrayState


fun main() = application {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.mmk.kmpnotifier.sample

import com.mmk.kmpnotifier.extensions.composeDesktopResourcesPath
import com.mmk.kmpnotifier.notification.NotifierManager
import com.mmk.kmpnotifier.notification.configuration.NotificationPlatformConfiguration
import java.io.File

actual fun onApplicationStartPlatformSpecific() {
println("Desktop app is initialized")
NotifierManager.initialize(
NotificationPlatformConfiguration.Desktop(showPushNotification = true, notificationIconPath = "ic_notification.png")
NotificationPlatformConfiguration.Desktop(
showPushNotification = true,
notificationIconPath = composeDesktopResourcesPath() + File.separator + "ic_notification.png"
)
)
}

0 comments on commit 05155dc

Please sign in to comment.