Skip to content

Commit

Permalink
-iOS Toast Enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
KhubaibKhan4 committed Sep 25, 2024
1 parent a455bf2 commit 0e92a71
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion alert-kmp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ mavenPublishing {
coordinates(
groupId = "io.github.khubaibkhan4",
artifactId = "alert-kmp",
version = "1.0.4"
version = "1.0.5"
)

// Configure POM metadata for the published artifact
Expand Down
44 changes: 27 additions & 17 deletions alert-kmp/src/iosMain/kotlin/NativeNotify.ios.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,38 @@ import platform.UserNotifications.UNNotificationRequest
import platform.UserNotifications.UNNotificationSound
import platform.UserNotifications.UNTimeIntervalNotificationTrigger
import platform.UserNotifications.UNUserNotificationCenter
import platform.darwin.dispatch_async
import platform.darwin.dispatch_get_main_queue

actual fun Notify(message: String, duration: NotificationDuration) {
val viewController = UIApplication.sharedApplication.keyWindow?.rootViewController?.modalViewController
val alertController = UIAlertController.alertControllerWithTitle(
title = UIDevice.currentDevice.systemName,
message = message,
preferredStyle = UIAlertControllerStyle.MAX_VALUE
)
alertController.addAction(
UIAlertAction.actionWithTitle(
"OK",
style = UIAlertControllerStyle.MAX_VALUE,
handler = null
val viewController = UIApplication.sharedApplication.keyWindow?.rootViewController
if (viewController != null) {
val alertController = UIAlertController.alertControllerWithTitle(
title = UIDevice.currentDevice.systemName,
message = message,
preferredStyle = UIAlertControllerStyleAlert
)
)
viewController?.presentViewController(alertController, animated = true, completion = {
val delay = if (duration == NotificationDuration.LONG) 3.0 else 1.5
NSTimer.scheduledTimerWithTimeInterval(delay, repeats = false) {
alertController.dismissViewControllerAnimated(true, completion = null)
alertController.addAction(
UIAlertAction.actionWithTitle(
"OK",
style = UIAlertActionStyleDefault,
handler = null
)
)

dispatch_async(dispatch_get_main_queue()) {
viewController.presentViewController(alertController, animated = true, completion = {
val delay = if (duration == NotificationDuration.LONG) 3.0 else 1.5
NSTimer.scheduledTimerWithTimeInterval(delay, repeats = false) {
alertController.dismissViewControllerAnimated(true, completion = null)
}
})
}
})
} else {
NSLog("Error: rootViewController is null")
}
}

actual fun createNotification(type: NotificationType): Notification = when (type) {
NotificationType.ALERT -> object : Notification() {
override fun show(message: String) {
Expand Down

0 comments on commit 0e92a71

Please sign in to comment.