Skip to content

Commit

Permalink
[FIX] Fixed some minor issues with Toast.kt.
Browse files Browse the repository at this point in the history
 - Instead of dismissing for backpressure; it only collapses when backpressure.
 - Removed action from toast of SystemFacade; because it is just to send messages not receive results.

[FEAT] Added promo-message
 - Added promo-message for Audiofy - media player; the message shows at multiples of 5 launch counts.
  • Loading branch information
iZakirSheikh committed Sep 17, 2024
1 parent ff38b24 commit f6bde4c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 45 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId = "com.googol.android.apps.photos"
minSdk = 24
targetSdk = 35
versionCode = 21
versionName = "0.1.0-dev21"
versionCode = 22
versionName = "0.1.0-dev22"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
33 changes: 25 additions & 8 deletions app/src/main/java/com/zs/gallery/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Downloading
import androidx.compose.material.icons.outlined.GetApp
import androidx.compose.material.icons.outlined.NewReleases
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
Expand Down Expand Up @@ -65,6 +66,7 @@ import com.google.firebase.analytics.analytics
import com.google.firebase.analytics.logEvent
import com.google.firebase.crashlytics.crashlytics
import com.primex.core.MetroGreen
import com.primex.core.Rose
import com.primex.core.getText2
import com.primex.core.runCatching
import com.primex.preferences.Key
Expand All @@ -82,6 +84,7 @@ import com.zs.gallery.common.getPackageInfoCompat
import com.zs.gallery.files.RouteTimeline
import com.zs.gallery.lockscreen.RouteLockScreen
import com.zs.gallery.settings.Settings
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
Expand Down Expand Up @@ -131,7 +134,7 @@ class MainActivity : ComponentActivity(), SystemFacade, NavController.OnDestinat
private val preferences: Preferences by inject()
private var navController: NavHostController? = null
var inAppUpdateProgress by mutableFloatStateOf(Float.NaN)
private set
private set

/**
* Timestamp (mills) indicating when the app last went to the background.
Expand Down Expand Up @@ -335,27 +338,23 @@ class MainActivity : ComponentActivity(), SystemFacade, NavController.OnDestinat
message: CharSequence,
icon: ImageVector?,
accent: Color,
action: CharSequence?,
duration: Int
) {
lifecycleScope.launch {
toastHostState.showToast(message, action, icon, accent, duration)
toastHostState.showToast(message, null, icon, accent, duration)
}
}

override fun showToast(
message: Int,
icon: ImageVector?,
accent: Color,
action: Int,
duration: Int
) {
lifecycleScope.launch {
val action =
if (action == ResourcesCompat.ID_NULL) null else resources.getText2(action)
toastHostState.showToast(
resources.getText2(id = message),
action,
null,
icon,
accent,
duration
Expand Down Expand Up @@ -399,10 +398,28 @@ class MainActivity : ComponentActivity(), SystemFacade, NavController.OnDestinat
// show what's new message on click.
val versionCode = BuildConfig.VERSION_CODE
val savedVersionCode = preferences.value(KEY_APP_VERSION_CODE)
if (savedVersionCode != versionCode){
if (savedVersionCode != versionCode) {
preferences[KEY_APP_VERSION_CODE] = versionCode
showToast(R.string.what_s_new_latest, duration = Toast.DURATION_INDEFINITE)
}

// promote media player
// TODO - properly handle promotional content.
lifecycleScope.launch {
val counter = preferences.value(Settings.KEY_LAUNCH_COUNTER)
if (counter > 0 && counter % 5 == 0) {
delay(3000)
val result = toastHostState.showToast(
message = resources.getText2(R.string.msg_media_player_promotion),
icon = Icons.Outlined.NewReleases,
duration = Toast.DURATION_INDEFINITE,
action = resources.getText2(R.string.get),
accent = Color.Rose
)
if (result == Toast.ACTION_PERFORMED)
launchAppStore("com.prime.player")
}
}
}

override fun launchUpdateFlow(report: Boolean) {
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/java/com/zs/gallery/common/SystemFacade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,24 @@ private const val PKG_MARKET_ID = "com.android.vending"

interface SystemFacade {

/**
* @see com.zs.foundation.toast.ToastHostState.showToast
*/
fun showToast(
message: CharSequence,
icon: ImageVector? = null,
accent: Color = Color.Unspecified,
action: CharSequence? = null,
@Duration duration: Int = if (action == null) Toast.DURATION_SHORT else Toast.DURATION_INDEFINITE,
@Duration duration: Int = Toast.DURATION_SHORT,
)

