Skip to content

Commit

Permalink
Merge pull request #3 from TalhaFaki/feature/architecture_changes
Browse files Browse the repository at this point in the history
normal functions update with composable
  • Loading branch information
tfaki authored Dec 3, 2021
2 parents b4e972b + acc404b commit 1e6b2ba
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/com/android/composablesweettoast/MainScreen.kt
Original file line number Diff line number Diff line change
@@ -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!")
}
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
buildscript {
ext {
compose_version = '1.0.5'
lifecycle_version = "2.4.0"
}
repositories {
google()
Expand Down
1 change: 1 addition & 0 deletions composablesweettoast/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 1e6b2ba

Please sign in to comment.