Skip to content

Commit

Permalink
adding toast in ios and desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
kapilyadav committed Aug 7, 2023
1 parent bd392b5 commit d9d2de5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
7 changes: 6 additions & 1 deletion shared/src/commonMain/kotlin/screens/EmailBodyScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import cafe.adriel.voyager.core.screen.ScreenKey
import cafe.adriel.voyager.navigator.LocalNavigator
import components.MailView
import downloadFile
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import model.EmailBody
import model.EmailMessage
Expand All @@ -31,6 +32,7 @@ import org.jetbrains.compose.resources.painterResource
import platformName
import res.whiteColor
import showToast
import utils.Utils.TOAST_TIMER
import utils.Utils.platformNameAndroid
import viewModels.AppViewModel

Expand All @@ -50,7 +52,10 @@ class EmailBodyScreen(private val body: EmailMessage) : Screen {

if (showToast.isNotEmpty()) {
showToast(showToast)
showToast = ""
scope.launch {
if(platformName != platformNameAndroid) delay(TOAST_TIMER)
showToast=""
}
}

Column(
Expand Down
7 changes: 6 additions & 1 deletion shared/src/commonMain/kotlin/screens/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import components.CtaIconButtonActions
import components.EmailBox
import components.MailView
import exitApp
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import model.DialogProps
import model.EmailBody
Expand All @@ -44,6 +45,7 @@ import res.whiteColor
import showDialog
import showToast
import utils.Utils
import utils.Utils.TOAST_TIMER
import utils.Utils.platformNameAndroid
import viewModels.AppViewModel
import viewModels.UiState
Expand All @@ -69,7 +71,10 @@ class HomeScreen : Screen {

if (showToast.isNotEmpty()) {
showToast(showToast)
showToast=""
scope.launch {
if(platformName != platformNameAndroid) delay(TOAST_TIMER)
showToast=""
}
}
/*
* Handling error state using dialog box implemented on each platforms
Expand Down
46 changes: 46 additions & 0 deletions shared/src/commonMain/kotlin/utils/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
package utils

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup
import appFont
import res.whiteColor
import kotlin.time.Duration.Companion.seconds

object Utils {
const val platformNameAndroid = "android"
const val platformNameIOS = "ios"
const val platformNameDesktop = "desktop"
val TOAST_TIMER = 2.seconds

fun String.getNameFromEmail(): String {
val atIndex = indexOf('@')
Expand Down Expand Up @@ -31,4 +50,31 @@ object Utils {
fun Logger(msg: String, isDebug: Boolean = true) {
if (isDebug) println("Kapil ke logs. Message hai $msg")
}
@Composable
fun Toast(message: String) {
Popup {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.BottomCenter
) {
Surface(
modifier = Modifier.padding(16.dp),
elevation = 8.dp,
shape = RoundedCornerShape(8.dp),
color = Color.Black.copy(alpha = 0.8f)
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Spacer(modifier = Modifier.width(2.dp))
Icon(imageVector = Icons.Filled.Notifications, modifier = Modifier.size(24.dp), contentDescription = "", tint = whiteColor)
Text(
text = message,
color = Color.White,
modifier = Modifier.padding(vertical = 8.dp, horizontal = 12.dp),
style = TextStyle(fontFamily = appFont)
)
}
}
}
}
}
}
3 changes: 2 additions & 1 deletion shared/src/desktopMain/kotlin/Platform.desktop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import model.DialogProps
import utils.Utils.Toast
import utils.Utils.platformNameDesktop
import java.io.BufferedInputStream
import java.io.File
Expand All @@ -23,7 +24,7 @@ actual val appFont: FontFamily

@Composable
actual fun showToast(msg: String) {
JOptionPane.showMessageDialog(null, msg)
Toast(msg)
}

actual val platformName: String = platformNameDesktop
Expand Down
3 changes: 2 additions & 1 deletion shared/src/iosMain/kotlin/Platform.ios.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.jetbrains.compose.resources.resource
import platform.Foundation.*
import platform.UIKit.*
import platform.posix.exit
import utils.Utils
import utils.Utils.platformNameIOS

actual val appFont: FontFamily = FontFamily(
Expand All @@ -20,7 +21,7 @@ actual val appFont: FontFamily = FontFamily(

@Composable
actual fun showToast(msg: String) {
showAlert(msg, "", DialogProps("ok") {}, null)
Utils.Toast(msg)
}

@OptIn(ExperimentalResourceApi::class)
Expand Down

0 comments on commit d9d2de5

Please sign in to comment.