Skip to content

Commit

Permalink
[refactor]: removed useless wrappers for settings fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Oct 2, 2023
1 parent a8aab1b commit 7a0fa62
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ package com.f0x1d.logfox.ui.fragment.settings
import android.os.Bundle
import com.f0x1d.logfox.R
import com.f0x1d.logfox.ui.fragment.settings.base.BasePreferenceFragment
import com.f0x1d.logfox.ui.fragment.settings.base.BaseSettingsWrapperFragment
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SettingsCrashesFragment: BaseSettingsWrapperFragment() {
class SettingsCrashesFragment: BasePreferenceFragment() {

override val wrappedFragment get() = SettingsCrashesWrappedFragment()
override val title = R.string.crashes
override val showBackArrow = true

@AndroidEntryPoint
class SettingsCrashesWrappedFragment: BasePreferenceFragment() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.settings_crashes)
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.settings_crashes)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,30 @@ import androidx.preference.Preference
import com.f0x1d.logfox.BuildConfig
import com.f0x1d.logfox.R
import com.f0x1d.logfox.ui.fragment.settings.base.BasePreferenceFragment
import com.f0x1d.logfox.ui.fragment.settings.base.BaseSettingsWrapperFragment
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SettingsMenuFragment: BaseSettingsWrapperFragment() {
class SettingsMenuFragment: BasePreferenceFragment() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.settings_menu)

override val wrappedFragment get() = SettingsMenuWrappedFragment()

@AndroidEntryPoint
class SettingsMenuWrappedFragment: BasePreferenceFragment() {

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.settings_menu)

findPreference<Preference>("pref_settings_ui")?.setOnPreferenceClickListener {
findNavController().navigate(SettingsMenuFragmentDirections.actionSettingsMenuFragmentToSettingsUIFragment())
return@setOnPreferenceClickListener true
}
findPreference<Preference>("pref_settings_service")?.setOnPreferenceClickListener {
findNavController().navigate(SettingsMenuFragmentDirections.actionSettingsMenuFragmentToSettingsServiceFragment())
return@setOnPreferenceClickListener true
}
findPreference<Preference>("pref_settings_crashes")?.setOnPreferenceClickListener {
findNavController().navigate(SettingsMenuFragmentDirections.actionSettingsMenuFragmentToSettingsCrashesFragment())
return@setOnPreferenceClickListener true
}
findPreference<Preference>("pref_settings_notifications")?.setOnPreferenceClickListener {
findNavController().navigate(SettingsMenuFragmentDirections.actionSettingsMenuFragmentToSettingsNotificationsFragment())
return@setOnPreferenceClickListener true
}

findPreference<Preference>("pref_settings_app_version")?.title = "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})"
findPreference<Preference>("pref_settings_ui")?.setOnPreferenceClickListener {
findNavController().navigate(SettingsMenuFragmentDirections.actionSettingsMenuFragmentToSettingsUIFragment())
return@setOnPreferenceClickListener true
}
findPreference<Preference>("pref_settings_service")?.setOnPreferenceClickListener {
findNavController().navigate(SettingsMenuFragmentDirections.actionSettingsMenuFragmentToSettingsServiceFragment())
return@setOnPreferenceClickListener true
}
findPreference<Preference>("pref_settings_crashes")?.setOnPreferenceClickListener {
findNavController().navigate(SettingsMenuFragmentDirections.actionSettingsMenuFragmentToSettingsCrashesFragment())
return@setOnPreferenceClickListener true
}
findPreference<Preference>("pref_settings_notifications")?.setOnPreferenceClickListener {
findNavController().navigate(SettingsMenuFragmentDirections.actionSettingsMenuFragmentToSettingsNotificationsFragment())
return@setOnPreferenceClickListener true
}

