Skip to content

Commit

Permalink
Implement Settings screen and "Skip File Details" preference
Browse files Browse the repository at this point in the history
  • Loading branch information
MateusRodCosta committed Jan 5, 2024
1 parent 22c0313 commit f25aa27
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ android {
}

dependencies {
implementation(libs.androidx.ktx)
implementation(libs.bundles.androidx.ktx)

val composeBom = platform(libs.compose.bom)
implementation(composeBom)
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,35 @@
<activity
android:name=".MainActivity"
android:exported="true">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

<activity
android:name=".ShareActivity"
android:exported="true" >
android:name=".SettingsActivity"
android:exported="true" />

<activity
android:name=".DetailsActivity"
android:exported="true">

<intent-filter>
<action android:name="android.intent.action.SEND" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="image/*" />
<data android:mimeType="audio/*" />
<data android:mimeType="video/*" />
<data android:mimeType="text/*" />
<data android:mimeType="application/*" />
</intent-filter>

</activity>

</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,25 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import androidx.preference.PreferenceManager
import com.mateusrodcosta.apps.share2storage.model.SampleUriDataProvider
import com.mateusrodcosta.apps.share2storage.model.UriData
import com.mateusrodcosta.apps.share2storage.theme.AppTheme
import com.mateusrodcosta.apps.share2storage.utils.AppBasicDivider
import com.mateusrodcosta.apps.share2storage.utils.getUriData
import com.mateusrodcosta.apps.share2storage.utils.saveFile
import com.mateusrodcosta.apps.share2storage.utils.spSkipFileDetailsKey

class ShareActivity : ComponentActivity() {
class DetailsActivity : ComponentActivity() {

private var createFile: ActivityResultLauncher<String>? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val skipFileDetails = sharedPreferences.getBoolean(spSkipFileDetailsKey, false)

var uriData: UriData? = null

if (intent?.action == Intent.ACTION_SEND) {
Expand All @@ -103,34 +108,33 @@ class ShareActivity : ComponentActivity() {
R.string.toast_saved_file_failure
}, Toast.LENGTH_LONG
).show()
if (skipFileDetails) finish()
})
}
}

setContent { ShareScreen(uriData) }
setContent { DetailsScreen(uriData) }

