-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing Desktop support for local notification (#44)
* Initializing Desktop target * Initial local notifier implementation * Implementation of joptionspane for not supported tray notification
- Loading branch information
1 parent
6807e2d
commit 14d4a92
Showing
18 changed files
with
262 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
kmpnotifier/src/jvmMain/kotlin/com/mmk/kmpnotifier/di/PlatformModule.jvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.mmk.kmpnotifier.di | ||
|
||
import com.mmk.kmpnotifier.firebase.FirebaseDesktopPushNotifier | ||
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 | ||
import com.mmk.kmpnotifier.permission.DesktopPermissionUtil | ||
import com.mmk.kmpnotifier.permission.PermissionUtil | ||
import org.koin.core.module.Module | ||
import org.koin.core.module.dsl.factoryOf | ||
import org.koin.dsl.bind | ||
import org.koin.dsl.module | ||
|
||
internal actual val platformModule: Module = module { | ||
factory { Platform.Desktop } bind Platform::class | ||
|
||
factory { | ||
val configuration = | ||
get<NotificationPlatformConfiguration>() as NotificationPlatformConfiguration.Desktop | ||
DesktopNotifierFactory.getNotifier(configuration = configuration) | ||
} bind Notifier::class | ||
factoryOf(::DesktopPermissionUtil) bind PermissionUtil::class | ||
factoryOf(::FirebaseDesktopPushNotifier) bind PushNotifier::class | ||
} |
27 changes: 27 additions & 0 deletions
27
kmpnotifier/src/jvmMain/kotlin/com/mmk/kmpnotifier/extensions/DesktopPlatformExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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 | ||
} | ||
|
||
internal fun getDesktopPlatformType(): DesktopPlatform? { | ||
val name = System.getProperty("os.name") | ||
return when { | ||
name?.contains("Linux") == true -> DesktopPlatform.Linux | ||
name?.contains("Win") == true -> DesktopPlatform.Windows | ||
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() | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
kmpnotifier/src/jvmMain/kotlin/com/mmk/kmpnotifier/firebase/FirebaseDesktopPushNotifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.mmk.kmpnotifier.firebase | ||
|
||
import com.mmk.kmpnotifier.notification.PushNotifier | ||
|
||
internal class FirebaseDesktopPushNotifier:PushNotifier { | ||
override suspend fun getToken(): String? { | ||
println("Get firebase toekn") | ||
return null | ||
} | ||
|
||
override suspend fun deleteMyToken() { | ||
println("Delete firebase toekn") | ||
} | ||
|
||
override suspend fun subscribeToTopic(topic: String) { | ||
println("Subscribe firebase topic") | ||
} | ||
|
||
override suspend fun unSubscribeFromTopic(topic: String) { | ||
println("Unsubscribe firebase topic") | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
kmpnotifier/src/jvmMain/kotlin/com/mmk/kmpnotifier/notification/DesktopNotifierFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
kmpnotifier/src/jvmMain/kotlin/com/mmk/kmpnotifier/notification/impl/JOptionPaneNotifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
kmpnotifier/src/jvmMain/kotlin/com/mmk/kmpnotifier/notification/impl/TrayNotifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
kmpnotifier/src/jvmMain/kotlin/com/mmk/kmpnotifier/permission/DesktopPermissionUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.mmk.kmpnotifier.permission | ||
|
||
internal class DesktopPermissionUtil:PermissionUtil { | ||
override fun hasNotificationPermission(onPermissionResult: (Boolean) -> Unit) { | ||
println("Desktop has permission result") | ||
onPermissionResult(true) | ||
} | ||
|
||
override fun askNotificationPermission(onPermissionGranted: () -> Unit) { | ||
println("Desktop ask permission") | ||
onPermissionGranted() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+618 Bytes
sample/src/commonMain/composeResources/drawable/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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
sample/src/desktopMain/kotlin/com/mmk/kmpnotifier/sample/Main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.mmk.kmpnotifier.sample | ||
|
||
import androidx.compose.ui.window.Window | ||
import androidx.compose.ui.window.application | ||
|
||
|
||
fun main() = application { | ||
AppInitializer.onApplicationStart() | ||
Window( | ||
onCloseRequest = ::exitApplication, | ||
title = "KMPNotifier Desktop", | ||
) { | ||
println("Desktop app is started") | ||
App() | ||
|
||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
sample/src/desktopMain/kotlin/com/mmk/kmpnotifier/sample/Platform.desktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 = composeDesktopResourcesPath() + File.separator + "ic_notification.png" | ||
) | ||
) | ||
} |