findPreference<Preference>("pref_settings_app_version")?.title = "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,48 @@ import com.f0x1d.logfox.LogFoxApp
import com.f0x1d.logfox.R
import com.f0x1d.logfox.extensions.hasNotificationsPermission
import com.f0x1d.logfox.ui.fragment.settings.base.BasePreferenceFragment
import com.f0x1d.logfox.ui.fragment.settings.base.BaseSettingsWrapperFragment
import com.f0x1d.logfox.utils.preferences.AppPreferences
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class SettingsNotificationsFragment: BaseSettingsWrapperFragment() {
class SettingsNotificationsFragment: BasePreferenceFragment() {

override val wrappedFragment get() = SettingsNotificationWrappedFragment()
override val title = R.string.notifications
override val showBackArrow = true

@AndroidEntryPoint
class SettingsNotificationWrappedFragment: BasePreferenceFragment() {
@Inject
lateinit var appPreferences: AppPreferences

@Inject
lateinit var appPreferences: AppPreferences
@SuppressLint("InlinedApi")
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.settings_notifications)

@SuppressLint("InlinedApi")
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.settings_notifications)
findPreference<Preference>("pref_notifications_permission")?.setOnPreferenceClickListener {
startActivity(Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra(Settings.EXTRA_APP_PACKAGE, BuildConfig.APPLICATION_ID)
})
return@setOnPreferenceClickListener true
}

findPreference<Preference>("pref_logging_notification")?.apply {
isVisible = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O

findPreference<Preference>("pref_notifications_permission")?.setOnPreferenceClickListener {
startActivity(Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
setOnPreferenceClickListener {
startActivity(Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra(Settings.EXTRA_APP_PACKAGE, BuildConfig.APPLICATION_ID)
putExtra(Settings.EXTRA_CHANNEL_ID, LogFoxApp.LOGGING_STATUS_CHANNEL_ID)
})
return@setOnPreferenceClickListener true
}

findPreference<Preference>("pref_logging_notification")?.apply {
isVisible = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O

setOnPreferenceClickListener {
startActivity(Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra(Settings.EXTRA_APP_PACKAGE, BuildConfig.APPLICATION_ID)
putExtra(Settings.EXTRA_CHANNEL_ID, LogFoxApp.LOGGING_STATUS_CHANNEL_ID)
})
return@setOnPreferenceClickListener true
}
}
}
}

