@@ -2,7 +2,9 @@ package com.neki.android.core.ui.toast
22
33import android.content.Context
44import android.content.res.Resources
5+ import android.graphics.PixelFormat
56import android.view.Gravity
7+ import android.view.WindowManager
68import android.widget.Toast
79import androidx.activity.ComponentActivity
810import androidx.annotation.DrawableRes
@@ -13,32 +15,48 @@ import androidx.compose.runtime.Composable
1315import androidx.compose.ui.Modifier
1416import androidx.compose.ui.platform.ComposeView
1517import androidx.compose.ui.unit.dp
18+ import androidx.lifecycle.lifecycleScope
1619import androidx.lifecycle.setViewTreeLifecycleOwner
1720import androidx.lifecycle.setViewTreeViewModelStoreOwner
1821import androidx.savedstate.setViewTreeSavedStateRegistryOwner
1922import com.neki.android.core.designsystem.R
2023import com.neki.android.core.designsystem.toast.NekiActionToast
2124import com.neki.android.core.designsystem.toast.NekiToast
2225import com.neki.android.core.designsystem.ui.theme.NekiTheme
26+ import kotlinx.coroutines.Dispatchers
27+ import kotlinx.coroutines.Job
28+ import kotlinx.coroutines.delay
29+ import kotlinx.coroutines.launch
2330
2431class NekiToast (
2532 private val context : Context ,
26- ) : Toast(context) {
27- private fun makeText (
28- duration : Int = LENGTH_SHORT ,
29- toast : @Composable () -> Unit ,
33+ ) {
34+ private val windowManager = context.getSystemService(Context .WINDOW_SERVICE ) as WindowManager
35+
36+ private fun makeToast (
37+ duration : Int = Toast .LENGTH_SHORT ,
38+ toast : @Composable (dismiss: () -> Unit ) -> Unit ,
3039 ) {
3140 val activity = context as ComponentActivity
41+ var dismissJob: Job ? = null
42+ lateinit var composeView: ComposeView
43+
44+ fun dismiss () {
45+ dismissJob?.cancel()
46+ if (composeView.isAttachedToWindow) {
47+ windowManager.removeView(composeView)
48+ }
49+ }
3250
33- val composeView = ComposeView (context).apply {
51+ composeView = ComposeView (context).apply {
3452 setContent {
3553 NekiTheme {
3654 Box (
3755 modifier = Modifier
3856 .fillMaxWidth()
3957 .padding(horizontal = 20 .dp),
4058 ) {
41- toast()
59+ toast { dismiss() }
4260 }
4361 }
4462 }
@@ -48,25 +66,32 @@ class NekiToast(
4866 setViewTreeViewModelStoreOwner(activity)
4967 }
5068
51- this .duration = duration
52- view = composeView
53- setGravity(
54- Gravity .FILL_HORIZONTAL or Gravity .BOTTOM ,
55- 0 ,
56- (28 * Resources .getSystem().displayMetrics.density).toInt(),
57- )
69+ val params = WindowManager .LayoutParams (
70+ WindowManager .LayoutParams .MATCH_PARENT ,
71+ WindowManager .LayoutParams .WRAP_CONTENT ,
72+ WindowManager .LayoutParams .TYPE_APPLICATION ,
73+ WindowManager .LayoutParams .FLAG_NOT_FOCUSABLE ,
74+ PixelFormat .TRANSLUCENT ,
75+ ).apply {
76+ windowAnimations = android.R .style.Animation_Toast
77+ gravity = Gravity .BOTTOM or Gravity .FILL_HORIZONTAL
78+ y = (28 * Resources .getSystem().displayMetrics.density).toInt()
79+ }
80+
81+ windowManager.addView(composeView, params)
5882
59- show()
83+ dismissJob = activity.lifecycleScope.launch(Dispatchers .Main ) {
84+ delay(if (duration == Toast .LENGTH_SHORT ) 2500L else 3500L )
85+ dismiss()
86+ }
6087 }
6188
6289 fun showToast (
6390 text : String ,
6491 @DrawableRes iconRes : Int = R .drawable.icon_checkbox_on,
65- duration : Int = LENGTH_SHORT ,
92+ duration : Int = Toast . LENGTH_SHORT ,
6693 ) {
67- makeText(
68- duration = duration,
69- ) {
94+ makeToast(duration = duration) {
7095 NekiToast (
7196 iconRes = iconRes,
7297 text = text,
@@ -75,20 +100,21 @@ class NekiToast(
75100 }
76101
77102 fun showActionToast (
78- @DrawableRes iconRes : Int ,
79103 text : String ,
80104 buttonText : String ,
105+ @DrawableRes iconRes : Int = R .drawable.icon_checkbox_on,
106+ duration : Int = Toast .LENGTH_SHORT ,
81107 onClickActionButton : () -> Unit ,
82- duration : Int = LENGTH_SHORT ,
83108 ) {
84- makeText(
85- duration = duration,
86- ) {
109+ makeToast(duration = duration) { dismiss ->
87110 NekiActionToast (
88111 iconRes = iconRes,
89112 text = text,
90113 buttonText = buttonText,
91- onClickActionButton = onClickActionButton,
114+ onClickActionButton = {
115+ dismiss()
116+ onClickActionButton()
117+ },
92118 )
93119 }
94120 }
0 commit comments