forked from upstream-mirrors/quik
Move packages to com.moez.QKSMS
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.common.base
|
||||
|
||||
import android.support.annotation.CallSuper
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.disposables.Disposable
|
||||
|
||||
/**
|
||||
* Base RecyclerView.Adapter that provides some convenience when creating a new Adapter, such as
|
||||
* data list handing and item animations
|
||||
*/
|
||||
abstract class FlowableAdapter<T> : QkAdapter<T>() {
|
||||
|
||||
var flowable: Flowable<List<T>>? = null
|
||||
set(value) {
|
||||
if (field === value) return
|
||||
|
||||
field = value
|
||||
|
||||
// Stop listening for updates on the old flowable
|
||||
dispose()
|
||||
|
||||
// If we're attached to any RecyclerViews, then subscribe to updates
|
||||
if (recyclerViews.isNotEmpty()) {
|
||||
subscribe()
|
||||
}
|
||||
}
|
||||
|
||||
private val recyclerViews = ArrayList<RecyclerView>()
|
||||
private var disposable: Disposable? = null
|
||||
|
||||
@CallSuper
|
||||
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
|
||||
|
||||
// If this is the first RecyclerView to be attached, then start listening to updates
|
||||
if (recyclerViews.isEmpty() || disposable == null) {
|
||||
subscribe()
|
||||
}
|
||||
|
||||
recyclerViews.add(recyclerView)
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
|
||||
recyclerViews.remove(recyclerView)
|
||||
|
||||
// If no more RecyclerViews are attached, stop listening to updates
|
||||
if (recyclerViews.isEmpty()) {
|
||||
dispose()
|
||||
}
|
||||
}
|
||||
|
||||
private fun subscribe() {
|
||||
disposable = flowable?.subscribe {
|
||||
data = it
|
||||
}
|
||||
}
|
||||
|
||||
private fun dispose() {
|
||||
disposable?.dispose()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.common.base
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.Bundle
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.toolbar.*
|
||||
|
||||
abstract class QkActivity : AppCompatActivity() {
|
||||
|
||||
protected val menu: Subject<Menu> = BehaviorSubject.create()
|
||||
|
||||
@SuppressLint("InlinedApi")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
onNewIntent(intent)
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
return when (item.itemId) {
|
||||
android.R.id.home -> {
|
||||
onBackPressed()
|
||||
true
|
||||
}
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
|
||||
override fun setContentView(layoutResID: Int) {
|
||||
super.setContentView(layoutResID)
|
||||
setSupportActionBar(toolbar)
|
||||
title = title // The title may have been set before layout inflation
|
||||
}
|
||||
|
||||
override fun setTitle(titleId: Int) {
|
||||
title = getString(titleId)
|
||||
}
|
||||
|
||||
override fun setTitle(title: CharSequence?) {
|
||||
super.setTitle(title)
|
||||
toolbarTitle?.text = title
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
val result = super.onCreateOptionsMenu(menu)
|
||||
if (menu != null) {
|
||||
this.menu.onNext(menu)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
protected open fun showBackButton(show: Boolean) {
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(show)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.common.base
|
||||
|
||||
import android.support.v7.util.DiffUtil
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.view.View
|
||||
import com.moez.QKSMS.common.util.extensions.setVisible
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
|
||||
/**
|
||||
* Base RecyclerView.Adapter that provides some convenience when creating a new Adapter, such as
|
||||
* data list handing and item animations
|
||||
*/
|
||||
abstract class QkAdapter<T> : RecyclerView.Adapter<QkViewHolder>() {
|
||||
|
||||
var data: List<T> = ArrayList()
|
||||
set(value) {
|
||||
if (field === value) return
|
||||
|
||||
val diff = DiffUtil.calculateDiff(getDiffUtilCallback(field, value))
|
||||
field = value
|
||||
diff.dispatchUpdatesTo(this)
|
||||
onDatasetChanged()
|
||||
|
||||
emptyView?.setVisible(value.isEmpty())
|
||||
}
|
||||
|
||||
/**
|
||||
* This view can be set, and the adapter will automatically control the visibility of this view
|
||||
* based on the data
|
||||
*/
|
||||
var emptyView: View? = null
|
||||
set(value) {
|
||||
field = value
|
||||
value?.setVisible(false)
|
||||
}
|
||||
|
||||
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
|
||||
|
||||
private val selection = mutableListOf<Long>()
|
||||
|
||||
/**
|
||||
* Toggles the selected state for a particular view
|
||||
*
|
||||
* If we are currently in selection mode (we have an active selection), then the state will
|
||||
* toggle. If we are not in selection mode, then we will only toggle if [force]
|
||||
*/
|
||||
protected fun toggleSelection(id: Long, force: Boolean = true): Boolean {
|
||||
if (!force && selection.isEmpty()) return false
|
||||
|
||||
when (selection.contains(id)) {
|
||||
true -> selection.remove(id)
|
||||
false -> selection.add(id)
|
||||
}
|
||||
|
||||
selectionChanges.onNext(selection)
|
||||
return true
|
||||
}
|
||||
|
||||
protected fun isSelected(id: Long): Boolean {
|
||||
return selection.contains(id)
|
||||
}
|
||||
|
||||
fun clearSelection() {
|
||||
selection.clear()
|
||||
selectionChanges.onNext(selection)
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
fun getItem(position: Int): T {
|
||||
return data[position]
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return data.size
|
||||
}
|
||||
|
||||
open fun onDatasetChanged() {}
|
||||
|
||||
/**
|
||||
* Allows the adapter implementation to provide a custom DiffUtil.Callback
|
||||
* If not, then the abstract implementation will be used
|
||||
*/
|
||||
private fun getDiffUtilCallback(oldData: List<T>, newData: List<T>): DiffUtil.Callback {
|
||||
return object : DiffUtil.Callback() {
|
||||
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
|
||||
areItemsTheSame(oldData[oldItemPosition], newData[newItemPosition])
|
||||
|
||||
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
|
||||
areContentsTheSame(oldData[oldItemPosition], newData[newItemPosition])
|
||||
|
||||
override fun getOldListSize() = oldData.size
|
||||
|
||||
override fun getNewListSize() = newData.size
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun areItemsTheSame(old: T, new: T): Boolean {
|
||||
return old == new
|
||||
}
|
||||
|
||||
protected open fun areContentsTheSame(old: T, new: T): Boolean {
|
||||
return old == new
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.moez.QKSMS.common.base
|
||||
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.view.View
|
||||
import com.moez.QKSMS.common.util.extensions.setVisible
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import io.realm.OrderedRealmCollection
|
||||
import io.realm.RealmList
|
||||
import io.realm.RealmModel
|
||||
import io.realm.RealmRecyclerViewAdapter
|
||||
import io.realm.RealmResults
|
||||
|
||||
abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkViewHolder>(null, true) {
|
||||
|
||||
/**
|
||||
* This view can be set, and the adapter will automatically control the visibility of this view
|
||||
* based on the data
|
||||
*/
|
||||
var emptyView: View? = null
|
||||
|
||||
private val emptyListener: (OrderedRealmCollection<T>) -> Unit = { data ->
|
||||
emptyView?.setVisible(data.isLoaded && data.isEmpty())
|
||||
}
|
||||
|
||||
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
|
||||
|
||||
private val selection = mutableListOf<Long>()
|
||||
|
||||
/**
|
||||
* Toggles the selected state for a particular view
|
||||
*
|
||||
* If we are currently in selection mode (we have an active selection), then the state will
|
||||
* toggle. If we are not in selection mode, then we will only toggle if [force]
|
||||
*/
|
||||
protected fun toggleSelection(id: Long, force: Boolean = true): Boolean {
|
||||
if (!force && selection.isEmpty()) return false
|
||||
|
||||
when (selection.contains(id)) {
|
||||
true -> selection.remove(id)
|
||||
false -> selection.add(id)
|
||||
}
|
||||
|
||||
selectionChanges.onNext(selection)
|
||||
return true
|
||||
}
|
||||
|
||||
protected fun isSelected(id: Long): Boolean {
|
||||
return selection.contains(id)
|
||||
}
|
||||
|
||||
fun clearSelection() {
|
||||
selection.clear()
|
||||
selectionChanges.onNext(selection)
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
override fun updateData(data: OrderedRealmCollection<T>?) {
|
||||
if (getData() === data) return
|
||||
|
||||
removeListener(getData())
|
||||
addListener(data)
|
||||
|
||||
data?.run(emptyListener)
|
||||
|
||||
super.updateData(data)
|
||||
}
|
||||
|
||||
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
|
||||
super.onAttachedToRecyclerView(recyclerView)
|
||||
addListener(data)
|
||||
}
|
||||
|
||||
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
|
||||
super.onDetachedFromRecyclerView(recyclerView)
|
||||
removeListener(data)
|
||||
}
|
||||
|
||||
private fun addListener(data: OrderedRealmCollection<T>?) {
|
||||
when (data) {
|
||||
is RealmResults<T> -> data.addChangeListener(emptyListener)
|
||||
is RealmList<T> -> data.addChangeListener(emptyListener)
|
||||
}
|
||||
}
|
||||
|
||||
private fun removeListener(data: OrderedRealmCollection<T>?) {
|
||||
when (data) {
|
||||
is RealmResults<T> -> data.removeChangeListener(emptyListener)
|
||||
is RealmList<T> -> data.removeChangeListener(emptyListener)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.common.base
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.ActivityManager
|
||||
import android.arch.lifecycle.Lifecycle
|
||||
import android.graphics.BitmapFactory
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.util.Colors
|
||||
import com.moez.QKSMS.common.util.extensions.resolveThemeColor
|
||||
import com.moez.QKSMS.util.Preferences
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.rxkotlin.Observables
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.toolbar.*
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Base activity that automatically applies any necessary theme theme settings and colors
|
||||
*
|
||||
* In most cases, this should be used instead of the base QkActivity, except for when
|
||||
* an activity does not depend on the theme
|
||||
*/
|
||||
abstract class QkThemedActivity : QkActivity() {
|
||||
|
||||
@Inject lateinit var colors: Colors
|
||||
@Inject lateinit var prefs: Preferences
|
||||
|
||||
/**
|
||||
* In case the activity should be themed for a specific conversation, the selected conversation
|
||||
* can be changed by pushing the threadId to this subject
|
||||
*/
|
||||
protected val threadId: Subject<Long> = BehaviorSubject.createDefault(0)
|
||||
|
||||
/**
|
||||
* Switch the theme if the threadId changes
|
||||
*/
|
||||
protected val theme = threadId
|
||||
.distinctUntilChanged()
|
||||
.switchMap { threadId -> colors.themeObservable(threadId) }
|
||||
|
||||
@SuppressLint("InlinedApi")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
|
||||
val night = prefs.night.get()
|
||||
val black = prefs.black.get()
|
||||
setTheme(getActivityThemeRes(night, black))
|
||||
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
// When certain preferences change, we need to recreate the activity
|
||||
Observable.merge(
|
||||
listOf(prefs.night, prefs.black, prefs.textSize, prefs.systemFont).map { it.asObservable().skip(1) })
|
||||
.autoDisposable(scope())
|
||||
.subscribe { recreate() }
|
||||
|
||||
// Set the color for the status bar icons
|
||||
// If night mode, or no dark icons supported, use light icons
|
||||
// If night mode and only dark status icons supported, use dark status icons
|
||||
// If night mode and all dark icons supported, use all dark icons
|
||||
window.decorView.systemUiVisibility = when {
|
||||
night || Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> 0
|
||||
Build.VERSION.SDK_INT < Build.VERSION_CODES.O -> View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
|
||||
else -> View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
|
||||
}
|
||||
|
||||
// Some devices don't let you modify android.R.attr.navigationBarColor
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
window.navigationBarColor = resolveThemeColor(android.R.attr.windowBackground)
|
||||
}
|
||||
|
||||
// Set the color for the overflow and navigation icon
|
||||
val textTertiary = resolveThemeColor(android.R.attr.textColorTertiary)
|
||||
toolbar?.overflowIcon = toolbar?.overflowIcon?.apply { setTint(textTertiary) }
|
||||
toolbar?.navigationIcon = toolbar?.navigationIcon?.apply { setTint(textTertiary) }
|
||||
|
||||
// Set the color for the recent apps title
|
||||
val toolbarColor = resolveThemeColor(R.attr.colorPrimary)
|
||||
val icon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
|
||||
val taskDesc = ActivityManager.TaskDescription(getString(R.string.app_name), icon, toolbarColor)
|
||||
setTaskDescription(taskDesc)
|
||||
}
|
||||
|
||||
override fun onPostCreate(savedInstanceState: Bundle?) {
|
||||
super.onPostCreate(savedInstanceState)
|
||||
|
||||
// Update the colours of the menu items
|
||||
Observables.combineLatest(menu, theme, { menu, theme ->
|
||||
val text = resolveThemeColor(android.R.attr.textColorSecondary)
|
||||
(0 until menu.size())
|
||||
.map { position -> menu.getItem(position) }
|
||||
.forEach { menuItem ->
|
||||
menuItem?.icon?.run {
|
||||
setTint(when (menuItem.itemId) {
|
||||
in getColoredMenuItems() -> theme.theme
|
||||
else -> text
|
||||
})
|
||||
|
||||
menuItem.icon = this
|
||||
}
|
||||
}
|
||||
}).autoDisposable(scope(Lifecycle.Event.ON_DESTROY)).subscribe()
|
||||
}
|
||||
|
||||
open fun getColoredMenuItems(): List<Int> {
|
||||
return listOf()
|
||||
}
|
||||
|
||||
/**
|
||||
* This can be overridden in case an activity does not want to use the default themes
|
||||
*/
|
||||
open fun getActivityThemeRes(night: Boolean, black: Boolean) = when {
|
||||
night && black -> R.style.AppThemeBlack
|
||||
night && !black -> R.style.AppThemeDark
|
||||
else -> R.style.AppThemeLight
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.common.base
|
||||
|
||||
import android.arch.lifecycle.LifecycleOwner
|
||||
|
||||
interface QkView<in State> : LifecycleOwner {
|
||||
|
||||
fun render(state: State)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.common.base
|
||||
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.view.View
|
||||
|
||||
class QkViewHolder(view: View) : RecyclerView.ViewHolder(view)
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.common.base
|
||||
|
||||
import android.arch.lifecycle.ViewModel
|
||||
import android.support.annotation.CallSuper
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
|
||||
abstract class QkViewModel<in View : QkView<State>, State>(initialState: State) : ViewModel() {
|
||||
|
||||
protected val state: BehaviorSubject<State> = BehaviorSubject.createDefault(initialState)
|
||||
|
||||
protected val disposables = CompositeDisposable()
|
||||
|
||||
@CallSuper
|
||||
open fun bindView(view: View) {
|
||||
state
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.render(it) }
|
||||
}
|
||||
|
||||
protected fun newState(reducer: State.() -> State) {
|
||||
state.value?.let { state.onNext(reducer(it)) }
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
disposables.dispose()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user