forked from upstream-mirrors/quik
#1036 - Swipe between pictures
This commit is contained in:
@@ -18,8 +18,6 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.common
|
package com.moez.QKSMS.common
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.app.ActivityOptions
|
|
||||||
import android.content.ActivityNotFoundException
|
import android.content.ActivityNotFoundException
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
@@ -28,7 +26,6 @@ import android.os.Build
|
|||||||
import android.provider.ContactsContract
|
import android.provider.ContactsContract
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
import android.view.View
|
|
||||||
import com.moez.QKSMS.BuildConfig
|
import com.moez.QKSMS.BuildConfig
|
||||||
import com.moez.QKSMS.feature.backup.BackupActivity
|
import com.moez.QKSMS.feature.backup.BackupActivity
|
||||||
import com.moez.QKSMS.feature.blocked.BlockedActivity
|
import com.moez.QKSMS.feature.blocked.BlockedActivity
|
||||||
@@ -115,18 +112,6 @@ class Navigator @Inject constructor(
|
|||||||
startActivity(intent)
|
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() {
|
fun showBackup() {
|
||||||
analyticsManager.track("Viewed Backup")
|
analyticsManager.track("Viewed Backup")
|
||||||
startActivity(Intent(context, BackupActivity::class.java))
|
startActivity(Intent(context, BackupActivity::class.java))
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import android.widget.EditText
|
|||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
import android.widget.ProgressBar
|
import android.widget.ProgressBar
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import androidx.viewpager.widget.ViewPager
|
||||||
|
|
||||||
var ViewGroup.animateLayoutChanges: Boolean
|
var ViewGroup.animateLayoutChanges: Boolean
|
||||||
get() = layoutTransition != null
|
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() {
|
fun RecyclerView.scrapViews() {
|
||||||
val adapter = adapter
|
val adapter = adapter
|
||||||
val layoutManager = layoutManager
|
val layoutManager = layoutManager
|
||||||
|
|||||||
-4
@@ -67,10 +67,6 @@ class ConversationInfoController(val threadId: Long = 0) : QkController<Conversa
|
|||||||
override fun onViewCreated() {
|
override fun onViewCreated() {
|
||||||
items.postDelayed({ items?.animateLayoutChanges = true }, 100)
|
items.postDelayed({ items?.animateLayoutChanges = true }, 100)
|
||||||
|
|
||||||
mediaAdapter.thumbnailClicks
|
|
||||||
.autoDisposable(scope())
|
|
||||||
.subscribe { view -> navigator.showImageAnimated(activity!!, view) }
|
|
||||||
|
|
||||||
recipients.adapter = recipientAdapter
|
recipients.adapter = recipientAdapter
|
||||||
|
|
||||||
media.adapter = mediaAdapter
|
media.adapter = mediaAdapter
|
||||||
|
|||||||
+1
-7
@@ -20,18 +20,15 @@ package com.moez.QKSMS.feature.conversationinfo
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.moez.QKSMS.common.Navigator
|
import com.moez.QKSMS.common.Navigator
|
||||||
import com.moez.QKSMS.common.base.QkRealmAdapter
|
import com.moez.QKSMS.common.base.QkRealmAdapter
|
||||||
import com.moez.QKSMS.common.base.QkViewHolder
|
import com.moez.QKSMS.common.base.QkViewHolder
|
||||||
import com.moez.QKSMS.common.util.extensions.setVisible
|
import com.moez.QKSMS.common.util.extensions.setVisible
|
||||||
import com.moez.QKSMS.extensions.isImage
|
|
||||||
import com.moez.QKSMS.extensions.isVideo
|
import com.moez.QKSMS.extensions.isVideo
|
||||||
import com.moez.QKSMS.model.MmsPart
|
import com.moez.QKSMS.model.MmsPart
|
||||||
import com.moez.QKSMS.util.GlideApp
|
import com.moez.QKSMS.util.GlideApp
|
||||||
import io.reactivex.subjects.PublishSubject
|
|
||||||
import kotlinx.android.synthetic.main.conversation_media_list_item.view.*
|
import kotlinx.android.synthetic.main.conversation_media_list_item.view.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -40,15 +37,13 @@ class ConversationMediaAdapter @Inject constructor(
|
|||||||
private val navigator: Navigator
|
private val navigator: Navigator
|
||||||
) : QkRealmAdapter<MmsPart>() {
|
) : QkRealmAdapter<MmsPart>() {
|
||||||
|
|
||||||
val thumbnailClicks: PublishSubject<View> = PublishSubject.create()
|
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
|
||||||
val inflater = LayoutInflater.from(parent.context)
|
val inflater = LayoutInflater.from(parent.context)
|
||||||
val view = inflater.inflate(R.layout.conversation_media_list_item, parent, false)
|
val view = inflater.inflate(R.layout.conversation_media_list_item, parent, false)
|
||||||
return QkViewHolder(view).apply {
|
return QkViewHolder(view).apply {
|
||||||
view.thumbnail.setOnClickListener {
|
view.thumbnail.setOnClickListener {
|
||||||
val part = getItem(adapterPosition)!!
|
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)
|
.into(view.thumbnail)
|
||||||
|
|
||||||
view.video.setVisible(part.isVideo())
|
view.video.setVisible(part.isVideo())
|
||||||
view.thumbnail.transitionName = part.id.toString()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -18,145 +18,71 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.feature.gallery
|
package com.moez.QKSMS.feature.gallery
|
||||||
|
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.transition.ChangeBounds
|
|
||||||
import android.transition.ChangeImageTransform
|
|
||||||
import android.transition.TransitionSet
|
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
|
import androidx.core.view.isVisible
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.lifecycle.ViewModelProviders
|
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.R
|
||||||
import com.moez.QKSMS.common.GlideCompletionListener
|
|
||||||
import com.moez.QKSMS.common.base.QkActivity
|
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.common.util.extensions.setVisible
|
||||||
import com.moez.QKSMS.util.GlideApp
|
import com.moez.QKSMS.model.MmsPart
|
||||||
import dagger.android.AndroidInjection
|
import dagger.android.AndroidInjection
|
||||||
|
import io.reactivex.Observable
|
||||||
import io.reactivex.subjects.PublishSubject
|
import io.reactivex.subjects.PublishSubject
|
||||||
import io.reactivex.subjects.Subject
|
import io.reactivex.subjects.Subject
|
||||||
import kotlinx.android.synthetic.main.gallery_activity.*
|
import kotlinx.android.synthetic.main.gallery_activity.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
||||||
class GalleryActivity : QkActivity(), GalleryView {
|
class GalleryActivity : QkActivity(), GalleryView {
|
||||||
|
|
||||||
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
||||||
|
@Inject lateinit var pagerAdapter: GalleryPagerAdapter
|
||||||
|
|
||||||
override val screenTouchedIntent by lazy { image.clicks() }
|
private val optionsItemSubject: Subject<Int> = PublishSubject.create()
|
||||||
override val optionsItemSelectedIntent: Subject<Int> = PublishSubject.create()
|
private val pageChangedSubject: Subject<MmsPart> = PublishSubject.create()
|
||||||
|
|
||||||
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory)[GalleryViewModel::class.java] }
|
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory)[GalleryViewModel::class.java] }
|
||||||
|
|
||||||
private var exoPlayer: SimpleExoPlayer? = null
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
AndroidInjection.inject(this)
|
AndroidInjection.inject(this)
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.gallery_activity)
|
setContentView(R.layout.gallery_activity)
|
||||||
postponeEnterTransition()
|
|
||||||
showBackButton(true)
|
showBackButton(true)
|
||||||
viewModel.bindView(this)
|
viewModel.bindView(this)
|
||||||
|
|
||||||
val transition = TransitionSet().apply {
|
pagerAdapter.setInitialPositionHandler = { position ->
|
||||||
duration = 100
|
pager.setCurrentItem(position, false)
|
||||||
|
|
||||||
addTransition(ChangeBounds())
|
// The ViewPager's page change listener isn't called if the initial position is 0
|
||||||
addTransition(ChangeImageTransform())
|
if (position == 0) {
|
||||||
}
|
pageChanged(0)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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) {
|
override fun render(state: GalleryState) {
|
||||||
toolbar.setVisible(state.navigationVisible)
|
toolbar.setVisible(state.navigationVisible)
|
||||||
|
|
||||||
title = state.title
|
title = state.title
|
||||||
|
pagerAdapter.parts = state.parts
|
||||||
when {
|
|
||||||
ContentType.isImageType(state.type) -> showImage(state.uri)
|
|
||||||
ContentType.isVideoType(state.type) -> showVideo(state.uri)
|
|
||||||
else -> hideMedia()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showImage(uri: Uri?) {
|
override fun optionsItemSelected(): Observable<Int> = optionsItemSubject
|
||||||
image.setVisible(true)
|
|
||||||
video.setVisible(false)
|
|
||||||
if (image.drawable == null) {
|
|
||||||
|
|
||||||
// We need to explicitly request a gif from glide for animations to work
|
override fun screenTouched(): Observable<*> = pagerAdapter.clicks
|
||||||
when (uri?.let(contentResolver::getType)) {
|
|
||||||
ContentType.IMAGE_GIF -> GlideApp.with(this)
|
|
||||||
.asGif()
|
|
||||||
.load(uri)
|
|
||||||
.listener(GlideCompletionListener {
|
|
||||||
startPostponedEnterTransition()
|
|
||||||
})
|
|
||||||
.into(image)
|
|
||||||
|
|
||||||
else -> GlideApp.with(this)
|
override fun pageChanged(): Observable<MmsPart> = pageChangedSubject
|
||||||
.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 onCreateOptionsMenu(menu: Menu?): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||||
menuInflater.inflate(R.menu.gallery, menu)
|
menuInflater.inflate(R.menu.gallery, menu)
|
||||||
@@ -166,14 +92,14 @@ class GalleryActivity : QkActivity(), GalleryView {
|
|||||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
when (item.itemId) {
|
when (item.itemId) {
|
||||||
android.R.id.home -> onBackPressed()
|
android.R.id.home -> onBackPressed()
|
||||||
else -> optionsItemSelectedIntent.onNext(item.itemId)
|
else -> optionsItemSubject.onNext(item.itemId)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
exoPlayer?.release()
|
pagerAdapter.destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -24,6 +24,7 @@ import com.moez.QKSMS.injection.ViewModelKey
|
|||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import dagger.multibindings.IntoMap
|
import dagger.multibindings.IntoMap
|
||||||
|
import javax.inject.Named
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
class GalleryActivityModule {
|
class GalleryActivityModule {
|
||||||
@@ -31,6 +32,10 @@ class GalleryActivityModule {
|
|||||||
@Provides
|
@Provides
|
||||||
fun provideIntent(activity: GalleryActivity): Intent = activity.intent
|
fun provideIntent(activity: GalleryActivity): Intent = activity.intent
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Named("partId")
|
||||||
|
fun providePartId(activity: GalleryActivity): Long = activity.intent.getLongExtra("partId", 0L)
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@IntoMap
|
@IntoMap
|
||||||
@ViewModelKey(GalleryViewModel::class)
|
@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
|
package com.moez.QKSMS.feature.gallery
|
||||||
|
|
||||||
import android.net.Uri
|
import com.moez.QKSMS.model.MmsPart
|
||||||
|
import io.realm.RealmResults
|
||||||
|
|
||||||
data class GalleryState(
|
data class GalleryState(
|
||||||
val navigationVisible: Boolean = true,
|
val navigationVisible: Boolean = true,
|
||||||
val title: String = "",
|
val title: String? = "",
|
||||||
val uri: Uri? = null,
|
val parts: RealmResults<MmsPart>? = null)
|
||||||
val type: String = ""
|
|
||||||
)
|
|
||||||
@@ -19,11 +19,13 @@
|
|||||||
package com.moez.QKSMS.feature.gallery
|
package com.moez.QKSMS.feature.gallery
|
||||||
|
|
||||||
import com.moez.QKSMS.common.base.QkView
|
import com.moez.QKSMS.common.base.QkView
|
||||||
|
import com.moez.QKSMS.model.MmsPart
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
|
|
||||||
interface GalleryView : QkView<GalleryState> {
|
interface GalleryView : QkView<GalleryState> {
|
||||||
|
|
||||||
val screenTouchedIntent: Observable<Unit>
|
fun optionsItemSelected(): Observable<Int>
|
||||||
val optionsItemSelectedIntent: Observable<Int>
|
fun screenTouched(): Observable<*>
|
||||||
|
fun pageChanged(): Observable<MmsPart>
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,6 @@
|
|||||||
package com.moez.QKSMS.feature.gallery
|
package com.moez.QKSMS.feature.gallery
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.moez.QKSMS.common.androidxcompat.scope
|
import com.moez.QKSMS.common.androidxcompat.scope
|
||||||
import com.moez.QKSMS.common.base.QkViewModel
|
import com.moez.QKSMS.common.base.QkViewModel
|
||||||
@@ -33,44 +32,39 @@ import io.reactivex.Flowable
|
|||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
import io.reactivex.rxkotlin.withLatestFrom
|
import io.reactivex.rxkotlin.withLatestFrom
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Named
|
||||||
|
|
||||||
class GalleryViewModel @Inject constructor(
|
class GalleryViewModel @Inject constructor(
|
||||||
private val intent: Intent,
|
conversationRepo: ConversationRepository,
|
||||||
|
messageRepo: MessageRepository,
|
||||||
|
@Named("partId") private val partId: Long,
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val conversationRepo: ConversationRepository,
|
|
||||||
private val messageRepo: MessageRepository,
|
|
||||||
private val saveImage: SaveImage
|
private val saveImage: SaveImage
|
||||||
) : QkViewModel<GalleryView, GalleryState>(GalleryState()) {
|
) : QkViewModel<GalleryView, GalleryState>(GalleryState()) {
|
||||||
|
|
||||||
private val partIdFlowable = Flowable.just(intent)
|
|
||||||
.map { it.getLongExtra("partId", 0L) }
|
|
||||||
.filter { partId -> partId != 0L }
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
disposables += partIdFlowable
|
disposables += Flowable.just(partId)
|
||||||
.mapNotNull { partId -> messageRepo.getPart(partId) }
|
.mapNotNull(messageRepo::getMessageForPart)
|
||||||
.subscribe { part -> newState { copy(uri = part.getUri(), type = part.type) } }
|
.mapNotNull { message -> message.threadId }
|
||||||
|
.doOnNext { threadId -> newState { copy(parts = messageRepo.getPartsForConversation(threadId)) } }
|
||||||
disposables += partIdFlowable
|
.doOnNext { threadId -> newState { copy(title = conversationRepo.getConversation(threadId)?.getTitle()) } }
|
||||||
.mapNotNull { partId -> messageRepo.getMessageForPart(partId) }
|
.subscribe()
|
||||||
.mapNotNull { message -> conversationRepo.getConversation(message.threadId) }
|
|
||||||
.subscribe { conversation -> newState { copy(title = conversation.getTitle()) } }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun bindView(view: GalleryView) {
|
override fun bindView(view: GalleryView) {
|
||||||
super.bindView(view)
|
super.bindView(view)
|
||||||
|
|
||||||
// When the screen is touched, toggle the visibility of the navigation UI
|
// When the screen is touched, toggle the visibility of the navigation UI
|
||||||
view.screenTouchedIntent
|
view.screenTouched()
|
||||||
.withLatestFrom(state) { _, state -> state.navigationVisible }
|
.withLatestFrom(state) { _, state -> state.navigationVisible }
|
||||||
.map { navigationVisible -> !navigationVisible }
|
.map { navigationVisible -> !navigationVisible }
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { navigationVisible -> newState { copy(navigationVisible = navigationVisible) } }
|
.subscribe { navigationVisible -> newState { copy(navigationVisible = navigationVisible) } }
|
||||||
|
|
||||||
// Save image to device
|
// Save image to device
|
||||||
view.optionsItemSelectedIntent
|
view.optionsItemSelected()
|
||||||
.filter { itemId -> itemId == R.id.save }
|
.filter { itemId -> itemId == R.id.save }
|
||||||
.withLatestFrom(partIdFlowable.toObservable()) { _, partId -> partId }
|
.withLatestFrom(view.pageChanged()) { _, part -> part.id }
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { partId -> saveImage.execute(partId) { context.makeToast(R.string.gallery_toast_saved) } }
|
.subscribe { partId -> saveImage.execute(partId) { context.makeToast(R.string.gallery_toast_saved) } }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,13 +25,8 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context="com.moez.QKSMS.feature.gallery.GalleryActivity">
|
tools:context="com.moez.QKSMS.feature.gallery.GalleryActivity">
|
||||||
|
|
||||||
<com.github.chrisbanes.photoview.PhotoView
|
<androidx.viewpager.widget.ViewPager
|
||||||
android:id="@+id/image"
|
android:id="@+id/pager"
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent" />
|
|
||||||
|
|
||||||
<com.google.android.exoplayer2.ui.PlayerView
|
|
||||||
android:id="@+id/video"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent" />
|
android:layout_height="match_parent" />
|
||||||
|
|
||||||
@@ -43,17 +38,34 @@
|
|||||||
android:gravity="center_vertical"
|
android:gravity="center_vertical"
|
||||||
android:minHeight="56dp">
|
android:minHeight="56dp">
|
||||||
|
|
||||||
<com.moez.QKSMS.common.widget.QkTextView
|
<LinearLayout
|
||||||
android:id="@+id/toolbarTitle"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:background="@null"
|
android:gravity="center_vertical"
|
||||||
android:gravity="start|center_vertical"
|
android:orientation="vertical">
|
||||||
android:maxLines="1"
|
|
||||||
android:singleLine="true"
|
<com.moez.QKSMS.common.widget.QkTextView
|
||||||
android:textColor="@color/textPrimaryDark"
|
android:id="@+id/toolbarTitle"
|
||||||
app:textSize="toolbar"
|
android:layout_width="match_parent"
|
||||||
tools:text="Conversations" />
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@null"
|
||||||
|
android:gravity="start"
|
||||||
|
android:maxLines="1"
|
||||||
|
android:singleLine="true"
|
||||||
|
android:textColor="@color/textPrimaryDark"
|
||||||
|
app:textSize="toolbar"
|
||||||
|
tools:text="Conversations" />
|
||||||
|
|
||||||
|
<com.moez.QKSMS.common.widget.QkTextView
|
||||||
|
android:id="@+id/toolbarSubtitle"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="2dp"
|
||||||
|
android:textColor="@color/textTertiaryDark"
|
||||||
|
android:visibility="gone"
|
||||||
|
app:textSize="secondary" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
</androidx.appcompat.widget.Toolbar>
|
</androidx.appcompat.widget.Toolbar>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
~ 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/>.
|
||||||
|
-->
|
||||||
|
<com.github.chrisbanes.photoview.PhotoView
|
||||||
|
android:id="@+id/image"
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent" />
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
~ 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/>.
|
||||||
|
-->
|
||||||
|
<com.moez.QKSMS.common.widget.QkTextView
|
||||||
|
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:gravity="center"
|
||||||
|
android:text="@string/gallery_error"
|
||||||
|
android:textColor="?android:attr/textColorSecondary"
|
||||||
|
app:textSize="primary" />
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
~ 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/>.
|
||||||
|
-->
|
||||||
|
<com.google.android.exoplayer2.ui.PlayerView
|
||||||
|
android:id="@+id/video"
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent" />
|
||||||
@@ -145,6 +145,7 @@
|
|||||||
<string name="info_unblock">Unblock</string>
|
<string name="info_unblock">Unblock</string>
|
||||||
<string name="info_delete">Delete conversation</string>
|
<string name="info_delete">Delete conversation</string>
|
||||||
|
|
||||||
|
<string name="gallery_error">Couldn\'t load media</string>
|
||||||
<string name="gallery_toast_saved">Saved to gallery</string>
|
<string name="gallery_toast_saved">Saved to gallery</string>
|
||||||
|
|
||||||
<string name="backup_title">Backup and restore</string>
|
<string name="backup_title">Backup and restore</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user