Skip to content

Commit

Permalink
feat: Add action after clean setting (open share menu, copy to clipbo…
Browse files Browse the repository at this point in the history
…ard) (#172)

* chore: Improve app navigation

* feat: Add action after clean setting
  • Loading branch information
svenjacobs authored May 18, 2023
1 parent 37fc267 commit 731c2a5
Show file tree
Hide file tree
Showing 26 changed files with 389 additions and 154 deletions.
5 changes: 0 additions & 5 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ android {
return@signingConfigs
}

create("release") {
val release by creating {
val props = Properties()
signingFile.inputStream().use { props.load(it) }

Expand Down Expand Up @@ -113,6 +113,10 @@ play {
}

dependencies {
val composeBom = platform(libs.androidx.compose.bom)
implementation(composeBom)
implementation(platform(libs.kotlin.bom))

implementation(project(":core-common"))
implementation(project(":core-domain"))

Expand All @@ -121,9 +125,6 @@ dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.activity.ktx)

//region Compose
val composeBom = platform(libs.androidx.compose.bom)
implementation(composeBom)
debugImplementation(libs.androidx.compose.ui.tooling)
implementation(libs.bundles.androidx.compose)
implementation(libs.androidx.compose.material2) // Required by AboutLibraries
Expand All @@ -134,8 +135,6 @@ dependencies {
implementation(libs.androidx.navigation.compose)
implementation(libs.mikepenz.aboutlibraries.compose)
implementation(libs.google.accompanist.systemuicontroller)
androidTestImplementation(composeBom)
//endregion

implementation(libs.androidx.startup.runtime)
implementation(libs.androidx.lifecycle.runtime.ktx)
Expand All @@ -148,6 +147,8 @@ dependencies {

debugImplementation(libs.facebook.stetho)

androidTestImplementation(composeBom)

testImplementation(libs.kotest.runner.junit5)
testImplementation(libs.kotest.assertions.core)
testImplementation(libs.mockk)
Expand Down
18 changes: 10 additions & 8 deletions app/src/main/kotlin/com/svenjacobs/app/leon/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Léon - The URL Cleaner
* Copyright (C) 2022 Sven Jacobs
* Copyright (C) 2023 Sven Jacobs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -23,15 +23,15 @@ import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.browser.customtabs.CustomTabsClient
import androidx.browser.customtabs.CustomTabsServiceConnection
import androidx.compose.runtime.mutableStateOf
import androidx.core.view.WindowCompat
import com.svenjacobs.app.leon.ui.screens.main.MainScreen
import com.svenjacobs.app.leon.ui.screens.main.model.MainScreenViewModel
import com.svenjacobs.app.leon.ui.MainRouter

class MainActivity : ComponentActivity() {
private val mainScreenViewModel: MainScreenViewModel by viewModels()

private val sourceText = mutableStateOf<String?>(null)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -41,8 +41,8 @@ class MainActivity : ComponentActivity() {
onIntent(intent)

setContent {
MainScreen(
viewModel = mainScreenViewModel,
MainRouter(
sourceText = sourceText,
)
}
}
Expand All @@ -69,16 +69,18 @@ class MainActivity : ComponentActivity() {
} else {
null
}

Intent.ACTION_VIEW -> if (intent.scheme.orEmpty().startsWith("http")) {
intent.dataString
} else {
null
}

else -> null
}
}

mainScreenViewModel.setText(text)
sourceText.value = text
}

private fun warmupCustomTabsService() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Léon - The URL Cleaner
* Copyright (C) 2022 Sven Jacobs
* Copyright (C) 2023 Sven Jacobs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,16 +22,16 @@ import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.svenjacobs.app.leon.core.domain.inject.AppContainer.AppContext
import javax.inject.Singleton
import com.svenjacobs.app.leon.core.domain.action.ActionAfterClean
import com.svenjacobs.app.leon.core.domain.inject.DomainContainer.AppContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

/**
* Manages app specific preferences stored via [DataStore].
*/
@Singleton
class AppDataStoreManager(
private val context: Context = AppContext,
) {
Expand All @@ -43,12 +43,21 @@ class AppDataStoreManager(
}
}

val versionCode: Flow<Int> =
suspend fun setActionAfterClean(actionAfterClean: ActionAfterClean) {
context.dataStore.edit {
it[KEY_ACTION_AFTER_CLEAN] = actionAfterClean.name
}
}

val actionAfterClean: Flow<ActionAfterClean?> =
context.dataStore.data.map { preferences ->
preferences[KEY_VERSION_CODE] ?: 0
runCatching {
preferences[KEY_ACTION_AFTER_CLEAN]?.let(ActionAfterClean::valueOf)
}.getOrNull()
}

private companion object {
private val KEY_VERSION_CODE = intPreferencesKey("version_code")
private val KEY_ACTION_AFTER_CLEAN = stringPreferencesKey("action_after_clean")
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Léon - The URL Cleaner
* Copyright (C) 2022 Sven Jacobs
* Copyright (C) 2023 Sven Jacobs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -24,16 +24,14 @@ import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import com.svenjacobs.app.leon.core.domain.inject.AppContainer.AppContext
import com.svenjacobs.app.leon.core.domain.inject.DomainContainer.AppContext
import com.svenjacobs.app.leon.core.domain.sanitizer.Sanitizer
import javax.inject.Singleton
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

/**
* Manages [Sanitizer] specific preferences stored via [DataStore].
*/
@Singleton
class SanitizerDataStoreManager(
private val context: Context = AppContext,
) {
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/kotlin/com/svenjacobs/app/leon/inject/AppContainer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Léon - The URL Cleaner
* Copyright (C) 2023 Sven Jacobs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.svenjacobs.app.leon.inject

import com.svenjacobs.app.leon.datastore.AppDataStoreManager
import com.svenjacobs.app.leon.datastore.SanitizerDataStoreManager

object AppContainer {

val AppDataStoreManager: AppDataStoreManager by lazy { AppDataStoreManager() }
val SanitizerDataStoreManager: SanitizerDataStoreManager by lazy { SanitizerDataStoreManager() }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Léon - The URL Cleaner
* Copyright (C) 2022 Sven Jacobs
* Copyright (C) 2023 Sven Jacobs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -18,20 +18,21 @@

package com.svenjacobs.app.leon.sanitizer

import com.svenjacobs.app.leon.core.domain.inject.AppContainer.Sanitizers
import com.svenjacobs.app.leon.core.domain.inject.DomainContainer.Sanitizers
import com.svenjacobs.app.leon.core.domain.sanitizer.SanitizerId
import com.svenjacobs.app.leon.core.domain.sanitizer.SanitizerRepository
import com.svenjacobs.app.leon.core.domain.sanitizer.SanitizerRepository.SanitizerState
import com.svenjacobs.app.leon.core.domain.sanitizer.SanitizersCollection
import com.svenjacobs.app.leon.datastore.SanitizerDataStoreManager
import com.svenjacobs.app.leon.inject.AppContainer.SanitizerDataStoreManager
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map

class SanitizerRepositoryImpl(
private val dataStoreManager: SanitizerDataStoreManager = SanitizerDataStoreManager(),
private val dataStoreManager: SanitizerDataStoreManager = SanitizerDataStoreManager,
private val sanitizers: SanitizersCollection = Sanitizers,
) : SanitizerRepository {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Léon - The URL Cleaner
* Copyright (C) 2022 Sven Jacobs
* Copyright (C) 2023 Sven Jacobs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,7 +21,7 @@ package com.svenjacobs.app.leon.startup
import android.content.Context
import androidx.startup.Initializer
import com.svenjacobs.app.leon.BuildConfig
import com.svenjacobs.app.leon.datastore.AppDataStoreManager
import com.svenjacobs.app.leon.inject.AppContainer.AppDataStoreManager
import kotlinx.coroutines.runBlocking

/**
Expand All @@ -31,13 +31,12 @@ import kotlinx.coroutines.runBlocking
class AppInitializer : Initializer<Unit> {

override fun create(context: Context) {
val appDataStoreManager = AppDataStoreManager()
val stethoHelper = StethoHelper()

stethoHelper.initialize(context)

runBlocking {
appDataStoreManager.setVersionCode(BuildConfig.VERSION_CODE)
AppDataStoreManager.setVersionCode(BuildConfig.VERSION_CODE)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package com.svenjacobs.app.leon.startup

import android.content.Context
import com.svenjacobs.app.leon.core.domain.inject.AppContainer
import com.svenjacobs.app.leon.core.domain.inject.DomainContainer
import com.svenjacobs.app.leon.core.domain.sanitizer.aliexpress.AliexpressSanitizer
import com.svenjacobs.app.leon.core.domain.sanitizer.amazon.AmazonProductSanitizer
import com.svenjacobs.app.leon.core.domain.sanitizer.amazon.AmazonSanitizer
Expand Down Expand Up @@ -54,7 +54,7 @@ import kotlinx.collections.immutable.persistentListOf
class ContainerInitializer : DistinctInitializer<Unit> {

override fun create(context: Context) {
AppContainer.init(
DomainContainer.init(
appContext = context,
sanitizerRepositoryProvider = { SanitizerRepositoryImpl() },
sanitizers = persistentListOf(
Expand Down
66 changes: 66 additions & 0 deletions app/src/main/kotlin/com/svenjacobs/app/leon/ui/MainRouter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Léon - The URL Cleaner
* Copyright (C) 2023 Sven Jacobs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.svenjacobs.app.leon.ui

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.svenjacobs.app.leon.ui.screens.main.MainScreen
import com.svenjacobs.app.leon.ui.screens.settings.SettingsLicensesScreen
import com.svenjacobs.app.leon.ui.screens.settings.SettingsSanitizersScreen

@Composable
fun MainRouter(sourceText: State<String?>, modifier: Modifier = Modifier) {
val navController = rememberNavController()

NavHost(
modifier = modifier,
navController = navController,
startDestination = Routes.Main,
) {
composable(Routes.Main) {
MainScreen(
sourceText = sourceText,
onNavigateToSettingsSanitizers = { navController.navigate(Routes.SettingsSanitizers) },
onNavigateToSettingsLicenses = { navController.navigate(Routes.SettingsLicenses) },
)
}

composable(Routes.SettingsSanitizers) {
SettingsSanitizersScreen(
onBackClick = { navController.popBackStack() },
)
}

composable(Routes.SettingsLicenses) {
SettingsLicensesScreen(
onBackClick = { navController.popBackStack() },
)
}
}
}

private object Routes {
const val Main = "main"
const val SettingsSanitizers = "settings_sanitizers"
const val SettingsLicenses = "settings_licenses"
}
Loading

0 comments on commit 731c2a5

Please sign in to comment.