Skip to content

Commit

Permalink
Improve shared preferences defaults
Browse files Browse the repository at this point in the history
Some users requested defaults with lower friction
  • Loading branch information
MateusRodCosta committed Nov 28, 2024
1 parent 2781ad3 commit 79a0796
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.mateusrodcosta.apps.share2storage.screens.DetailsScreen
import com.mateusrodcosta.apps.share2storage.screens.DetailsScreenSkipped
import com.mateusrodcosta.apps.share2storage.utils.CreateDocumentWithInitialUri
import com.mateusrodcosta.apps.share2storage.utils.SharedPreferenceKeys
import com.mateusrodcosta.apps.share2storage.utils.SharedPreferencesDefaultValues
import com.mateusrodcosta.apps.share2storage.utils.getUriData
import com.mateusrodcosta.apps.share2storage.utils.saveFile
import kotlinx.coroutines.Dispatchers
Expand All @@ -51,11 +52,10 @@ class DetailsActivity : ComponentActivity() {
private lateinit var fileUri: Uri
private lateinit var uriData: UriData

private var skipFileDetails: Boolean = false
private var defaultSaveLocation: Uri? = null
private var shouldShowFilePreview: Boolean = true
private var shouldSkipFilePicker: Boolean = false

private var skipFileDetails: Boolean = false
private var shouldShowFilePreview: Boolean = true

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -99,11 +99,6 @@ class DetailsActivity : ComponentActivity() {
private fun getPreferences() {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)

val skipFileDetails =
sharedPreferences.getBoolean(SharedPreferenceKeys.SKIP_FILE_DETAILS_KEY, false)
Log.d("details] skipFileDetails", skipFileDetails.toString())
this.skipFileDetails = skipFileDetails

val defaultSaveLocationRaw =
sharedPreferences.getString(SharedPreferenceKeys.DEFAULT_SAVE_LOCATION_KEY, null)
Log.d("details] defaultSaveLocationRaw", defaultSaveLocationRaw.toString())
Expand All @@ -113,16 +108,28 @@ class DetailsActivity : ComponentActivity() {
Log.d("details] defaultSaveLocation", defaultSaveLocation.toString())
this.defaultSaveLocation = defaultSaveLocation

val showFilePreview =
sharedPreferences.getBoolean(SharedPreferenceKeys.SHOW_FILE_PREVIEW_KEY, true)
Log.d("details] showFilePreview", showFilePreview.toString())
this.shouldShowFilePreview = !skipFileDetails && showFilePreview

val skipFilePicker =
sharedPreferences.getBoolean(SharedPreferenceKeys.SKIP_FILE_PICKER, false)
val skipFilePicker = sharedPreferences.getBoolean(
SharedPreferenceKeys.SKIP_FILE_PICKER_KEY,
SharedPreferencesDefaultValues.SKIP_FILE_PICKER_DEFAULT
)
Log.d("details] skipFilePicker", skipFilePicker.toString())
// Only skip file picker if both a default folder is set and "Skip File Picker is selected"
this.shouldSkipFilePicker = defaultSaveLocation != null && skipFilePicker


val skipFileDetails = sharedPreferences.getBoolean(
SharedPreferenceKeys.SKIP_FILE_DETAILS_KEY,
SharedPreferencesDefaultValues.SKIP_FILE_DETAILS_DEFAULT
)
Log.d("details] skipFileDetails", skipFileDetails.toString())
this.skipFileDetails = skipFileDetails

val showFilePreview = sharedPreferences.getBoolean(
SharedPreferenceKeys.SHOW_FILE_PREVIEW_KEY,
SharedPreferencesDefaultValues.SHOW_FILE_PREVIEW_DEFAULT
)
Log.d("details] showFilePreview", showFilePreview.toString())
this.shouldShowFilePreview = !skipFileDetails && showFilePreview
}

private fun handleIntent(intent: Intent?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import com.mateusrodcosta.apps.share2storage.screens.dialogs.DefaultFolderDialog
import com.mateusrodcosta.apps.share2storage.screens.shared.AppBasicDivider
import com.mateusrodcosta.apps.share2storage.screens.shared.AppListHeader
import com.mateusrodcosta.apps.share2storage.ui.theme.AppTheme
import com.mateusrodcosta.apps.share2storage.utils.SharedPreferencesDefaultValues
import com.mateusrodcosta.apps.share2storage.utils.Utils
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -59,17 +60,21 @@ import kotlinx.coroutines.flow.StateFlow
@Composable
fun SettingsScreenPreview() {
val mockDefaultSaveLocation = MutableStateFlow(null)
val mockSkipFilePicker = MutableStateFlow(false)
val mockSkipFileDetails = MutableStateFlow(false)
val mockInterceptActionViewIntents = MutableStateFlow(false)
val mockShowFilePreview = MutableStateFlow(true)
val mockSkipFilePicker =
MutableStateFlow(SharedPreferencesDefaultValues.SKIP_FILE_PICKER_DEFAULT)
val mockSkipFileDetails =
MutableStateFlow(SharedPreferencesDefaultValues.SKIP_FILE_DETAILS_DEFAULT)
val mockShowFilePreview =
MutableStateFlow(SharedPreferencesDefaultValues.SHOW_FILE_PREVIEW_DEFAULT)
val mockInterceptActionViewIntents =
MutableStateFlow(SharedPreferencesDefaultValues.INTERCEPT_ACTION_VIEW_INTENTS_DEFAULT)

SettingsScreenContent(
spDefaultSaveLocation = mockDefaultSaveLocation,
spSkipFilePicker = mockSkipFilePicker,
spSkipFileDetails = mockSkipFileDetails,
spInterceptActionViewIntents = mockInterceptActionViewIntents,
spShowFilePreview = mockShowFilePreview,
spInterceptActionViewIntents = mockInterceptActionViewIntents,
)
}

Expand All @@ -78,17 +83,21 @@ fun SettingsScreenPreview() {
@Composable
fun SettingsScreenPreviewPtBr() {
val mockDefaultSaveLocation = MutableStateFlow(null)
val mockSkipFilePicker = MutableStateFlow(false)
val mockSkipFileDetails = MutableStateFlow(false)
val mockInterceptActionViewIntents = MutableStateFlow(false)
val mockShowFilePreview = MutableStateFlow(true)
val mockSkipFilePicker =
MutableStateFlow(SharedPreferencesDefaultValues.SKIP_FILE_PICKER_DEFAULT)
val mockSkipFileDetails =
MutableStateFlow(SharedPreferencesDefaultValues.SKIP_FILE_DETAILS_DEFAULT)
val mockShowFilePreview =
MutableStateFlow(SharedPreferencesDefaultValues.SHOW_FILE_PREVIEW_DEFAULT)
val mockInterceptActionViewIntents =
MutableStateFlow(SharedPreferencesDefaultValues.INTERCEPT_ACTION_VIEW_INTENTS_DEFAULT)

SettingsScreenContent(
spDefaultSaveLocation = mockDefaultSaveLocation,
spSkipFilePicker = mockSkipFilePicker,
spSkipFileDetails = mockSkipFileDetails,
spInterceptActionViewIntents = mockInterceptActionViewIntents,
spShowFilePreview = mockShowFilePreview,
spInterceptActionViewIntents = mockInterceptActionViewIntents,
)
}

Expand All @@ -99,8 +108,8 @@ fun SettingsScreen(navController: NavController, settingsViewModel: SettingsView
spDefaultSaveLocation = settingsViewModel.defaultSaveLocation,
spSkipFilePicker = settingsViewModel.skipFilePicker,
spSkipFileDetails = settingsViewModel.skipFileDetails,
spInterceptActionViewIntents = settingsViewModel.interceptActionViewIntents,
spShowFilePreview = settingsViewModel.showFilePreview,
spInterceptActionViewIntents = settingsViewModel.interceptActionViewIntents,
launchFilePicker = { settingsViewModel.getSaveLocationDirIntent().launch(null) },
clearDefaultSaveLocation = { settingsViewModel.clearDefaultSaveLocation() },
updateSkipFilePicker = { value: Boolean ->
Expand All @@ -125,8 +134,8 @@ fun SettingsScreenContent(
spDefaultSaveLocation: StateFlow<Uri?>,
spSkipFilePicker: StateFlow<Boolean>,
spSkipFileDetails: StateFlow<Boolean>,
spInterceptActionViewIntents: StateFlow<Boolean>,
spShowFilePreview: StateFlow<Boolean>,
spInterceptActionViewIntents: StateFlow<Boolean>,
launchFilePicker: () -> Unit = {},
clearDefaultSaveLocation: () -> Unit = {},
updateSkipFilePicker: (Boolean) -> Unit = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.core.content.edit
import androidx.lifecycle.ViewModel
import androidx.preference.PreferenceManager
import com.mateusrodcosta.apps.share2storage.utils.SharedPreferenceKeys
import com.mateusrodcosta.apps.share2storage.utils.SharedPreferencesDefaultValues
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

Expand Down Expand Up @@ -89,34 +90,41 @@ class SettingsViewModel : ViewModel() {
}
else null

val spSkipFileDetails =
sharedPreferences.getBoolean(SharedPreferenceKeys.SKIP_FILE_DETAILS_KEY, false)
Log.d("settings] initSharedPreferences] skipFileDetails", spSkipFileDetails.toString())

val spInterceptActionViewIntents = sharedPreferences.getBoolean(
SharedPreferenceKeys.INTERCEPT_ACTION_VIEW_INTENTS_KEY, false
val spSkipFilePicker = sharedPreferences.getBoolean(
SharedPreferenceKeys.SKIP_FILE_PICKER_KEY,
SharedPreferencesDefaultValues.SKIP_FILE_PICKER_DEFAULT
)
Log.d(
"settings] initSharedPreferences] interceptActionViewIntents",
spInterceptActionViewIntents.toString()
Log.d("settings] initSharedPreferences] skipFilePicker", spSkipFilePicker.toString())


val spSkipFileDetails = sharedPreferences.getBoolean(
SharedPreferenceKeys.SKIP_FILE_DETAILS_KEY,
SharedPreferencesDefaultValues.SKIP_FILE_DETAILS_DEFAULT
)
Log.d("settings] initSharedPreferences] skipFileDetails", spSkipFileDetails.toString())

val spShowFilePreview = sharedPreferences.getBoolean(
SharedPreferenceKeys.SHOW_FILE_PREVIEW_KEY, true
SharedPreferenceKeys.SHOW_FILE_PREVIEW_KEY,
SharedPreferencesDefaultValues.SHOW_FILE_PREVIEW_DEFAULT
)
Log.d(
"settings] initSharedPreferences] showFilePreview", spShowFilePreview.toString()
)

val spSkipFilePicker =
sharedPreferences.getBoolean(SharedPreferenceKeys.SKIP_FILE_PICKER, false)
Log.d("settings] initSharedPreferences] skipFilePicker", spSkipFilePicker.toString())
val spInterceptActionViewIntents = sharedPreferences.getBoolean(
SharedPreferenceKeys.INTERCEPT_ACTION_VIEW_INTENTS_KEY,
SharedPreferencesDefaultValues.INTERCEPT_ACTION_VIEW_INTENTS_DEFAULT
)
Log.d(
"settings] initSharedPreferences] interceptActionViewIntents",
spInterceptActionViewIntents.toString()
)

_defaultSaveLocation.value = spDefaultSaveLocation
_skipFilePicker.value = spSkipFilePicker
_skipFileDetails.value = spSkipFileDetails
_interceptActionViewIntents.value = spInterceptActionViewIntents
_showFilePreview.value = spShowFilePreview
_skipFilePicker.value = spSkipFilePicker
_interceptActionViewIntents.value = spInterceptActionViewIntents
}

fun updateDefaultSaveLocation(value: Uri?) {
Expand Down Expand Up @@ -205,7 +213,7 @@ class SettingsViewModel : ViewModel() {

fun updateSkipFilePicker(value: Boolean) {
sharedPreferences.edit(commit = true) {
putBoolean(SharedPreferenceKeys.SKIP_FILE_PICKER, value)
putBoolean(SharedPreferenceKeys.SKIP_FILE_PICKER_KEY, value)
Log.e("settings] updateSkipFilePicker", value.toString())
_skipFilePicker.value = value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@
package com.mateusrodcosta.apps.share2storage.utils

object SharedPreferenceKeys {
const val SKIP_FILE_DETAILS_KEY: String = "skip_file_details"
const val DEFAULT_SAVE_LOCATION_KEY: String = "default_save_location"
const val INTERCEPT_ACTION_VIEW_INTENTS_KEY: String = "intercept_action_view_intents"
const val SKIP_FILE_PICKER_KEY: String = "skip_file_picker"
const val SKIP_FILE_DETAILS_KEY: String = "skip_file_details"
const val SHOW_FILE_PREVIEW_KEY: String = "show_file_preview"
const val SKIP_FILE_PICKER: String = "skip_file_picker"
const val INTERCEPT_ACTION_VIEW_INTENTS_KEY: String = "intercept_action_view_intents"
}

object SharedPreferencesDefaultValues {
const val SKIP_FILE_PICKER_DEFAULT: Boolean = false
const val SKIP_FILE_DETAILS_DEFAULT: Boolean = true
const val SHOW_FILE_PREVIEW_DEFAULT: Boolean = false
const val INTERCEPT_ACTION_VIEW_INTENTS_DEFAULT: Boolean = false
}

0 comments on commit 79a0796

Please sign in to comment.