From 6252a5b18c72aa37bf1fdbbf078dcddecffd5069 Mon Sep 17 00:00:00 2001 From: Shabinder Singh Date: Sat, 29 May 2021 18:32:38 +0530 Subject: [PATCH] Analytics Row and Toggle Added --- .../com/shabinder/spotiflyer/MainActivity.kt | 2 +- .../spotiflyer/ui/AnalyticsDialog.kt | 47 +++++++++++-------- .../common/uikit/SpotiFlyerMainUi.kt | 42 ++++++++++++++++- .../kotlin/com/shabinder/common/di/Dir.kt | 2 +- .../shabinder/common/main/SpotiFlyerMain.kt | 8 +++- .../main/integration/SpotiFlyerMainImpl.kt | 7 ++- .../common/main/store/SpotiFlyerMainStore.kt | 1 + .../main/store/SpotiFlyerMainStoreProvider.kt | 13 ++++- 8 files changed, 96 insertions(+), 26 deletions(-) diff --git a/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt b/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt index 33ff834b..856a4a94 100644 --- a/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt +++ b/android/src/main/java/com/shabinder/spotiflyer/MainActivity.kt @@ -125,7 +125,7 @@ class MainActivity : ComponentActivity() { AnalyticsDialog( askForAnalyticsPermission, enableAnalytics = { - dir.enableAnalytics() + dir.toggleAnalytics(true) dir.firstLaunchDone() }, dismissDialog = { diff --git a/android/src/main/java/com/shabinder/spotiflyer/ui/AnalyticsDialog.kt b/android/src/main/java/com/shabinder/spotiflyer/ui/AnalyticsDialog.kt index f047c62b..a3b03678 100644 --- a/android/src/main/java/com/shabinder/spotiflyer/ui/AnalyticsDialog.kt +++ b/android/src/main/java/com/shabinder/spotiflyer/ui/AnalyticsDialog.kt @@ -12,7 +12,9 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.material.AlertDialog +import androidx.compose.material.ButtonDefaults import androidx.compose.material.Icon +import androidx.compose.material.OutlinedButton import androidx.compose.material.Text import androidx.compose.material.TextButton import androidx.compose.material.icons.Icons @@ -42,31 +44,38 @@ fun AnalyticsDialog( AlertDialog( onDismissRequest = dismissDialog, title = { - Row(verticalAlignment = Alignment.CenterVertically,horizontalArrangement = Arrangement.SpaceBetween) { - Icon(Icons.Rounded.Insights,"Analytics", Modifier.size(32.dp)) + Row(verticalAlignment = Alignment.CenterVertically,horizontalArrangement = Arrangement.SpaceEvenly) { + Icon(Icons.Rounded.Insights,"Analytics", Modifier.size(42.dp)) Spacer(Modifier.padding(horizontal = 8.dp)) Text("Grant Analytics",style = SpotiFlyerTypography.h5,textAlign = TextAlign.Center) - Spacer(Modifier.padding(horizontal = 8.dp)) - Column { - Icon(Icons.Rounded.Close,"Decline Analytics", Modifier.size(24.dp).clickable { - dismissDialog() - }) - Spacer(Modifier.padding(vertical = 8.dp)) - } } }, backgroundColor = Color.DarkGray, buttons = { - TextButton( - { - dismissDialog() - enableAnalytics() - }, - Modifier.padding(bottom = 16.dp, start = 16.dp, end = 16.dp).fillMaxWidth() - .background(colorPrimary, shape = SpotiFlyerShapes.medium) - .padding(horizontal = 8.dp), - ) { - Text("Sure!",color = Color.Black,fontSize = 18.sp,textAlign = TextAlign.Center) + Column { + OutlinedButton( + onClick = dismissDialog, + Modifier.padding(horizontal = 8.dp).fillMaxWidth() + .background(Color.DarkGray, shape = SpotiFlyerShapes.medium) + .padding(horizontal = 8.dp), + shape = SpotiFlyerShapes.medium, + colors = ButtonDefaults.buttonColors(backgroundColor = Color(0xFF303030)) + ) { + Text("Nope",color = colorPrimary,fontSize = 18.sp,textAlign = TextAlign.Center) + } + Spacer(Modifier.padding(vertical = 4.dp)) + TextButton( + onClick = { + dismissDialog() + enableAnalytics() + }, + Modifier.padding(bottom = 16.dp, start = 16.dp, end = 16.dp).fillMaxWidth() + .background(colorPrimary, shape = SpotiFlyerShapes.medium) + .padding(horizontal = 8.dp), + shape = SpotiFlyerShapes.medium + ) { + Text("Sure",color = Color.Black,fontSize = 18.sp,textAlign = TextAlign.Center) + } } }, text = { diff --git a/common/compose/src/commonMain/kotlin/com/shabinder/common/uikit/SpotiFlyerMainUi.kt b/common/compose/src/commonMain/kotlin/com/shabinder/common/uikit/SpotiFlyerMainUi.kt index 1691eb49..393e7897 100644 --- a/common/compose/src/commonMain/kotlin/com/shabinder/common/uikit/SpotiFlyerMainUi.kt +++ b/common/compose/src/commonMain/kotlin/com/shabinder/common/uikit/SpotiFlyerMainUi.kt @@ -46,6 +46,8 @@ import androidx.compose.material.Card import androidx.compose.material.Icon import androidx.compose.material.MaterialTheme import androidx.compose.material.OutlinedButton +import androidx.compose.material.Switch +import androidx.compose.material.SwitchDefaults import androidx.compose.material.Tab import androidx.compose.material.TabPosition import androidx.compose.material.TabRow @@ -59,6 +61,7 @@ import androidx.compose.material.icons.outlined.Info import androidx.compose.material.icons.rounded.CardGiftcard import androidx.compose.material.icons.rounded.Edit import androidx.compose.material.icons.rounded.Flag +import androidx.compose.material.icons.rounded.Insights import androidx.compose.material.icons.rounded.Share import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue @@ -102,7 +105,11 @@ fun SpotiFlyerMainContent(component: SpotiFlyerMain) { ) when (model.selectedCategory) { - HomeCategory.About -> AboutColumn { component.analytics.donationDialogVisit() } + HomeCategory.About -> AboutColumn( + analyticsEnabled = model.isAnalyticsEnabled, + donationDialogOpenEvent = { component.analytics.donationDialogVisit() }, + toggleAnalytics = component::toggleAnalytics + ) HomeCategory.History -> HistoryColumn( model.records.sortedByDescending { it.id }, component::loadImage, @@ -223,7 +230,9 @@ fun SearchPanel( @Composable fun AboutColumn( modifier: Modifier = Modifier, - donationDialogOpenEvent: () -> Unit + analyticsEnabled:Boolean, + donationDialogOpenEvent: () -> Unit, + toggleAnalytics: (enabled: Boolean) -> Unit ) { Box { @@ -398,6 +407,35 @@ fun AboutColumn( ) } } + Row( + modifier = modifier.fillMaxWidth().padding(vertical = 6.dp) + .clickable( + onClick = { + toggleAnalytics(!analyticsEnabled) + } + ), + verticalAlignment = Alignment.CenterVertically + ) { + Icon(Icons.Rounded.Insights, "Analytics Status", Modifier.size(32.dp)) + Spacer(modifier = Modifier.padding(start = 16.dp)) + Column( + Modifier.weight(1f) + ) { + Text( + text = "Analytics", + style = SpotiFlyerTypography.h6 + ) + Text( + text = "Your Data is Anonymized and never shared with 3rd party service", + style = SpotiFlyerTypography.subtitle2 + ) + } + Switch( + checked = analyticsEnabled, + onCheckedChange = null, + colors = SwitchDefaults.colors(uncheckedThumbColor = colorOffWhite) + ) + } } } } diff --git a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/Dir.kt b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/Dir.kt index 72975399..ab734432 100644 --- a/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/Dir.kt +++ b/common/dependency-injection/src/commonMain/kotlin/com/shabinder/common/di/Dir.kt @@ -57,7 +57,7 @@ expect class Dir( } val Dir.isAnalyticsEnabled get() = settings.getBooleanOrNull(AnalyticsKey) ?: false -fun Dir.enableAnalytics() = settings.putBoolean(AnalyticsKey, true) +fun Dir.toggleAnalytics(enabled: Boolean) = settings.putBoolean(AnalyticsKey, enabled) fun Dir.setDownloadDirectory(newBasePath: String) = settings.putString(DirKey, newBasePath) diff --git a/common/main/src/commonMain/kotlin/com/shabinder/common/main/SpotiFlyerMain.kt b/common/main/src/commonMain/kotlin/com/shabinder/common/main/SpotiFlyerMain.kt index 81df8e87..de848309 100644 --- a/common/main/src/commonMain/kotlin/com/shabinder/common/main/SpotiFlyerMain.kt +++ b/common/main/src/commonMain/kotlin/com/shabinder/common/main/SpotiFlyerMain.kt @@ -48,6 +48,11 @@ interface SpotiFlyerMain { * */ fun selectCategory(category: HomeCategory) + /* + * change TabBar Selected Category + * */ + fun toggleAnalytics(enabled: Boolean) + /* * Load Image from cache/Internet and cache it * */ @@ -72,7 +77,8 @@ interface SpotiFlyerMain { data class State( val records: List = emptyList(), val link: String = "", - val selectedCategory: HomeCategory = HomeCategory.About + val selectedCategory: HomeCategory = HomeCategory.About, + val isAnalyticsEnabled: Boolean = false ) enum class HomeCategory { diff --git a/common/main/src/commonMain/kotlin/com/shabinder/common/main/integration/SpotiFlyerMainImpl.kt b/common/main/src/commonMain/kotlin/com/shabinder/common/main/integration/SpotiFlyerMainImpl.kt index 5b4b30ad..765e06d3 100644 --- a/common/main/src/commonMain/kotlin/com/shabinder/common/main/integration/SpotiFlyerMainImpl.kt +++ b/common/main/src/commonMain/kotlin/com/shabinder/common/main/integration/SpotiFlyerMainImpl.kt @@ -45,7 +45,8 @@ internal class SpotiFlyerMainImpl( instanceKeeper.getStore { SpotiFlyerMainStoreProvider( storeFactory = storeFactory, - database = database + database = database, + dir = dir ).provide() } @@ -71,6 +72,10 @@ internal class SpotiFlyerMainImpl( store.accept(Intent.SelectCategory(category)) } + override fun toggleAnalytics(enabled: Boolean) { + store.accept(Intent.ToggleAnalytics(enabled)) + } + override suspend fun loadImage(url: String): Picture { return cache.get(url) { dir.loadImage(url, 150, 150) diff --git a/common/main/src/commonMain/kotlin/com/shabinder/common/main/store/SpotiFlyerMainStore.kt b/common/main/src/commonMain/kotlin/com/shabinder/common/main/store/SpotiFlyerMainStore.kt index f499f11d..ada7ed0f 100644 --- a/common/main/src/commonMain/kotlin/com/shabinder/common/main/store/SpotiFlyerMainStore.kt +++ b/common/main/src/commonMain/kotlin/com/shabinder/common/main/store/SpotiFlyerMainStore.kt @@ -25,6 +25,7 @@ internal interface SpotiFlyerMainStore : Store) : Result() data class CategoryChanged(val category: SpotiFlyerMain.HomeCategory) : Result() data class LinkChanged(val link: String) : Result() + data class ToggleAnalytics(val isEnabled: Boolean) : Result() } private inner class ExecutorImpl : SuspendExecutor() { override suspend fun executeAction(action: Unit, getState: () -> State) { + dispatch(Result.ToggleAnalytics(dir.isAnalyticsEnabled)) updates?.collect { dispatch(Result.ItemsLoaded(it)) } @@ -83,6 +89,10 @@ internal class SpotiFlyerMainStoreProvider( is Intent.ShareApp -> methods.value.shareApp() is Intent.SetLink -> dispatch(Result.LinkChanged(link = intent.link)) is Intent.SelectCategory -> dispatch(Result.CategoryChanged(intent.category)) + is Intent.ToggleAnalytics -> { + dispatch(Result.ToggleAnalytics(intent.enabled)) + dir.toggleAnalytics(intent.enabled) + } } } } @@ -93,6 +103,7 @@ internal class SpotiFlyerMainStoreProvider( is Result.ItemsLoaded -> copy(records = result.items) is Result.LinkChanged -> copy(link = result.link) is Result.CategoryChanged -> copy(selectedCategory = result.category) + is Result.ToggleAnalytics -> copy(isAnalyticsEnabled = result.isEnabled) } } }