/**
* @see com.zs.foundation.toast.ToastHostState.showToast
*/
fun showToast(
@StringRes message: Int,
icon: ImageVector? = null,
accent: Color = Color.Unspecified,
@StringRes action: Int = ResourcesCompat.ID_NULL,
@Duration duration: Int = if (action == ResourcesCompat.ID_NULL) Toast.DURATION_SHORT else Toast.DURATION_INDEFINITE,
@Duration duration: Int = Toast.DURATION_SHORT,
)

/**
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/com/zs/gallery/files/Timeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import androidx.compose.material.icons.outlined.Close
import androidx.compose.material.icons.outlined.DeleteOutline
import androidx.compose.material.icons.outlined.Favorite
import androidx.compose.material.icons.outlined.HotelClass
import androidx.compose.material.icons.outlined.NewReleases
import androidx.compose.material.icons.outlined.Recycling
import androidx.compose.material.icons.outlined.SelectAll
import androidx.compose.material.icons.outlined.Share
Expand Down Expand Up @@ -83,6 +84,7 @@ import com.zs.foundation.adaptive.TwoPane
import com.zs.foundation.adaptive.contentInsets
import com.zs.foundation.sharedBounds
import com.zs.foundation.sharedElement
import com.zs.foundation.toast.Toast
import com.zs.gallery.R
import com.zs.gallery.bin.RouteTrash
import com.zs.gallery.common.FloatingActionMenu
Expand All @@ -105,11 +107,14 @@ private fun TopAppBar(
) {
LargeTopAppBar(
navigationIcon = {
Icon(
val facade = LocalSystemFacade.current
IconButton(
painter = painterResource(id = R.drawable.ic_app),
contentDescription = null,
tint = Color.Unspecified,
modifier = Modifier.padding(AppTheme.padding.normal)
tint = null,
onClick = {
facade.showToast(R.string.what_s_new_latest, Icons.Outlined.NewReleases, duration = Toast.DURATION_INDEFINITE)
}
)
},
title = { Text(text = textResource(id = R.string.timeline)) },
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,9 @@
<string name="pref_app_lock">App Lock &beta;</string>
<string name="pref_app_lock_summery"><b>Unlock with biometrics</b>\nWhen enabled, you\'ll need to use the fingerprint, face or other unique identifiers to open &appname;.</string>
<string name="msg_favourites_updated">Favorites updated!</string>
<string name="msg_media_player_promotion"><b>New Media Player App!</b>
<font color ='gray'>\n🎶 Check out our new <b>Media Player!</b> Enjoy your favorite tunes and videos effortlessly. Give it a try! 🎥</font>
</string>
<string name="get">Get</string>

</resources>
65 changes: 37 additions & 28 deletions foundation/src/main/java/com/zs/foundation/toast/Toast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.graphics.vector.ImageVector
Expand All @@ -54,9 +58,9 @@ import androidx.compose.ui.platform.AccessibilityManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.primex.core.ImageBrush
import com.primex.core.MetroGreen
import com.primex.core.SignalWhite
import com.primex.core.composableOrNull
import com.primex.core.thenIf
import com.primex.core.verticalFadingEdge
import com.primex.core.visualEffect
import com.primex.material2.Button
Expand All @@ -65,7 +69,9 @@ import com.primex.material2.ListTile
import com.primex.material2.TextButton
import com.zs.foundation.AppTheme
import com.zs.foundation.Colors
import com.zs.foundation.ContentPadding
import com.zs.foundation.renderInSharedTransitionScopeOverlay
import com.zs.foundation.thenIf
import kotlinx.coroutines.CancellableContinuation
import kotlin.coroutines.resume

Expand Down Expand Up @@ -197,7 +203,7 @@ internal fun Toast.toMillis(
}


private val EXPANDED_TOAST_SHAPE = RoundedCornerShape(10)
private val EXPANDED_TOAST_SHAPE = RoundedCornerShape(8)
private val TOAST_SHAPE = RoundedCornerShape(16)

private inline val Colors.toastBackgroundColor
Expand Down Expand Up @@ -228,7 +234,7 @@ internal fun Toast(
// State to track if Toast is expanded
var isExpanded: Boolean by remember { mutableStateOf(false) }
// Handle back press to dismiss expanded Toast or the entire Toast
BackHandler { if (isExpanded) isExpanded = false else value.dismiss() }
BackHandler(isExpanded) { isExpanded = !isExpanded }
// State for swipe-to-dismiss gesture
val dismissState = rememberDismissState(
confirmStateChange = {
Expand All @@ -242,28 +248,35 @@ internal fun Toast(
SwipeToDismiss(
dismissState,
background = {},
modifier = modifier.renderInSharedTransitionScopeOverlay(1.0f),
modifier = modifier
.animateContentSize()
.renderInSharedTransitionScopeOverlay(1.0f),
dismissContent = {
// Shape of the Toast based on expanded state
val shape = if (isExpanded) EXPANDED_TOAST_SHAPE else TOAST_SHAPE
val shape = if (isExpanded) EXPANDED_TOAST_SHAPE else AppTheme.shapes.small
ListTile(
shape = shape,
color = backgroundColor,
onColor = contentColor,
modifier = Modifier
.padding(horizontal = 16.dp)
.padding(horizontal = 18.dp)
// Size constraints for the Toast
.shadow(6.dp, shape)
.sizeIn(360.dp, 56.dp, 400.dp, 340.dp)
// Toggle expanded state on click
.clickable(indication = null, interactionSource = null) {
isExpanded = !isExpanded
}
.animateContentSize()
// Apply border and visual effect if dark theme
.thenIf(!AppTheme.colors.isLight) {
border(0.7.dp, Color.SignalWhite.copy(0.08f), shape)
.visualEffect(ImageBrush.NoiseBrush, 0.2f, overlay = true)
},
.thenIf(!isExpanded){
drawWithContent {
drawContent()
drawRect(color = actionColor, size = size.copy(width = 3.dp.toPx()))
}
}
//.border(0.7.dp, Color.SignalWhite.copy(0.08f), shape)
.visualEffect(ImageBrush.NoiseBrush, 0.60f, overlay = true)
,
leading = composableOrNull(value.icon != null) {
// FixMe: It might cause problems.
val icon = value.icon!!
Expand All @@ -290,10 +303,10 @@ internal fun Toast(
color = contentColor,
style = AppTheme.typography.bodyMedium,
// Limit lines when not expanded
maxLines = if (!isExpanded) 2 else Int.MAX_VALUE,
maxLines = if (!isExpanded) 3 else Int.MAX_VALUE,
modifier = Modifier
// Max height constraint
.heightIn(max = 185.dp)
.heightIn(max = 195.dp)
.thenIf(isExpanded) {
val state = rememberScrollState()
verticalFadingEdge(backgroundColor, state, 16.dp)
Expand All @@ -304,32 +317,28 @@ internal fun Toast(
// Footer with action buttons when expanded
footer = composableOrNull(isExpanded) {
Row(
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth(),
modifier = Modifier.thenIf (value.icon != null) { padding(start = 20.dp) },
content = {
// Cancel button
TextButton(
stringResource(android.R.string.cancel),
value::dismiss,
modifier = Modifier.scale(0.9f),
colors = ButtonDefaults.textButtonColors(contentColor = contentColor),
shape = AppTheme.shapes.compact
)
// Action button if available
val action = value.action
if (action != null)
Button(
TextButton(
label = action,
onClick = value::action,
colors = ButtonDefaults.buttonColors(
contentColor = actionColor,
backgroundColor = actionColor.copy(0.12f)
),
colors = ButtonDefaults.buttonColors(contentColor = actionColor),
modifier = Modifier.scale(0.9f),
shape = AppTheme.shapes.compact,
elevation = null
)
// Cancel button
TextButton(
stringResource(android.R.string.cancel).uppercase(),
value::dismiss,
modifier = Modifier.scale(0.9f),
colors = ButtonDefaults.textButtonColors(contentColor = contentColor),
shape = AppTheme.shapes.compact
)
}
)
}
Expand Down

0 comments on commit f6bde4c

Please sign in to comment.