forked from upstream-mirrors/quik
#1036 - Swipe between pictures
This commit is contained in:
@@ -18,8 +18,6 @@
|
||||
*/
|
||||
package com.moez.QKSMS.common
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.ActivityOptions
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
@@ -28,7 +26,6 @@ import android.os.Build
|
||||
import android.provider.ContactsContract
|
||||
import android.provider.Settings
|
||||
import android.provider.Telephony
|
||||
import android.view.View
|
||||
import com.moez.QKSMS.BuildConfig
|
||||
import com.moez.QKSMS.feature.backup.BackupActivity
|
||||
import com.moez.QKSMS.feature.blocked.BlockedActivity
|
||||
@@ -115,18 +112,6 @@ class Navigator @Inject constructor(
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the attachment full-screen
|
||||
* The transitionName for the view should be the id of the image being displayed
|
||||
*/
|
||||
fun showImageAnimated(from: Activity, view: View) {
|
||||
val intent = Intent(context, GalleryActivity::class.java)
|
||||
intent.putExtra("partId", view.transitionName.toLong())
|
||||
|
||||
val options = ActivityOptions.makeSceneTransitionAnimation(from, view, view.transitionName)
|
||||
from.startActivity(intent, options.toBundle())
|
||||
}
|
||||
|
||||
fun showBackup() {
|
||||
analyticsManager.track("Viewed Backup")
|
||||
startActivity(Intent(context, BackupActivity::class.java))
|
||||
|
||||
@@ -31,6 +31,7 @@ import android.widget.EditText
|
||||
import android.widget.ImageView
|
||||
import android.widget.ProgressBar
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.viewpager.widget.ViewPager
|
||||
|
||||
var ViewGroup.animateLayoutChanges: Boolean
|
||||
get() = layoutTransition != null
|
||||
@@ -105,6 +106,14 @@ fun View.forwardTouches(parent: View) {
|
||||
}
|
||||
}
|
||||
|
||||
fun ViewPager.addOnPageChangeListener(listener: (Int) -> Unit) {
|
||||
addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
|
||||
override fun onPageSelected(position: Int) {
|
||||
listener(position)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun RecyclerView.scrapViews() {
|
||||
val adapter = adapter
|
||||
val layoutManager = layoutManager
|
||||
|
||||
-4
@@ -67,10 +67,6 @@ class ConversationInfoController(val threadId: Long = 0) : QkController<Conversa
|
||||
override fun onViewCreated() {
|
||||
items.postDelayed({ items?.animateLayoutChanges = true }, 100)
|
||||
|
||||
mediaAdapter.thumbnailClicks
|
||||
.autoDisposable(scope())
|
||||
.subscribe { view -> navigator.showImageAnimated(activity!!, view) }
|
||||
|
||||
recipients.adapter = recipientAdapter
|
||||
|
||||
media.adapter = mediaAdapter
|
||||
|
||||
+1
-7
@@ -20,18 +20,15 @@ package com.moez.QKSMS.feature.conversationinfo
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.Navigator
|
||||
import com.moez.QKSMS.common.base.QkRealmAdapter
|
||||
import com.moez.QKSMS.common.base.QkViewHolder
|
||||
import com.moez.QKSMS.common.util.extensions.setVisible
|
||||
import com.moez.QKSMS.extensions.isImage
|
||||
import com.moez.QKSMS.extensions.isVideo
|
||||
import com.moez.QKSMS.model.MmsPart
|
||||
import com.moez.QKSMS.util.GlideApp
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import kotlinx.android.synthetic.main.conversation_media_list_item.view.*
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -40,15 +37,13 @@ class ConversationMediaAdapter @Inject constructor(
|
||||
private val navigator: Navigator
|
||||
) : QkRealmAdapter<MmsPart>() {
|
||||
|
||||
val thumbnailClicks: PublishSubject<View> = PublishSubject.create()
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
|
||||
val inflater = LayoutInflater.from(parent.context)
|
||||
val view = inflater.inflate(R.layout.conversation_media_list_item, parent, false)
|
||||
return QkViewHolder(view).apply {
|
||||
view.thumbnail.setOnClickListener {
|
||||
val part = getItem(adapterPosition)!!
|
||||
if (part.isImage()) thumbnailClicks.onNext(it) else navigator.showMedia(part.id)
|
||||
navigator.showMedia(part.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,7 +58,6 @@ class ConversationMediaAdapter @Inject constructor(
|
||||
.into(view.thumbnail)
|
||||
|
||||
view.video.setVisible(part.isVideo())
|
||||
view.thumbnail.transitionName = part.id.toString()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,145 +18,71 @@
|
||||
*/
|
||||
package com.moez.QKSMS.feature.gallery
|
||||
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.transition.ChangeBounds
|
||||
import android.transition.ChangeImageTransform
|
||||
import android.transition.TransitionSet
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import com.google.android.exoplayer2.ExoPlayerFactory
|
||||
import com.google.android.exoplayer2.SimpleExoPlayer
|
||||
import com.google.android.exoplayer2.source.ExtractorMediaSource
|
||||
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection
|
||||
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
|
||||
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
|
||||
import com.google.android.exoplayer2.util.Util
|
||||
import com.google.android.mms.ContentType
|
||||
import com.jakewharton.rxbinding2.view.clicks
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.GlideCompletionListener
|
||||
import com.moez.QKSMS.common.base.QkActivity
|
||||
import com.moez.QKSMS.common.util.extensions.addOnPageChangeListener
|
||||
import com.moez.QKSMS.common.util.extensions.setVisible
|
||||
import com.moez.QKSMS.util.GlideApp
|
||||
import com.moez.QKSMS.model.MmsPart
|
||||
import dagger.android.AndroidInjection
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.gallery_activity.*
|
||||
import javax.inject.Inject
|
||||
|
||||
|
||||
class GalleryActivity : QkActivity(), GalleryView {
|
||||
|
||||
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
||||
@Inject lateinit var pagerAdapter: GalleryPagerAdapter
|
||||
|
||||
override val screenTouchedIntent by lazy { image.clicks() }
|
||||
override val optionsItemSelectedIntent: Subject<Int> = PublishSubject.create()
|
||||
|
||||
private val optionsItemSubject: Subject<Int> = PublishSubject.create()
|
||||
private val pageChangedSubject: Subject<MmsPart> = PublishSubject.create()
|
||||
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory)[GalleryViewModel::class.java] }
|
||||
|
||||
private var exoPlayer: SimpleExoPlayer? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
AndroidInjection.inject(this)
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.gallery_activity)
|
||||
postponeEnterTransition()
|
||||
showBackButton(true)
|
||||
viewModel.bindView(this)
|
||||
|
||||
val transition = TransitionSet().apply {
|
||||
duration = 100
|
||||
pagerAdapter.setInitialPositionHandler = { position ->
|
||||
pager.setCurrentItem(position, false)
|
||||
|
||||
addTransition(ChangeBounds())
|
||||
addTransition(ChangeImageTransform())
|
||||
}
|
||||
|
||||
window.sharedElementReturnTransition = transition
|
||||
window.sharedElementEnterTransition = transition
|
||||
|
||||
// FIXME: Setting a transitionName breaks GIF playback
|
||||
// image.transitionName = intent.getLongExtra("partId", 0L).toString()
|
||||
|
||||
// When calling the public setter, it doesn't allow the midscale to be the same as the
|
||||
// maxscale or the minscale. We don't want 3 levels and we don't want to modify the library
|
||||
// so let's celebrate the invention of reflection!
|
||||
image.attacher.run {
|
||||
javaClass.getDeclaredField("mMinScale").run {
|
||||
isAccessible = true
|
||||
setFloat(image.attacher, 1f)
|
||||
}
|
||||
javaClass.getDeclaredField("mMidScale").run {
|
||||
isAccessible = true
|
||||
setFloat(image.attacher, 1f)
|
||||
}
|
||||
javaClass.getDeclaredField("mMaxScale").run {
|
||||
isAccessible = true
|
||||
setFloat(image.attacher, 3f)
|
||||
// The ViewPager's page change listener isn't called if the initial position is 0
|
||||
if (position == 0) {
|
||||
pageChanged(0)
|
||||
}
|
||||
}
|
||||
pager.adapter = pagerAdapter
|
||||
pager.addOnPageChangeListener(this::pageChanged)
|
||||
}
|
||||
|
||||
private fun pageChanged(position: Int) {
|
||||
toolbarSubtitle.text = pagerAdapter.getPageTitle(position)
|
||||
toolbarSubtitle.isVisible = toolbarTitle.text.isNotBlank()
|
||||
|
||||
pagerAdapter.getItem(position)?.run(pageChangedSubject::onNext)
|
||||
}
|
||||
|
||||
override fun render(state: GalleryState) {
|
||||
toolbar.setVisible(state.navigationVisible)
|
||||
|
||||
title = state.title
|
||||
|
||||
when {
|
||||
ContentType.isImageType(state.type) -> showImage(state.uri)
|
||||
ContentType.isVideoType(state.type) -> showVideo(state.uri)
|
||||
else -> hideMedia()
|
||||
}
|
||||
pagerAdapter.parts = state.parts
|
||||
}
|
||||
|
||||
private fun showImage(uri: Uri?) {
|
||||
image.setVisible(true)
|
||||
video.setVisible(false)
|
||||
if (image.drawable == null) {
|
||||
override fun optionsItemSelected(): Observable<Int> = optionsItemSubject
|
||||
|
||||
// We need to explicitly request a gif from glide for animations to work
|
||||
when (uri?.let(contentResolver::getType)) {
|
||||
ContentType.IMAGE_GIF -> GlideApp.with(this)
|
||||
.asGif()
|
||||
.load(uri)
|
||||
.listener(GlideCompletionListener {
|
||||
startPostponedEnterTransition()
|
||||
})
|
||||
.into(image)
|
||||
override fun screenTouched(): Observable<*> = pagerAdapter.clicks
|
||||
|
||||
else -> GlideApp.with(this)
|
||||
.asBitmap()
|
||||
.load(uri)
|
||||
.listener(GlideCompletionListener {
|
||||
startPostponedEnterTransition()
|
||||
})
|
||||
.into(image)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showVideo(uri: Uri?) {
|
||||
image.setVisible(false)
|
||||
video.setVisible(true)
|
||||
|
||||
if (video.player == null) {
|
||||
val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(null)
|
||||
val trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)
|
||||
exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector)
|
||||
video.player = exoPlayer
|
||||
|
||||
val dataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(this, "QKSMS"))
|
||||
val videoSource = ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri)
|
||||
exoPlayer?.prepare(videoSource)
|
||||
}
|
||||
}
|
||||
|
||||
private fun hideMedia() {
|
||||
image.setVisible(false)
|
||||
video.setVisible(false)
|
||||
}
|
||||
override fun pageChanged(): Observable<MmsPart> = pageChangedSubject
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.gallery, menu)
|
||||
@@ -166,14 +92,14 @@ class GalleryActivity : QkActivity(), GalleryView {
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
android.R.id.home -> onBackPressed()
|
||||
else -> optionsItemSelectedIntent.onNext(item.itemId)
|
||||
else -> optionsItemSubject.onNext(item.itemId)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
exoPlayer?.release()
|
||||
pagerAdapter.destroy()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import com.moez.QKSMS.injection.ViewModelKey
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.multibindings.IntoMap
|
||||
import javax.inject.Named
|
||||
|
||||
@Module
|
||||
class GalleryActivityModule {
|
||||
@@ -31,6 +32,10 @@ class GalleryActivityModule {
|
||||
@Provides
|
||||
fun provideIntent(activity: GalleryActivity): Intent = activity.intent
|
||||
|
||||
@Provides
|
||||
@Named("partId")
|
||||
fun providePartId(activity: GalleryActivity): Long = activity.intent.getLongExtra("partId", 0L)
|
||||
|
||||
@Provides
|
||||
@IntoMap
|
||||
@ViewModelKey(GalleryViewModel::class)
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* 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.feature.gallery
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.viewpager.widget.PagerAdapter
|
||||
import com.google.android.exoplayer2.ExoPlayer
|
||||
import com.google.android.exoplayer2.ExoPlayerFactory
|
||||
import com.google.android.exoplayer2.source.ExtractorMediaSource
|
||||
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection
|
||||
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
|
||||
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
|
||||
import com.google.android.exoplayer2.util.Util
|
||||
import com.google.android.mms.ContentType
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.util.DateFormatter
|
||||
import com.moez.QKSMS.extensions.isImage
|
||||
import com.moez.QKSMS.extensions.isVideo
|
||||
import com.moez.QKSMS.model.MmsPart
|
||||
import com.moez.QKSMS.util.GlideApp
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import io.realm.RealmResults
|
||||
import kotlinx.android.synthetic.main.gallery_image_page.view.*
|
||||
import kotlinx.android.synthetic.main.gallery_video_page.view.*
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Named
|
||||
|
||||
class GalleryPagerAdapter @Inject constructor(
|
||||
context: Context,
|
||||
@Named("partId") private val partId: Long,
|
||||
private val dateFormatter: DateFormatter
|
||||
) : PagerAdapter() {
|
||||
|
||||
var parts: RealmResults<MmsPart>? = null
|
||||
set(value) {
|
||||
if (field === value) return
|
||||
field = value
|
||||
|
||||
field?.asFlowable()
|
||||
?.filter { it.isLoaded }
|
||||
?.subscribe { notifyDataSetChanged() }
|
||||
?.run(disposables::add)
|
||||
}
|
||||
|
||||
val clicks: Subject<View> = PublishSubject.create()
|
||||
|
||||
/**
|
||||
* The Adapter isn't able to set the position itself, so it's owner must apply the initial
|
||||
* position once the data is loaded
|
||||
*/
|
||||
var setInitialPositionHandler: ((Int) -> Unit)? = null
|
||||
|
||||
private var loaded = false
|
||||
|
||||
private val contentResolver = context.contentResolver
|
||||
private val disposables = CompositeDisposable()
|
||||
private val exoPlayers = Collections.newSetFromMap(WeakHashMap<ExoPlayer?, Boolean>())
|
||||
|
||||
override fun instantiateItem(container: ViewGroup, position: Int): Any {
|
||||
val part = getItem(position)
|
||||
val inflater = LayoutInflater.from(container.context)
|
||||
return when {
|
||||
part?.isImage() == true -> inflater.inflate(R.layout.gallery_image_page, container, false).apply {
|
||||
|
||||
// When calling the public setter, it doesn't allow the midscale to be the same as the
|
||||
// maxscale or the minscale. We don't want 3 levels and we don't want to modify the library
|
||||
// so let's celebrate the invention of reflection!
|
||||
image.attacher.run {
|
||||
javaClass.getDeclaredField("mMinScale").run {
|
||||
isAccessible = true
|
||||
setFloat(image.attacher, 1f)
|
||||
}
|
||||
javaClass.getDeclaredField("mMidScale").run {
|
||||
isAccessible = true
|
||||
setFloat(image.attacher, 1f)
|
||||
}
|
||||
javaClass.getDeclaredField("mMaxScale").run {
|
||||
isAccessible = true
|
||||
setFloat(image.attacher, 3f)
|
||||
}
|
||||
}
|
||||
|
||||
// We need to explicitly request a gif from glide for animations to work
|
||||
when (part.getUri().let(contentResolver::getType)) {
|
||||
ContentType.IMAGE_GIF -> GlideApp.with(context)
|
||||
.asGif()
|
||||
.load(part.getUri())
|
||||
.into(image)
|
||||
|
||||
else -> GlideApp.with(context)
|
||||
.asBitmap()
|
||||
.load(part.getUri())
|
||||
.into(image)
|
||||
}
|
||||
|
||||
container.addView(this)
|
||||
}
|
||||
|
||||
part?.isVideo() == true -> inflater.inflate(R.layout.gallery_video_page, container, false).apply {
|
||||
val videoTrackSelectionFactory = AdaptiveTrackSelection.Factory(null)
|
||||
val trackSelector = DefaultTrackSelector(videoTrackSelectionFactory)
|
||||
val exoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector)
|
||||
video.player = exoPlayer
|
||||
exoPlayers.add(exoPlayer)
|
||||
|
||||
val dataSourceFactory = DefaultDataSourceFactory(context, Util.getUserAgent(context, "QKSMS"))
|
||||
val videoSource = ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(part.getUri())
|
||||
exoPlayer?.prepare(videoSource)
|
||||
|
||||
container.addView(this)
|
||||
}
|
||||
|
||||
else -> inflater.inflate(R.layout.gallery_invalid_page, container, false).apply {
|
||||
container.addView(this)
|
||||
}
|
||||
}.apply { setOnClickListener(clicks::onNext) }
|
||||
}
|
||||
|
||||
override fun getPageTitle(position: Int): CharSequence? {
|
||||
return getItem(position)?.messages?.firstOrNull()?.date?.let(dateFormatter::getDetailedTimestamp)
|
||||
}
|
||||
|
||||
override fun isViewFromObject(view: View, `object`: Any): Boolean {
|
||||
return view == `object`
|
||||
}
|
||||
|
||||
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
|
||||
container.removeView(`object` as? View)
|
||||
}
|
||||
|
||||
override fun getCount() = parts?.size ?: 0
|
||||
|
||||
fun getItem(position: Int): MmsPart? = parts?.get(position)
|
||||
|
||||
override fun notifyDataSetChanged() {
|
||||
super.notifyDataSetChanged()
|
||||
|
||||
if (!loaded && parts?.isLoaded == true) {
|
||||
loaded = true
|
||||
parts?.indexOfFirst { it.id == partId }
|
||||
?.let { setInitialPositionHandler?.invoke(it) }
|
||||
}
|
||||
}
|
||||
|
||||
fun destroy() {
|
||||
disposables.dispose()
|
||||
exoPlayers.forEach { exoPlayer -> exoPlayer?.release() }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,11 +18,10 @@
|
||||
*/
|
||||
package com.moez.QKSMS.feature.gallery
|
||||
|
||||
import android.net.Uri
|
||||
import com.moez.QKSMS.model.MmsPart
|
||||
import io.realm.RealmResults
|
||||
|
||||
data class GalleryState(
|
||||
val navigationVisible: Boolean = true,
|
||||
val title: String = "",
|
||||
val uri: Uri? = null,
|
||||
val type: String = ""
|
||||
)
|
||||
val title: String? = "",
|
||||
val parts: RealmResults<MmsPart>? = null)
|
||||
@@ -19,11 +19,13 @@
|
||||
package com.moez.QKSMS.feature.gallery
|
||||
|
||||
import com.moez.QKSMS.common.base.QkView
|
||||
import com.moez.QKSMS.model.MmsPart
|
||||
import io.reactivex.Observable
|
||||
|
||||
interface GalleryView : QkView<GalleryState> {
|
||||
|
||||
val screenTouchedIntent: Observable<Unit>
|
||||
val optionsItemSelectedIntent: Observable<Int>
|
||||
fun optionsItemSelected(): Observable<Int>
|
||||
fun screenTouched(): Observable<*>
|
||||
fun pageChanged(): Observable<MmsPart>
|
||||
|
||||
}
|
||||
@@ -19,7 +19,6 @@
|
||||
package com.moez.QKSMS.feature.gallery
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.androidxcompat.scope
|
||||
import com.moez.QKSMS.common.base.QkViewModel
|
||||
@@ -33,44 +32,39 @@ import io.reactivex.Flowable
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.rxkotlin.withLatestFrom
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Named
|
||||
|
||||
class GalleryViewModel @Inject constructor(
|
||||
private val intent: Intent,
|
||||
conversationRepo: ConversationRepository,
|
||||
messageRepo: MessageRepository,
|
||||
@Named("partId") private val partId: Long,
|
||||
private val context: Context,
|
||||
private val conversationRepo: ConversationRepository,
|
||||
private val messageRepo: MessageRepository,
|
||||
private val saveImage: SaveImage
|
||||
) : QkViewModel<GalleryView, GalleryState>(GalleryState()) {
|
||||
|
||||
private val partIdFlowable = Flowable.just(intent)
|
||||
.map { it.getLongExtra("partId", 0L) }
|
||||
.filter { partId -> partId != 0L }
|
||||
|
||||
init {
|
||||
disposables += partIdFlowable
|
||||
.mapNotNull { partId -> messageRepo.getPart(partId) }
|
||||
.subscribe { part -> newState { copy(uri = part.getUri(), type = part.type) } }
|
||||
|
||||
disposables += partIdFlowable
|
||||
.mapNotNull { partId -> messageRepo.getMessageForPart(partId) }
|
||||
.mapNotNull { message -> conversationRepo.getConversation(message.threadId) }
|
||||
.subscribe { conversation -> newState { copy(title = conversation.getTitle()) } }
|
||||
disposables += Flowable.just(partId)
|
||||
.mapNotNull(messageRepo::getMessageForPart)
|
||||
.mapNotNull { message -> message.threadId }
|
||||
.doOnNext { threadId -> newState { copy(parts = messageRepo.getPartsForConversation(threadId)) } }
|
||||
.doOnNext { threadId -> newState { copy(title = conversationRepo.getConversation(threadId)?.getTitle()) } }
|
||||
.subscribe()
|
||||
}
|
||||
|
||||
override fun bindView(view: GalleryView) {
|
||||
super.bindView(view)
|
||||
|
||||
// When the screen is touched, toggle the visibility of the navigation UI
|
||||
view.screenTouchedIntent
|
||||
view.screenTouched()
|
||||
.withLatestFrom(state) { _, state -> state.navigationVisible }
|
||||
.map { navigationVisible -> !navigationVisible }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { navigationVisible -> newState { copy(navigationVisible = navigationVisible) } }
|
||||
|
||||
// Save image to device
|
||||
view.optionsItemSelectedIntent
|
||||
view.optionsItemSelected()
|
||||
.filter { itemId -> itemId == R.id.save }
|
||||
.withLatestFrom(partIdFlowable.toObservable()) { _, partId -> partId }
|
||||
.withLatestFrom(view.pageChanged()) { _, part -> part.id }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { partId -> saveImage.execute(partId) { context.makeToast(R.string.gallery_toast_saved) } }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user