Skip to content

Commit

Permalink
Merge pull request #4 from TalhaFaki/refactor
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
tekinarslan authored Dec 3, 2021
2 parents 1e6b2ba + 556b339 commit 0732579
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,28 @@ import com.talhafaki.composablesweettoast.util.SweetToastUtil

class SweetToast(context: Context) : Toast(context) {

companion object {
@Composable
fun MakeTest(
message: String,
duration: Int = LENGTH_SHORT,
type: SweetToastProperty
): SweetToast {
val context = LocalContext.current
@Composable
fun MakeTest(
message: String,
duration: Int = LENGTH_LONG,
type: SweetToastProperty
) {
val context = LocalContext.current

val sweetToast = SweetToast(context)
val views = ComposeView(context)
views.setContent {
SweetToastUtil.SetView(
messageTxt = message,
resourceIcon = type.getResourceId(),
backgroundColor = type.getBackgroundColor()
)
}
val views = ComposeView(context)
views.setContent {
SweetToastUtil.SetView(
messageTxt = message,
resourceIcon = type.getResourceId(),
backgroundColor = type.getBackgroundColor()
)
}

ViewTreeLifecycleOwner.set(views, LocalLifecycleOwner.current)
ViewTreeViewModelStoreOwner.set(views, LocalViewModelStoreOwner.current)
ViewTreeSavedStateRegistryOwner.set(views, LocalSavedStateRegistryOwner.current)
ViewTreeLifecycleOwner.set(views, LocalLifecycleOwner.current)
ViewTreeViewModelStoreOwner.set(views, LocalViewModelStoreOwner.current)
ViewTreeSavedStateRegistryOwner.set(views, LocalSavedStateRegistryOwner.current)

sweetToast.duration = duration
sweetToast.view = views
return sweetToast
}
this.duration = duration
this.view = views
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.talhafaki.composablesweettoast.util

import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
Expand All @@ -12,6 +11,7 @@ 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.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
Expand All @@ -27,43 +27,31 @@ import com.talhafaki.composablesweettoast.toasttypes.Warning
object SweetToastUtil {

@Composable
fun SweetSuccess(message: String) {
val sweetToast = SweetToast.MakeTest(
message,
duration = Toast.LENGTH_LONG,
type = Success()
)
sweetToast.show()
fun SweetSuccess(message: String, duration: Int = Toast.LENGTH_LONG) {
val sweetSuccessToast = SweetToast(LocalContext.current)
sweetSuccessToast.MakeTest(message = message, duration = duration, type = Success())
sweetSuccessToast.show()
}

@Composable
fun SweetError(message: String) {
val sweetToast = SweetToast.MakeTest(
message,
duration = Toast.LENGTH_LONG,
type = Error()
)
sweetToast.show()
fun SweetError(message: String, duration: Int = Toast.LENGTH_LONG) {
val sweetErrorToast = SweetToast(LocalContext.current)
sweetErrorToast.MakeTest(message = message, duration = duration, type = Error())
sweetErrorToast.show()
}

@Composable
fun SweetInfo(message: String) {
val sweetToast = SweetToast.MakeTest(
message,
duration = Toast.LENGTH_LONG,
type = Info()
)
sweetToast.show()
fun SweetInfo(message: String, duration: Int = Toast.LENGTH_LONG) {
val sweetInfoToast = SweetToast(LocalContext.current)
sweetInfoToast.MakeTest(message = message, duration = duration, type = Info())
sweetInfoToast.show()
}

@Composable
fun SweetWarning(message: String) {
val sweetToast = SweetToast.MakeTest(
message,
duration = Toast.LENGTH_LONG,
type = Warning()
)
sweetToast.show()
fun SweetWarning(message: String, duration: Int = Toast.LENGTH_LONG) {
val sweetWarningToast = SweetToast(LocalContext.current)
sweetWarningToast.MakeTest(message = message, duration = duration, type = Warning())
sweetWarningToast.show()
}

@Composable
Expand Down

0 comments on commit 0732579

Please sign in to comment.