forked from upstream-mirrors/quik
Created custom view to handle preferences better with MVI
This commit is contained in:
@@ -5,24 +5,23 @@ import android.app.ProgressDialog
|
||||
import android.os.Bundle
|
||||
import android.support.v7.widget.LinearLayoutManager
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import com.jakewharton.rxbinding2.view.clicks
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.di.AppComponentManager
|
||||
import com.moez.QKSMS.presentation.base.QkActivity
|
||||
import com.moez.QKSMS.presentation.view.PreferenceView
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.settings_activity.*
|
||||
|
||||
class SettingsActivity : QkActivity<SettingsViewModel>(), SettingsView {
|
||||
|
||||
override val viewModelClass = SettingsViewModel::class
|
||||
override val preferencesAddedIntent by lazy { fragment.preferencesAdded }
|
||||
override val preferenceClickIntent by lazy { fragment.preferenceClicks }
|
||||
override val preferenceChangeIntent by lazy { fragment.preferenceChanges }
|
||||
override val preferenceClickIntent: Subject<PreferenceView> = PublishSubject.create()
|
||||
override val themeSelectedIntent by lazy { themeAdapter.colorSelected }
|
||||
|
||||
private val fragment by lazy {
|
||||
fragmentManager.findFragmentById(fragmentFrame.id) as? SettingsFragment ?: SettingsFragment()
|
||||
}
|
||||
|
||||
private val progressDialog by lazy { // TODO remove this
|
||||
private val progressDialog by lazy {
|
||||
// TODO remove this
|
||||
ProgressDialog(this).apply {
|
||||
isIndeterminate = true
|
||||
setCancelable(false)
|
||||
@@ -59,9 +58,15 @@ class SettingsActivity : QkActivity<SettingsViewModel>(), SettingsView {
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
viewModel.bindView(this)
|
||||
|
||||
fragmentManager.beginTransaction()
|
||||
.replace(fragmentFrame.id, fragment)
|
||||
.commit()
|
||||
// Listen to clicks for all of the preferences
|
||||
(0 until preferences.childCount)
|
||||
.map { index -> preferences.getChildAt(index) }
|
||||
.filter { view -> view is PreferenceView }
|
||||
.forEach { preference ->
|
||||
preference.clicks().subscribe {
|
||||
preferenceClickIntent.onNext(preference as PreferenceView)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun render(state: SettingsState) {
|
||||
@@ -71,7 +76,7 @@ class SettingsActivity : QkActivity<SettingsViewModel>(), SettingsView {
|
||||
if (themeDialog.isShowing && !state.selectingTheme) themeDialog.dismiss()
|
||||
else if (!themeDialog.isShowing && state.selectingTheme) themeDialog.show()
|
||||
|
||||
fragment.preferenceManager?.findPreference("defaultSms")?.setSummary(when (state.isDefaultSmsApp) {
|
||||
defaultSms.summary = getString(when (state.isDefaultSmsApp) {
|
||||
true -> R.string.settings_default_sms_summary_true
|
||||
else -> R.string.settings_default_sms_summary_false
|
||||
})
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.moez.QKSMS.presentation.settings
|
||||
|
||||
import android.os.Bundle
|
||||
import android.preference.Preference
|
||||
import android.preference.PreferenceFragment
|
||||
import com.moez.QKSMS.R
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
|
||||
class SettingsFragment : PreferenceFragment() {
|
||||
|
||||
val preferencesAdded: Subject<Unit> = PublishSubject.create()
|
||||
val preferenceClicks: Subject<Preference> = PublishSubject.create()
|
||||
val preferenceChanges: Subject<PreferenceChange> = PublishSubject.create()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
addPreferencesFromResource(R.xml.settings)
|
||||
|
||||
preferencesAdded.onNext(Unit)
|
||||
|
||||
val clickListener = Preference.OnPreferenceClickListener {
|
||||
preferenceClicks.onNext(it)
|
||||
true
|
||||
}
|
||||
|
||||
val changeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
|
||||
preferenceChanges.onNext(PreferenceChange(preference, newValue))
|
||||
true
|
||||
}
|
||||
|
||||
(0 until preferenceScreen.preferenceCount)
|
||||
.map { index -> preferenceScreen.getPreference(index) }
|
||||
.forEach { preference ->
|
||||
preference.onPreferenceClickListener = clickListener
|
||||
preference.onPreferenceChangeListener = changeListener
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +1,11 @@
|
||||
package com.moez.QKSMS.presentation.settings
|
||||
|
||||
import android.preference.Preference
|
||||
import com.moez.QKSMS.presentation.base.QkView
|
||||
import com.moez.QKSMS.presentation.view.PreferenceView
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.subjects.Subject
|
||||
|
||||
interface SettingsView : QkView<SettingsState> {
|
||||
|
||||
val preferencesAddedIntent: Observable<Unit>
|
||||
val preferenceClickIntent: Observable<Preference>
|
||||
val preferenceChangeIntent: Observable<PreferenceChange>
|
||||
|
||||
val preferenceClickIntent: Subject<PreferenceView>
|
||||
val themeSelectedIntent: Observable<Int>
|
||||
|
||||
}
|
||||
|
||||
data class PreferenceChange(val preference: Preference, val newValue: Any)
|
||||
@@ -4,6 +4,7 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.provider.Telephony
|
||||
import com.f2prateek.rx.preferences2.RxSharedPreferences
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.di.AppComponentManager
|
||||
import com.moez.QKSMS.domain.interactor.FullSync
|
||||
import com.moez.QKSMS.presentation.base.QkViewModel
|
||||
@@ -32,16 +33,11 @@ class SettingsViewModel : QkViewModel<SettingsView, SettingsState>(SettingsState
|
||||
override fun bindView(view: SettingsView) {
|
||||
super.bindView(view)
|
||||
|
||||
// Force update the view once we receive notification that the fragment's preference view has been created
|
||||
intents += view.preferencesAddedIntent.subscribe {
|
||||
newState { it.copy() }
|
||||
}
|
||||
|
||||
intents += view.preferenceClickIntent.subscribe {
|
||||
Timber.v("Preference click: ${it.key}")
|
||||
Timber.v("Preference click: ${context.resources.getResourceName(it.id)}")
|
||||
|
||||
when (it.key) {
|
||||
"defaultSms" -> {
|
||||
when (it.id) {
|
||||
R.id.defaultSms -> {
|
||||
val intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
|
||||
if (Telephony.Sms.getDefaultSmsPackage(context) != context.packageName) {
|
||||
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, context.packageName)
|
||||
@@ -49,11 +45,11 @@ class SettingsViewModel : QkViewModel<SettingsView, SettingsState>(SettingsState
|
||||
context.startActivity(intent)
|
||||
}
|
||||
|
||||
"theme" -> {
|
||||
R.id.theme -> {
|
||||
newState { it.copy(selectingTheme = true) }
|
||||
}
|
||||
|
||||
"sync" -> {
|
||||
R.id.sync -> {
|
||||
newState { it.copy(syncing = true) }
|
||||
fullSync.execute(Unit, {
|
||||
newState { it.copy(syncing = false) }
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.moez.QKSMS.presentation.view
|
||||
|
||||
import android.content.Context
|
||||
import android.support.v7.widget.LinearLayoutCompat
|
||||
import android.util.AttributeSet
|
||||
import android.view.View
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.util.extensions.setVisible
|
||||
import kotlinx.android.synthetic.main.preference_view.view.*
|
||||
|
||||
class PreferenceView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null)
|
||||
: LinearLayoutCompat(context, attrs) {
|
||||
|
||||
var title: String? = null
|
||||
set(value) {
|
||||
field = value
|
||||
titleView.text = value
|
||||
}
|
||||
|
||||
var summary: String? = null
|
||||
set(value) {
|
||||
field = value
|
||||
summaryView.text = value
|
||||
summaryView.setVisible(value?.isNotEmpty() == true)
|
||||
}
|
||||
|
||||
init {
|
||||
View.inflate(context, R.layout.preference_view, this)
|
||||
setBackgroundResource(R.drawable.ripple)
|
||||
|
||||
context.obtainStyledAttributes(attrs, R.styleable.PreferenceView)?.run {
|
||||
title = getString(R.styleable.PreferenceView_title)
|
||||
summary = getString(R.styleable.PreferenceView_summary)
|
||||
getResourceId(R.styleable.PreferenceView_widget, -1).takeIf { it != -1 }?.run {
|
||||
View.inflate(context, this, widgetFrame)
|
||||
}
|
||||
recycle()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<merge xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical"
|
||||
tools:parentTag="com.moez.QKSMS.presentation.view.PreferenceView">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/titleView"
|
||||
style="@style/TextPrimary"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
tools:text="Default SMS App" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/summaryView"
|
||||
style="@style/TextSecondary"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
tools:text="QKSMS is your default SMS app" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/widgetFrame"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</merge>
|
||||
@@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
@@ -7,9 +8,42 @@
|
||||
<include layout="@layout/toolbar" />
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/fragmentFrame"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1">
|
||||
|
||||
<ScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/preferences"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<com.moez.QKSMS.presentation.view.PreferenceView
|
||||
android:id="@+id/defaultSms"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:title="@string/settings_default_sms_title" />
|
||||
|
||||
<com.moez.QKSMS.presentation.view.PreferenceView
|
||||
android:id="@+id/theme"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:title="@string/settings_theme_title" />
|
||||
|
||||
<com.moez.QKSMS.presentation.view.PreferenceView
|
||||
android:id="@+id/sync"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:summary="@string/settings_sync_summary"
|
||||
app:title="@string/settings_sync_title" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
||||
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<declare-styleable name="PreferenceView">
|
||||
<attr name="title" format="string" />
|
||||
<attr name="summary" format="string" />
|
||||
<attr name="key" format="string" />
|
||||
<attr name="widget" format="reference" />
|
||||
</declare-styleable>
|
||||
|
||||
</resources>
|
||||
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<Preference
|
||||
android:key="defaultSms"
|
||||
android:title="@string/settings_default_sms_title" />
|
||||
|
||||
<Preference
|
||||
android:key="theme"
|
||||
android:title="@string/settings_theme_title" />
|
||||
|
||||
<Preference
|
||||
android:key="sync"
|
||||
android:summary="@string/settings_sync_summary"
|
||||
android:title="@string/settings_sync_title" />
|
||||
|
||||
</PreferenceScreen>
|
||||
Reference in New Issue
Block a user