(currently hidden) option to save image to gallery

This commit is contained in:
Moez Bhatti
2018-03-12 18:41:38 -07:00
parent c91bad4386
commit 3332045193
6 changed files with 105 additions and 4 deletions
@@ -0,0 +1,42 @@
/*
* 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 interactor
import android.content.Context
import android.provider.MediaStore
import androidx.net.toUri
import data.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class SaveImage @Inject constructor(
private val context: Context,
private val messageRepo: MessageRepository
) : Interactor<Long>() {
override fun buildObservable(params: Long): Flowable<*> {
return Flowable.just(params)
.map { partId -> messageRepo.getPart(partId) }
.map { part -> part.image }
.map { uriString -> uriString.toUri() }
.map { uri -> MediaStore.Images.Media.getBitmap(context.contentResolver, uri) }
.doOnNext { bitmap -> MediaStore.Images.Media.insertImage(context.contentResolver, bitmap, "title", "description") }
}
}
@@ -22,10 +22,14 @@ import android.os.Bundle
import android.transition.ChangeBounds import android.transition.ChangeBounds
import android.transition.ChangeImageTransform import android.transition.ChangeImageTransform
import android.transition.TransitionSet import android.transition.TransitionSet
import android.view.Menu
import android.view.MenuItem
import com.jakewharton.rxbinding2.view.clicks import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R import com.moez.QKSMS.R
import common.util.GlideApp import common.util.GlideApp
import common.util.extensions.setVisible import common.util.extensions.setVisible
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.gallery_activity.* import kotlinx.android.synthetic.main.gallery_activity.*
import presentation.common.GlideCompletionListener import presentation.common.GlideCompletionListener
import presentation.common.base.QkActivity import presentation.common.base.QkActivity
@@ -34,6 +38,7 @@ class GalleryActivity : QkActivity<GalleryViewModel>(), GalleryView {
override val viewModelClass = GalleryViewModel::class override val viewModelClass = GalleryViewModel::class
override val screenTouchedIntent by lazy { image.clicks() } override val screenTouchedIntent by lazy { image.clicks() }
override val optionsItemSelectedIntent: Subject<Int> = PublishSubject.create()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@@ -89,4 +94,14 @@ class GalleryActivity : QkActivity<GalleryViewModel>(), GalleryView {
} }
} }
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.gallery, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
optionsItemSelectedIntent.onNext(item.itemId)
return true
}
} }
@@ -24,5 +24,6 @@ import presentation.common.base.QkView
interface GalleryView : QkView<GalleryState> { interface GalleryView : QkView<GalleryState> {
val screenTouchedIntent: Observable<Unit> val screenTouchedIntent: Observable<Unit>
val optionsItemSelectedIntent: Observable<Int>
} }
@@ -18,12 +18,16 @@
*/ */
package presentation.feature.gallery package presentation.feature.gallery
import android.content.Context
import android.content.Intent import android.content.Intent
import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable import com.uber.autodispose.kotlin.autoDisposable
import common.di.appComponent import common.di.appComponent
import common.util.extensions.makeToast
import common.util.extensions.mapNotNull import common.util.extensions.mapNotNull
import data.repository.MessageRepository import data.repository.MessageRepository
import interactor.SaveImage
import io.reactivex.Flowable import io.reactivex.Flowable
import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom import io.reactivex.rxkotlin.withLatestFrom
@@ -32,15 +36,17 @@ import javax.inject.Inject
class GalleryViewModel(intent: Intent) : QkViewModel<GalleryView, GalleryState>(GalleryState()) { class GalleryViewModel(intent: Intent) : QkViewModel<GalleryView, GalleryState>(GalleryState()) {
@Inject lateinit var context: Context
@Inject lateinit var messageRepo: MessageRepository @Inject lateinit var messageRepo: MessageRepository
@Inject lateinit var saveImage: SaveImage
private val partIdFlowable = Flowable.just(intent)
.map { it.getLongExtra("partId", 0L) }
.filter { partId -> partId != 0L }
init { init {
appComponent.inject(this) appComponent.inject(this)
val partIdFlowable = Flowable.just(intent)
.map { it.getLongExtra("partId", 0L) }
.filter { partId -> partId != 0L }
disposables += partIdFlowable disposables += partIdFlowable
.mapNotNull { partId -> messageRepo.getPart(partId) } .mapNotNull { partId -> messageRepo.getPart(partId) }
.mapNotNull { part -> part.image } .mapNotNull { part -> part.image }
@@ -61,6 +67,13 @@ class GalleryViewModel(intent: Intent) : QkViewModel<GalleryView, GalleryState>(
.map { navigationVisible -> !navigationVisible } .map { navigationVisible -> !navigationVisible }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { navigationVisible -> newState { it.copy(navigationVisible = navigationVisible) } } .subscribe { navigationVisible -> newState { it.copy(navigationVisible = navigationVisible) } }
// Save image to device
view.optionsItemSelectedIntent
.filter { itemId -> itemId == R.id.save }
.withLatestFrom(partIdFlowable.toObservable(), { _, partId -> partId })
.autoDisposable(view.scope())
.subscribe { partId -> saveImage.execute(partId) { context.makeToast(R.string.gallery_toast_saved) } }
} }
} }
+27
View File
@@ -0,0 +1,27 @@
<?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/>.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/save"
android:title="@string/menu_save"
android:visible="false" />
</menu>
+3
View File
@@ -40,6 +40,7 @@
<string name="menu_archive">Archive conversation</string> <string name="menu_archive">Archive conversation</string>
<string name="menu_unarchive">Remove from archive</string> <string name="menu_unarchive">Remove from archive</string>
<string name="menu_delete">Delete</string> <string name="menu_delete">Delete</string>
<string name="menu_save">Save image</string>
<string name="main_syncing">Optimizing database, this might take a minute…</string> <string name="main_syncing">Optimizing database, this might take a minute…</string>
@@ -74,6 +75,8 @@
<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_toast_saved">Saved image</string>
<string name="settings_default_sms_title">Default SMS app</string> <string name="settings_default_sms_title">Default SMS app</string>
<string name="settings_default_sms_summary_true">QKSMS is your default SMS app</string> <string name="settings_default_sms_summary_true">QKSMS is your default SMS app</string>
<string name="settings_default_sms_summary_false">Set QKSMS as your default SMS app</string> <string name="settings_default_sms_summary_false">Set QKSMS as your default SMS app</string>