if (skipFileDetails) createFile?.launch(uriData?.displayName ?: "")
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
@Preview
fun ShareScreen(@PreviewParameter(SampleUriDataProvider::class) uriData: UriData?) {
fun DetailsScreen(@PreviewParameter(SampleUriDataProvider::class) uriData: UriData?) {
AppTheme {
Scaffold(
topBar = {
TopAppBar(
title = { Text(stringResource(R.string.file_details)) }
Scaffold(topBar = {
TopAppBar(title = { Text(stringResource(R.string.file_details)) })
}, floatingActionButton = {
FloatingActionButton(onClick = {
createFile?.launch(uriData?.displayName ?: "")
}, content = {
Image(
imageVector = Icons.Rounded.Download,
contentDescription = stringResource(R.string.save_button)
)
},
floatingActionButton = {
FloatingActionButton(onClick = {
createFile?.launch(uriData?.displayName ?: "")
}, content = {
Image(
imageVector = Icons.Rounded.Download,
contentDescription = stringResource(R.string.save_button)
)
})
}) { paddingValues ->
})
}) { paddingValues ->
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Settings
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
Expand Down Expand Up @@ -69,14 +73,16 @@ class MainActivity : ComponentActivity() {
fun MainScreen() {
val context = this
AppTheme {
Scaffold(
topBar = {
TopAppBar(
title = { Text(stringResource(R.string.app_name)) }
)
}
)
{ paddingValues ->
Scaffold(topBar = {
TopAppBar(title = { Text(stringResource(R.string.app_name)) }, actions = {
IconButton(onClick = {
val i = Intent(context, SettingsActivity::class.java)
startActivity(i)
}) {
Icon(Icons.Rounded.Settings, stringResource(id = R.string.settings))
}
})
}) { paddingValues ->
Box(modifier = Modifier.padding(paddingValues)) {
Column(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright (C) 2022 - 2023 Mateus Rodrigues Costa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.mateusrodcosta.apps.share2storage

import android.content.SharedPreferences
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import com.mateusrodcosta.apps.share2storage.theme.AppTheme
import com.mateusrodcosta.apps.share2storage.utils.spSkipFileDetailsKey


class SettingsActivity : ComponentActivity() {

private lateinit var sharedPreferences: SharedPreferences

private var spSkipFileDetails: Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initSharedPreferences()
setContent { SettingsScreen() }
}

private fun initSharedPreferences() {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
spSkipFileDetails = sharedPreferences.getBoolean(spSkipFileDetailsKey, false)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
@Preview
fun SettingsScreen() {
var skipFileDetails by remember { mutableStateOf(spSkipFileDetails) }

val updateSkipFileDetails: (Boolean) -> Unit = { value ->
sharedPreferences.edit(commit = true) {
putBoolean(spSkipFileDetailsKey, value)
}
skipFileDetails = value
}
AppTheme {
Scaffold(topBar = {
TopAppBar(
title = { Text(stringResource(R.string.settings)) },
navigationIcon = {
IconButton(onClick = { finish() }) {
Icon(
Icons.Filled.ArrowBack, stringResource(id = R.string.back_arrow)
)
}
},
)
}) { paddingValues ->
Box(modifier = Modifier.padding(paddingValues)) {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState()),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Column {
Row(modifier = Modifier
.clickable { updateSkipFileDetails(!skipFileDetails) }
.padding(PaddingValues(horizontal = 16.dp, vertical = 8.dp))
.heightIn(min = 48.dp),
verticalAlignment = Alignment.CenterVertically) {
Column(
modifier = Modifier.weight(1.0f),
) {
Text(
stringResource(id = R.string.settings_skip_file_details_page),
style = MaterialTheme.typography.titleLarge,
)
Text(
stringResource(R.string.settings_skip_file_details_page_info),
style = MaterialTheme.typography.bodyLarge
)
}
Spacer(modifier = Modifier.width(8.dp))
Switch(
checked = skipFileDetails,
onCheckedChange = updateSkipFileDetails
)

}
}
}
}
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import android.provider.OpenableColumns
import com.mateusrodcosta.apps.share2storage.model.UriData
import java.io.*

const val spSkipFileDetailsKey: String = "skip_file_details"

fun getUriData(contentResolver: ContentResolver, uri: Uri?): UriData? {
if (uri == null) return null
val type = contentResolver.getType(uri)
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<string name="how_to_use_about">Desenvolvido por Mateus Rodrigues Costa, licenciado sob AGPLv3+</string>
<string name="how_to_use_github">Código-fonte disponível no Github em https://github.com/MateusRodCosta/Share2Storage</string>

<string name="settings">Settings</string>
<string name="settings_skip_file_details_page">Pular página de Detalhes do arquivo</string>
<string name="settings_skip_file_details_page_info">Abrir seletor de arquivos assim que receber arquivo</string>

<string name="save_button">Salvar</string>
<string name="no_file_found">Nenhum arquivo</string>

Expand All @@ -23,4 +27,6 @@

<string name="toast_saved_file_success">Arquivo salvo com sucesso!</string>
<string name="toast_saved_file_failure">Ocorreu um erro ao salvar o arquivo.</string>

<string name="back_arrow">Voltar</string>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<string name="how_to_use_about">Developed by Mateus Rodrigues Costa, licensed under AGPLv3+</string>
<string name="how_to_use_github">Source code available on Github at https://github.com/MateusRodCosta/Share2Storage</string>

<string name="settings">Settings</string>
<string name="settings_skip_file_details_page">Skip File Details page</string>
<string name="settings_skip_file_details_page_info">Open file picker as soon as file is received</string>

<string name="save_button">Save</string>
<string name="no_file_found">No file found</string>

Expand All @@ -23,4 +27,6 @@

<string name="toast_saved_file_success">File saved successfully!</string>
<string name="toast_saved_file_failure">An error occurred while saving the file.</string>

<string name="back_arrow">Back Arrow</string>
</resources>
5 changes: 4 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
androidGradlePlugin = "8.2.0"
kotlin = "1.9.21"
ktx = "1.12.0"
preference-ktx = "1.2.1"
composeBom = "2023.10.01"
activityCompose = "1.8.2"
junit = "4.13.2"
androidxTestRunner = "1.5.2"
androidxTestRules = "1.5.0"

[libraries]
androidx-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "ktx" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "ktx" }
androidx-preference-ktx = { group = "androidx.preference", name = "preference-ktx", version.ref = "preference-ktx" }
compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
compose-material3 = { group = "androidx.compose.material3", name = "material3" }
compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
Expand All @@ -23,6 +25,7 @@ androidx-test-runner = { group = "androidx.test", name="runner", version.ref = "
androidx-test-rules = { group = "androidx.test", name="rules", version.ref = "androidxTestRules" }

[bundles]
androidx-ktx = ["androidx.core.ktx", "androidx.preference.ktx"]
compose = ["compose.material3", "compose.ui.tooling.preview", "compose.ui.material.icons.extended"]
compose-debug = ["compose.ui.tooling", "compose.ui.test.manifest"]
testing = ["junit.junit"]
Expand Down

0 comments on commit f25aa27

Please sign in to comment.