Flatten hierarchy in presentation module

This commit is contained in:
Moez Bhatti
2018-03-22 01:43:09 -04:00
parent 2e4d96059a
commit 7212983ffe
95 changed files with 51 additions and 65 deletions
@@ -0,0 +1,133 @@
/*
* 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 presentation.feature.themepicker
import android.content.Context
import android.content.res.Resources
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayout
import com.moez.QKSMS.R
import common.util.Colors
import common.util.extensions.dpToPx
import common.util.extensions.setBackgroundTint
import common.util.extensions.setTint
import common.util.extensions.setVisible
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.theme_list_item.view.*
import kotlinx.android.synthetic.main.theme_palette_list_item.view.*
import presentation.common.base.QkAdapter
import presentation.common.base.QkViewHolder
import javax.inject.Inject
class ThemeAdapter @Inject constructor(
private val context: Context,
private val colors: Colors
) : QkAdapter<List<Int>>() {
var threadId: Long = 0
set(value) {
field = value
threadIdChanged()
}
val colorSelected: Subject<Int> = PublishSubject.create()
var selectedColor: Int = -1
set(value) {
val oldPosition = data.indexOfFirst { it.contains(field) }
val newPosition = data.indexOfFirst { it.contains(value) }
field = value
oldPosition.takeIf { it != -1 }?.let { position -> notifyItemChanged(position) }
newPosition.takeIf { it != -1 }?.let { position -> notifyItemChanged(position) }
}
private var iconTint = 0
private val disposables = CompositeDisposable()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.theme_palette_list_item, parent, false)
view.palette.flexWrap = FlexWrap.WRAP
view.palette.flexDirection = FlexDirection.ROW
return QkViewHolder(view)
}
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val palette = getItem(position)
val view = holder.itemView
val screenWidth = Resources.getSystem().displayMetrics.widthPixels
val minPadding = (16 * 6).dpToPx(context)
val size = if (screenWidth - minPadding > (56 * 5).dpToPx(context)) {
56.dpToPx(context)
} else {
(screenWidth - minPadding) / 5
}
val swatchPadding = (screenWidth - size * 5) / 12
view.palette.removeAllViews()
view.palette.setPadding(swatchPadding, swatchPadding, swatchPadding, swatchPadding)
(palette.subList(0, 5) + palette.subList(5, 10).reversed())
.mapIndexed { index, color ->
LayoutInflater.from(context).inflate(R.layout.theme_list_item, view.palette, false).apply {
// Send clicks to the selected subject
setOnClickListener { colorSelected.onNext(color) }
// Apply the color to the view
theme.setBackgroundTint(color)
// Control the check visibility and tint
check.setVisible(color == selectedColor)
check.setTint(iconTint)
// Update the size so that the spacing is perfectly even
layoutParams = (layoutParams as FlexboxLayout.LayoutParams).apply {
height = size
width = size
isWrapBefore = index % 5 == 0
setMargins(swatchPadding, swatchPadding, swatchPadding, swatchPadding)
}
}
}
.forEach { theme -> view.palette.addView(theme) }
}
private fun threadIdChanged() {
disposables.clear()
disposables += colors.textPrimaryOnThemeForConversation(threadId)
.subscribe { color -> iconTint = color }
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
disposables.clear()
}
}
@@ -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 presentation.feature.themepicker
import android.content.Context
import android.support.v4.view.PagerAdapter
import android.view.View
import android.view.ViewGroup
import com.moez.QKSMS.R
import javax.inject.Inject
class ThemePagerAdapter @Inject constructor(private val context: Context) : PagerAdapter() {
override fun instantiateItem(container: ViewGroup, position: Int): Any {
return when (position) {
1 -> container.findViewById(R.id.rgbPicker)
else -> container.findViewById(R.id.materialColors)
}
}
override fun getPageTitle(position: Int): CharSequence? {
return when (position) {
1 -> context.getString(R.string.theme_rgb)
else -> context.getString(R.string.theme_material)
}
}
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view == `object`
}
override fun getCount(): Int {
return 2
}
}
@@ -0,0 +1,74 @@
/*
* 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 presentation.feature.themepicker
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import injection.appComponent
import common.util.extensions.pageScrolled
import kotlinx.android.synthetic.main.theme_picker_activity.*
import presentation.common.base.QkThemedActivity
import javax.inject.Inject
class ThemePickerActivity : QkThemedActivity<ThemePickerViewModel>(), ThemePickerView {
override val viewModelClass = ThemePickerViewModel::class
override val themeSelectedIntent by lazy { themeAdapter.colorSelected }
override val pageScrolledIntent by lazy { pager.pageScrolled() }
@Inject lateinit var themeAdapter: ThemeAdapter
@Inject lateinit var themePagerAdapter: ThemePagerAdapter
init {
appComponent.inject(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.theme_picker_activity)
setTitle(R.string.title_theme)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
viewModel.bindView(this)
pager.offscreenPageLimit = 1
pager.adapter = themePagerAdapter
tabs.pager = pager
themeAdapter.data = colors.materialColors
materialColors.layoutManager = LinearLayoutManager(this)
materialColors.adapter = themeAdapter
colors.background
.autoDisposable(scope())
.subscribe { color -> window.decorView.setBackgroundColor(color) }
}
override fun render(state: ThemePickerState) {
rgbPicker.alpha = state.rgbAlpha
rgbPicker.setBackgroundColor(state.selectedColor)
themeAdapter.threadId = state.threadId
themeAdapter.selectedColor = state.selectedColor
}
}
@@ -0,0 +1,25 @@
/*
* 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 presentation.feature.themepicker
data class ThemePickerState(
val threadId: Long = 0,
val selectedColor: Int = -1,
val rgbAlpha: Float = 0f
)
@@ -0,0 +1,30 @@
/*
* 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 presentation.feature.themepicker
import common.util.extensions.PageScroll
import io.reactivex.Observable
import presentation.common.base.QkView
interface ThemePickerView : QkView<ThemePickerState> {
val pageScrolledIntent: Observable<PageScroll>
val themeSelectedIntent: Observable<Int>
}
@@ -0,0 +1,64 @@
/*
* 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 presentation.feature.themepicker
import android.content.Intent
import com.f2prateek.rx.preferences2.Preference
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import injection.appComponent
import util.Preferences
import io.reactivex.rxkotlin.plusAssign
import presentation.common.base.QkViewModel
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class ThemePickerViewModel(intent: Intent) : QkViewModel<ThemePickerView, ThemePickerState>(ThemePickerState()) {
@Inject lateinit var prefs: Preferences
private val threadId = intent.extras?.getLong("threadId") ?: 0L
private val theme: Preference<Int>
init {
appComponent.inject(this)
newState { it.copy(threadId = threadId) }
theme = prefs.theme(threadId)
disposables += theme.asObservable()
.subscribe { color -> newState { it.copy(selectedColor = color) } }
}
override fun bindView(view: ThemePickerView) {
super.bindView(view)
view.pageScrolledIntent
.throttleFirst(16, TimeUnit.MILLISECONDS)
.map { (position, positionOffset) -> if (position == 0) positionOffset * positionOffset else 1f }
.autoDisposable(view.scope())
.subscribe { alpha -> newState { it.copy(rgbAlpha = alpha) } }
view.themeSelectedIntent
.autoDisposable(view.scope())
.subscribe { color -> theme.set(color) }
}
}