diff --git a/app/src/main/java/com/android/composablesweettoast/MainActivity.kt b/app/src/main/java/com/android/composablesweettoast/MainActivity.kt index 0d38fb2..e16eb81 100644 --- a/app/src/main/java/com/android/composablesweettoast/MainActivity.kt +++ b/app/src/main/java/com/android/composablesweettoast/MainActivity.kt @@ -4,14 +4,13 @@ import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import com.android.composablesweettoast.ui.theme.ComposableSweetToastTheme -import com.talhafaki.composablesweettoast.util.SweetToastUtil.SweetSuccess class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { ComposableSweetToastTheme { - SweetSuccess(message = "Success Text!") + MainScreen() } } } diff --git a/app/src/main/java/com/android/composablesweettoast/MainScreen.kt b/app/src/main/java/com/android/composablesweettoast/MainScreen.kt new file mode 100644 index 0000000..429c484 --- /dev/null +++ b/app/src/main/java/com/android/composablesweettoast/MainScreen.kt @@ -0,0 +1,25 @@ +package com.android.composablesweettoast + +import androidx.compose.material.Button +import androidx.compose.material.Text +import androidx.compose.runtime.* +import com.talhafaki.composablesweettoast.util.SweetToastUtil.SweetSuccess + +/** + * Created by stekinarslan on 02,December,2021 + */ + +@Composable +fun MainScreen() { + var openDialogSuccess by remember { mutableStateOf(false) } + + Button(onClick = { + openDialogSuccess = true + }) { + Text("Sweet Toast Success") + } + + if (openDialogSuccess) { + SweetSuccess(message = "Success Text!") + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 98ccb0e..be6eded 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,7 @@ buildscript { ext { compose_version = '1.0.5' + lifecycle_version = "2.4.0" } repositories { google() diff --git a/composablesweettoast/build.gradle b/composablesweettoast/build.gradle index 12ce38d..ab7b485 100644 --- a/composablesweettoast/build.gradle +++ b/composablesweettoast/build.gradle @@ -35,6 +35,7 @@ android { dependencies { implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.material:material:$compose_version" + implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version" implementation "androidx.compose.compiler:compiler:1.1.0-beta04" implementation 'androidx.activity:activity-ktx:1.4.0' testImplementation 'junit:junit:4.13.2' diff --git a/composablesweettoast/src/main/java/com/talhafaki/composablesweettoast/main/SweetToast.kt b/composablesweettoast/src/main/java/com/talhafaki/composablesweettoast/main/SweetToast.kt index 17ccd2d..6a1c65a 100644 --- a/composablesweettoast/src/main/java/com/talhafaki/composablesweettoast/main/SweetToast.kt +++ b/composablesweettoast/src/main/java/com/talhafaki/composablesweettoast/main/SweetToast.kt @@ -2,24 +2,25 @@ package com.talhafaki.composablesweettoast.main import android.content.Context import android.widget.Toast -import androidx.activity.ComponentActivity import androidx.compose.runtime.Composable -import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.platform.* import androidx.lifecycle.* +import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner import androidx.savedstate.ViewTreeSavedStateRegistryOwner import com.talhafaki.composablesweettoast.util.SweetToastProperty import com.talhafaki.composablesweettoast.util.SweetToastUtil -class SweetToast(mContext: Context) : Toast(mContext) { +class SweetToast(context: Context) : Toast(context) { companion object { @Composable - fun makeTest( - context: ComponentActivity, + fun MakeTest( message: String, duration: Int = LENGTH_SHORT, type: SweetToastProperty ): SweetToast { + val context = LocalContext.current + val sweetToast = SweetToast(context) val views = ComposeView(context) views.setContent { @@ -29,9 +30,11 @@ class SweetToast(mContext: Context) : Toast(mContext) { backgroundColor = type.getBackgroundColor() ) } - ViewTreeLifecycleOwner.set(views, context) - ViewTreeViewModelStoreOwner.set(views, context) - ViewTreeSavedStateRegistryOwner.set(views, context) + + ViewTreeLifecycleOwner.set(views, LocalLifecycleOwner.current) + ViewTreeViewModelStoreOwner.set(views, LocalViewModelStoreOwner.current) + ViewTreeSavedStateRegistryOwner.set(views, LocalSavedStateRegistryOwner.current) + sweetToast.duration = duration sweetToast.view = views return sweetToast diff --git a/composablesweettoast/src/main/java/com/talhafaki/composablesweettoast/util/SweetToastUtil.kt b/composablesweettoast/src/main/java/com/talhafaki/composablesweettoast/util/SweetToastUtil.kt index 2f7b12e..268ec11 100644 --- a/composablesweettoast/src/main/java/com/talhafaki/composablesweettoast/util/SweetToastUtil.kt +++ b/composablesweettoast/src/main/java/com/talhafaki/composablesweettoast/util/SweetToastUtil.kt @@ -25,10 +25,10 @@ import com.talhafaki.composablesweettoast.toasttypes.Success import com.talhafaki.composablesweettoast.toasttypes.Warning object SweetToastUtil { + @Composable - fun ComponentActivity.SweetSuccess(message: String) { - val sweetToast = SweetToast.makeTest( - this, + fun SweetSuccess(message: String) { + val sweetToast = SweetToast.MakeTest( message, duration = Toast.LENGTH_LONG, type = Success() @@ -37,9 +37,8 @@ object SweetToastUtil { } @Composable - fun ComponentActivity.SweetError(message: String) { - val sweetToast = SweetToast.makeTest( - this, + fun SweetError(message: String) { + val sweetToast = SweetToast.MakeTest( message, duration = Toast.LENGTH_LONG, type = Error() @@ -48,9 +47,8 @@ object SweetToastUtil { } @Composable - fun ComponentActivity.SweetInfo(message: String) { - val sweetToast = SweetToast.makeTest( - this, + fun SweetInfo(message: String) { + val sweetToast = SweetToast.MakeTest( message, duration = Toast.LENGTH_LONG, type = Info() @@ -59,9 +57,8 @@ object SweetToastUtil { } @Composable - fun ComponentActivity.SweetWarning(message: String) { - val sweetToast = SweetToast.makeTest( - this, + fun SweetWarning(message: String) { + val sweetToast = SweetToast.MakeTest( message, duration = Toast.LENGTH_LONG, type = Warning()