override fun onStart() {
super.onStart()
override fun onStart() {
super.onStart()

findPreference<Preference>("pref_notifications_permission")?.isVisible = !requireContext().hasNotificationsPermission()
}
findPreference<Preference>("pref_notifications_permission")?.isVisible = !requireContext().hasNotificationsPermission()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.f0x1d.logfox.extensions.setupAsListPreference
import com.f0x1d.logfox.extensions.toast
import com.f0x1d.logfox.repository.logging.LoggingRepository
import com.f0x1d.logfox.ui.fragment.settings.base.BasePreferenceFragment
import com.f0x1d.logfox.ui.fragment.settings.base.BaseSettingsWrapperFragment
import com.f0x1d.logfox.utils.fillWithStrings
import com.f0x1d.logfox.utils.preferences.AppPreferences
import com.f0x1d.logfox.utils.terminal.DefaultTerminal
Expand All @@ -22,88 +21,83 @@ import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class SettingsServiceFragment: BaseSettingsWrapperFragment() {
class SettingsServiceFragment: BasePreferenceFragment() {

override val wrappedFragment get() = SettingsServiceWrappedFragment()
override val title = R.string.service
override val showBackArrow = true

@AndroidEntryPoint
class SettingsServiceWrappedFragment: BasePreferenceFragment() {

@Inject
lateinit var loggingRepository: LoggingRepository

@Inject
lateinit var appPreferences: AppPreferences

@Inject
lateinit var terminals: Array<Terminal>

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.settings_service)

findPreference<Preference>("pref_selected_terminal_index")?.apply {
val filledTerminalSettings = terminals.map { it.title }.toIntArray().fillWithStrings(requireContext())

setupAsListPreference(
{
setIcon(R.drawable.ic_dialog_terminal)
},
filledTerminalSettings,
{ appPreferences.selectedTerminalIndex }
) {
if (appPreferences.selectedTerminalIndex == it) {
loggingRepository.restartLogging()
return@setupAsListPreference
}

lifecycleScope.launch {
val selectedTerminal = terminals[it]
if (selectedTerminal.isSupported()) {
appPreferences.selectedTerminalIndex = it
askAboutNewTerminalRestart()
} else
requireContext().toast(R.string.terminal_unavailable)
}
}
@Inject
lateinit var loggingRepository: LoggingRepository

observeAndUpdateSummaryForList(appPreferences, this@SettingsServiceWrappedFragment, 0, filledTerminalSettings)
}
@Inject
lateinit var appPreferences: AppPreferences

@Inject
lateinit var terminals: Array<Terminal>

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.settings_service)

findPreference<SwitchPreferenceCompat>("pref_start_on_boot")?.apply {
setOnPreferenceChangeListener { preference, newValue ->
val badTerminal = appPreferences.selectedTerminalIndex == DefaultTerminal.INDEX
findPreference<Preference>("pref_selected_terminal_index")?.apply {
val filledTerminalSettings = terminals.map { it.title }.toIntArray().fillWithStrings(requireContext())

if (isAtLeastAndroid13 && newValue as Boolean && badTerminal) {
showAndroid13WarningDialog()
}
return@setOnPreferenceChangeListener true
setupAsListPreference(
{
setIcon(R.drawable.ic_dialog_terminal)
},
filledTerminalSettings,
{ appPreferences.selectedTerminalIndex }
) {
if (appPreferences.selectedTerminalIndex == it) {
loggingRepository.restartLogging()
return@setupAsListPreference
}

lifecycleScope.launch {
val selectedTerminal = terminals[it]
if (selectedTerminal.isSupported()) {
appPreferences.selectedTerminalIndex = it
askAboutNewTerminalRestart()
} else
requireContext().toast(R.string.terminal_unavailable)
}
}

observeAndUpdateSummaryForList(appPreferences, this@SettingsServiceFragment, 0, filledTerminalSettings)
}

private fun askAboutNewTerminalRestart() {
MaterialAlertDialogBuilder(requireContext())
.setIcon(R.drawable.ic_dialog_terminal)
.setTitle(R.string.new_terminal_selected)
.setMessage(R.string.new_terminal_selected_question)
.setPositiveButton(R.string.yes) { dialog, _ ->
loggingRepository.restartLogging()
findPreference<SwitchPreferenceCompat>("pref_start_on_boot")?.apply {
setOnPreferenceChangeListener { preference, newValue ->
val badTerminal = appPreferences.selectedTerminalIndex == DefaultTerminal.INDEX

if (isAtLeastAndroid13 && newValue as Boolean && badTerminal) {
showAndroid13WarningDialog()
}
.setNeutralButton(R.string.no, null)
.show()
return@setOnPreferenceChangeListener true
}
}
}

private fun showAndroid13WarningDialog() {
MaterialAlertDialogBuilder(requireContext())
.setIcon(R.drawable.ic_dialog_warning)
.setTitle(R.string.warning)
.setMessage(R.string.android13_start_on_boot_warning)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, null)
.show()
}
private fun askAboutNewTerminalRestart() {
MaterialAlertDialogBuilder(requireContext())
.setIcon(R.drawable.ic_dialog_terminal)
.setTitle(R.string.new_terminal_selected)
.setMessage(R.string.new_terminal_selected_question)
.setPositiveButton(R.string.yes) { dialog, _ ->
loggingRepository.restartLogging()
}
.setNeutralButton(R.string.no, null)
.show()
}

private fun showAndroid13WarningDialog() {
MaterialAlertDialogBuilder(requireContext())
.setIcon(R.drawable.ic_dialog_warning)
.setTitle(R.string.warning)
.setMessage(R.string.android13_start_on_boot_warning)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, null)
.show()
}
}

Expand Down
Loading

0 comments on commit 7a0fa62

Please sign in to comment.