Skip to content

Commit

Permalink
Refactored settings screen
Browse files Browse the repository at this point in the history
  • Loading branch information
feelfreelinux committed Feb 23, 2018
1 parent f6fd4aa commit f84b774
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.feelfreelinux.wykopmobilny.ui.modules.settings
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.preference.PreferenceFragmentCompat
import android.view.MenuItem
import io.github.feelfreelinux.wykopmobilny.R
import io.github.feelfreelinux.wykopmobilny.base.BaseActivity
Expand All @@ -14,6 +15,9 @@ class SettingsActivity : BaseActivity() {
companion object {
val THEME_CHANGED_EXTRA = "THEME_CHANGED"
val THEME_CHANGED_RESULT = 154
val EXTRA_SCREEN = "SCREEN"
val SCREEN_MAIN = "MAIN"
val SCREEN_APPEARANCE = "APPEREANCE"

fun createIntent(context: Context): Intent {
return Intent(context, SettingsActivity::class.java)
Expand All @@ -29,8 +33,22 @@ class SettingsActivity : BaseActivity() {
setSupportActionBar(toolbar)
supportActionBar?.title = "Ustawienia"
supportActionBar?.setDisplayHomeAsUpEnabled(true)
if (supportFragmentManager.backStackEntryCount == 0) {
if (intent.hasExtra(EXTRA_SCREEN) && intent.getStringExtra(EXTRA_SCREEN) != SCREEN_MAIN) {
when (intent.getStringExtra(EXTRA_SCREEN)) {
SCREEN_APPEARANCE -> openFragment(SettingsAppearance(), "appearance")
}
} else {
openFragment(SettingsFragment(), "main")
}
}
}

fun openFragment(fragment: PreferenceFragmentCompat, tag : String) {
supportFragmentManager.beginTransaction()
.replace(R.id.settings_fragment, SettingsFragment())
.replace(R.id.settings_fragment, fragment, tag)
// Add this transaction to the back stack
.addToBackStack(null)
.commit()
}

Expand All @@ -43,10 +61,15 @@ class SettingsActivity : BaseActivity() {
}

override fun onBackPressed() {
if (intent.hasExtra(THEME_CHANGED_EXTRA)) {
setResult(THEME_CHANGED_RESULT)
navigatorApi.openMainActivity()
if (supportFragmentManager.backStackEntryCount == 1){
if (intent.hasExtra(THEME_CHANGED_EXTRA)) {
setResult(THEME_CHANGED_RESULT)
navigatorApi.openMainActivity()
}
finish()
}
else {
super.onBackPressed()
}
finish()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.github.feelfreelinux.wykopmobilny.ui.modules.settings

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.preference.CheckBoxPreference
import android.support.v7.preference.ListPreference
import android.support.v7.preference.PreferenceFragmentCompat
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.support.AndroidSupportInjection
import dagger.android.support.HasSupportFragmentInjector
import io.github.feelfreelinux.wykopmobilny.R
import io.github.feelfreelinux.wykopmobilny.ui.modules.notifications.notificationsservice.WykopNotificationsJob
import io.github.feelfreelinux.wykopmobilny.utils.SettingsPreferencesApi
import javax.inject.Inject

class SettingsAppearance : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener, HasSupportFragmentInjector {
@Inject
lateinit var settingsApi: SettingsPreferencesApi
@Inject lateinit var childFragmentInjector : DispatchingAndroidInjector<Fragment>

override fun supportFragmentInjector(): AndroidInjector<Fragment> {
return childFragmentInjector
}

override fun onAttach(context: Context) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.app_preferences_appearance)
findPreference("useAmoledTheme").isEnabled = settingsApi.useDarkTheme
findPreference("cutImageProportion").isEnabled = settingsApi.cutImages
findPreference("linkImagePosition").isEnabled = !settingsApi.linkSimpleList && settingsApi.linkShowImage
findPreference("linkShowAuthor").isEnabled = !settingsApi.linkSimpleList

(findPreference("hotEntriesScreen") as ListPreference).apply {
summary = entry
}

(findPreference("defaultScreen") as ListPreference).apply {
summary = entry
}

(findPreference("linkImagePosition") as ListPreference).apply {
summary = entry
}
}

override fun onSharedPreferenceChanged(sharedPrefs: SharedPreferences, key: String) {
val pref = findPreference(key)
findPreference("useAmoledTheme").isEnabled = settingsApi.useDarkTheme
findPreference("cutImageProportion").isEnabled = settingsApi.cutImages
findPreference("linkShowAuthor").isEnabled = !settingsApi.linkSimpleList
findPreference("linkImagePosition").isEnabled = !settingsApi.linkSimpleList

if (pref is ListPreference) {
pref.setSummary(pref.entry)
when(pref.key) {
"linkImagePosition" -> restartActivity()
}
} else if (pref is CheckBoxPreference) {
when (pref.key) {
"linkShowAuthor", "linkSimpleList", "linkShowImage" -> restartActivity()
"useDarkTheme", "useAmoledTheme" -> restartActivity()
}
}
}

override fun onResume() {
super.onResume()
preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}

override fun onPause() {
super.onPause()
preferenceScreen.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
}

private fun restartActivity() {
val intent = Intent(context, SettingsActivity::class.java)
intent.putExtra(SettingsActivity.THEME_CHANGED_EXTRA, true)
intent.putExtra(SettingsActivity.EXTRA_SCREEN, SettingsActivity.SCREEN_APPEARANCE)
startActivity(intent)
activity?.finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.preference.CheckBoxPreference
import android.support.v7.preference.ListPreference
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
Expand All @@ -33,43 +34,21 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.app_preferences)
findPreference("useAmoledTheme").isEnabled = settingsApi.useDarkTheme
findPreference("cutImageProportion").isEnabled = settingsApi.cutImages
findPreference("linkImagePosition").isEnabled = !settingsApi.linkSimpleList && settingsApi.linkShowImage
findPreference("linkShowAuthor").isEnabled = !settingsApi.linkSimpleList
(findPreference("notificationsSchedulerDelay") as ListPreference).apply {
summary = entry
}

(findPreference("hotEntriesScreen") as ListPreference).apply {
summary = entry
}

(findPreference("defaultScreen") as ListPreference).apply {
summary = entry
(findPreference("appearance") as Preference).setOnPreferenceClickListener {
(activity as SettingsActivity).openFragment(SettingsAppearance(), "appearance")
true
}

(findPreference("linkImagePosition") as ListPreference).apply {
summary = entry
}
}

override fun onSharedPreferenceChanged(sharedPrefs: SharedPreferences, key: String) {
val pref = findPreference(key)
findPreference("useAmoledTheme").isEnabled = settingsApi.useDarkTheme
findPreference("cutImageProportion").isEnabled = settingsApi.cutImages
findPreference("linkShowAuthor").isEnabled = !settingsApi.linkSimpleList
findPreference("linkImagePosition").isEnabled = !settingsApi.linkSimpleList

if (pref is ListPreference) {
pref.setSummary(pref.entry)
when(pref.key) {
"linkImagePosition" -> restartActivity()
}
} else if (pref is CheckBoxPreference) {
if (pref is CheckBoxPreference) {
when (pref.key) {
"linkShowAuthor", "linkSimpleList", "linkShowImage" -> restartActivity()
"useDarkTheme", "useAmoledTheme" -> restartActivity()
"showNotifications" -> WykopNotificationsJob.schedule(settingsApi)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ import dagger.android.ContributesAndroidInjector
abstract class SettingsFragmentProvider {
@ContributesAndroidInjector()
abstract fun provideMainSettingsFragment(): SettingsFragment

@ContributesAndroidInjector()
abstract fun provideAppereanceFragment(): SettingsAppearance
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class EntryWidget : CardView, EntryView, LayoutContainer {
if (entry.body.isNotEmpty()) {
entryContentTextView.apply {
isVisible = true
if (shouldEnableClickListener) {
if (shouldEnableClickListener && settingsPreferencesApi.cutLongEntries) {
maxLines = EllipsizingTextView.MAX_LINES
ellipsize = TextUtils.TruncateAt.END
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface SettingsPreferencesApi {
var useAmoledTheme : Boolean
var showNotifications : Boolean
var showMinifiedImages : Boolean
var cutLongEntries : Boolean
var cutImages : Boolean
var openSpoilersDialog : Boolean
var cutImageProportion : Int
Expand All @@ -35,6 +36,7 @@ class SettingsPreferences(context : Context) : Preferences(context, true), Setti
override var useDarkTheme by booleanPref(defaultValue = false)
override var useAmoledTheme by booleanPref(defaultValue = false)
override var showMinifiedImages by booleanPref(defaultValue = false)
override var cutLongEntries by booleanPref(defaultValue = true)
override var cutImages by booleanPref(defaultValue = true)
override var cutImageProportion by intPref(defaultValue = 60)
override var openSpoilersDialog by booleanPref(defaultValue = true)
Expand Down
83 changes: 3 additions & 80 deletions app/src/main/res/xml/app_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,9 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sample="http://schemas.android.com/apk/res-auto">

<android.support.v7.preference.PreferenceCategory
android:title="@string/pref_appearance">

<android.support.v7.preference.CheckBoxPreference
android:key="useDarkTheme"
android:title="@string/pref_dark_style"
android:defaultValue="false"/>

<android.support.v7.preference.CheckBoxPreference
android:key="useAmoledTheme"
android:title="@string/useAmoledTheme"
android:defaultValue="false"/>

<android.support.v7.preference.ListPreference
android:key="defaultScreen"
android:title="@string/defaultScreen"
android:entries="@array/preferences_default_screen"
android:entryValues="@array/preferences_default_screen_values"
android:defaultValue="mainpage"/>

<android.support.v7.preference.ListPreference
android:key="hotEntriesScreen"
android:title="@string/defaultHotScreen"
android:entries="@array/preferences_hot_screen"
android:entryValues="@array/preferences_hot_screen_values"
android:defaultValue="newest"/>

<android.support.v7.preference.CheckBoxPreference
android:key="linkSimpleList"
android:title="@string/link_simple_list"
android:defaultValue="false"/>

<android.support.v7.preference.CheckBoxPreference
android:key="linkShowImage"
android:title="@string/link_show_image"
android:defaultValue="true"/>

<android.support.v7.preference.ListPreference
android:key="linkImagePosition"
android:title="@string/linkImagePosition"
android:entries="@array/preferences_link_image_position"
android:entryValues="@array/preferences_link_image_position_values"
android:defaultValue="left"/>

<android.support.v7.preference.CheckBoxPreference
android:key="linkShowAuthor"
android:title="@string/link_show_author"
android:defaultValue="false"/>

<android.support.v7.preference.CheckBoxPreference
android:key="openSpoilersDialog"
android:title="@string/open_spoilers_in_dialog"
android:defaultValue="true"/>

</android.support.v7.preference.PreferenceCategory>

<android.support.v7.preference.PreferenceCategory
android:title="@string/app_config_images">

<android.support.v7.preference.CheckBoxPreference
android:key="showMinifiedImages"
android:title="@string/app_config_images_minified"
android:defaultValue="false"/>

<android.support.v7.preference.CheckBoxPreference
android:key="cutImages"
android:title="@string/app_config_images_cut"
android:defaultValue="true"/>

<com.pavelsikun.seekbarpreference.SeekBarPreferenceCompat
android:key="cutImageProportion"
android:title="@string/app_config_images_cut_image_proportion"
android:defaultValue="60"
sample:msbp_minValue="20"
sample:msbp_maxValue="150"
sample:msbp_interval="10"
sample:msbp_measurementUnit="%"
sample:msbp_dialogEnabled="false"/>

</android.support.v7.preference.PreferenceCategory>
<android.support.v7.preference.Preference
android:key="appearance"
android:title="Ustawienia wyglądu aplikacji"/>

<android.support.v7.preference.PreferenceCategory
android:title="@string/pref_notifications">
Expand Down
Loading

0 comments on commit f84b774

Please sign in to comment.