Skip to content

Commit

Permalink
Update announcements to use swipe to dismiss
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperDragonXD committed Jul 26, 2024
1 parent c36e06c commit fb92d55
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,32 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.Launch
import androidx.compose.material.icons.rounded.Close
import androidx.compose.material.icons.rounded.NewReleases
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.SwipeToDismissBox
import androidx.compose.material3.SwipeToDismissBoxValue
import androidx.compose.material3.Text
import androidx.compose.material3.rememberSwipeToDismissBoxState
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand All @@ -40,6 +43,7 @@ import app.lawnchair.ui.preferences.data.liveinfo.liveInformationManager
import app.lawnchair.ui.preferences.data.liveinfo.model.Announcement
import app.lawnchair.ui.util.addIf
import com.android.launcher3.BuildConfig
import com.android.launcher3.R
import kotlinx.collections.immutable.ImmutableList

@Composable
Expand All @@ -66,7 +70,7 @@ fun AnnouncementPreference(
modifier = modifier,
) {
announcements.forEachIndexed { index, announcement ->
var show by remember { mutableStateOf(true) }
var show by rememberSaveable { mutableStateOf(true) }
AnnouncementItem(show, announcement) { show = false }
if (index != announcements.lastIndex && show && (!announcement.test || BuildConfig.DEBUG)) {
Spacer(modifier = Modifier.height(16.dp))
Expand Down Expand Up @@ -103,13 +107,60 @@ private fun AnnouncementItemContent(
modifier: Modifier = Modifier,
onClose: () -> Unit,
) {
Surface(
modifier = modifier
.padding(16.dp, 0.dp, 16.dp, 0.dp),
shape = MaterialTheme.shapes.large,
color = MaterialTheme.colorScheme.surfaceVariant,
val state = rememberSwipeToDismissBoxState(
confirmValueChange = {
when (it) {
SwipeToDismissBoxValue.StartToEnd -> {
onClose()
}
SwipeToDismissBoxValue.EndToStart -> return@rememberSwipeToDismissBoxState false
SwipeToDismissBoxValue.Settled -> return@rememberSwipeToDismissBoxState false
}
return@rememberSwipeToDismissBoxState true
},
)

SwipeToDismissBox(
state = state,
enableDismissFromEndToStart = false,
backgroundContent = {
Surface(
modifier = modifier
.alpha(
if (state.dismissDirection != SwipeToDismissBoxValue.StartToEnd) 1f else calculateAlpha(state.progress),
)
.fillMaxSize()
.padding(16.dp, 0.dp, 16.dp, 0.dp),
shape = MaterialTheme.shapes.large,
color = MaterialTheme.colorScheme.surfaceColorAtElevation(1.dp),
) {
PreferenceTemplate(
{},
description = {
Text(stringResource(R.string.accessibility_close))
},
)
}
},
) {
AnnouncementPreferenceItemContent(text = text, url = url, onClose = onClose)
Surface(
modifier = modifier
.alpha(
if (state.dismissDirection != SwipeToDismissBoxValue.StartToEnd) 1f else calculateAlpha(state.progress),
)
.padding(16.dp, 0.dp, 16.dp, 0.dp),
shape = MaterialTheme.shapes.large,
color = MaterialTheme.colorScheme.surfaceVariant,
) {
AnnouncementPreferenceItemContent(text = text, url = url)
}
}
}

private fun calculateAlpha(progress: Float): Float {
return when {
progress < 0.5f -> 1f // Fully opaque until halfway
else -> 1f - (progress - 0.5f) * 2 // Fade out linearly from halfway to the end
}
}

Expand All @@ -118,7 +169,6 @@ private fun AnnouncementPreferenceItemContent(
text: String,
url: String?,
modifier: Modifier = Modifier,
onClose: (() -> Unit)?,
) {
val context = LocalContext.current
val hasLink = !url.isNullOrBlank()
Expand Down Expand Up @@ -165,19 +215,6 @@ private fun AnnouncementPreferenceItemContent(
}

Spacer(modifier = Modifier.width(8.dp))

if (onClose != null) {
IconButton(
onClick = onClose,
modifier = Modifier.size(16.dp).offset(x = (8).dp, y = (-16).dp),
) {
Icon(
imageVector = Icons.Rounded.Close,
tint = MaterialTheme.colorScheme.surfaceTint,
contentDescription = null,
)
}
}
}
},
)
Expand All @@ -189,7 +226,6 @@ private fun InfoPreferenceWithoutLinkPreview() {
AnnouncementPreferenceItemContent(
text = "Very important announcement ",
url = "",
onClose = null,
)
}

Expand All @@ -199,6 +235,5 @@ private fun InfoPreferenceWithLinkPreview() {
AnnouncementPreferenceItemContent(
text = "Very important announcement with a very important link",
url = "https://lawnchair.app/",
onClose = null,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.serialization.Serializable

@Serializable
data class LiveInformation(
private val version: Int,
private val announcements: List<Announcement>,
) {

Expand All @@ -14,6 +15,7 @@ data class LiveInformation(

companion object {
val default = LiveInformation(
version = 1,
announcements = emptyList(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fun PreferencesOverflowMenu(
Modifier
.clip(highlightShape)
.background(highlightColor)
}
},
)
}
val openRestoreBackup = restoreBackupOpener()
Expand Down

0 comments on commit fb92d55

Please sign in to comment.