Commit 6bee1eba authored by Alexandre Roux's avatar Alexandre Roux

Merge branch 'ask-storage-permission' into 'sprint_2'

ask storage permission when saving a picture from mms

See merge request !10
parents 14633fd7 dc023850
Pipeline #27220 passed with stage
in 9 minutes and 11 seconds
......@@ -18,9 +18,12 @@
*/
package com.moez.QKSMS.feature.gallery
import android.Manifest
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.annotation.NonNull
import androidx.core.app.ActivityCompat
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
......@@ -50,6 +53,7 @@ class GalleryActivity : QkActivity(), GalleryView {
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 val permissionResultSubject: Subject<Unit> = PublishSubject.create()
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
......@@ -112,9 +116,28 @@ class GalleryActivity : QkActivity(), GalleryView {
return true
}
override fun onStart() {
super.onStart()
}
override fun onPause() {
super.onPause()
}
override fun onDestroy() {
super.onDestroy()
pagerAdapter.destroy()
}
override fun requestStoragePermission() {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 0)
}
override fun onRequestPermissionsResult(requestCode: Int, @NonNull permissions: Array<String>, @NonNull grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
permissionResultSubject.onNext(Unit)
}
override fun permissionResult(): Observable<*> = permissionResultSubject
}
\ No newline at end of file
......@@ -27,5 +27,6 @@ interface GalleryView : QkView<GalleryState> {
fun optionsItemSelected(): Observable<Int>
fun screenTouched(): Observable<*>
fun pageChanged(): Observable<MmsPart>
fun requestStoragePermission()
fun permissionResult(): Observable<*>
}
\ No newline at end of file
......@@ -25,6 +25,7 @@ import com.moez.QKSMS.common.base.QkViewModel
import com.moez.QKSMS.common.util.extensions.makeToast
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.interactor.SaveImage
import com.moez.QKSMS.manager.PermissionManager
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import com.uber.autodispose.kotlin.autoDisposable
......@@ -39,7 +40,8 @@ class GalleryViewModel @Inject constructor(
messageRepo: MessageRepository,
@Named("partId") private val partId: Long,
private val context: Context,
private val saveImage: SaveImage
private val saveImage: SaveImage,
private val permissionManager: PermissionManager
) : QkViewModel<GalleryView, GalleryState>(GalleryState()) {
init {
......@@ -53,7 +55,13 @@ class GalleryViewModel @Inject constructor(
override fun bindView(view: GalleryView) {
super.bindView(view)
view.permissionResult()
.map { permissionManager.hasStorage() }
.autoDisposable(view.scope())
.subscribe{
if(permissionManager.hasStorage())
saveImage.execute(partId) { context.makeToast(R.string.gallery_toast_saved) }
}
// When the screen is touched, toggle the visibility of the navigation UI
view.screenTouched()
.withLatestFrom(state) { _, state -> state.navigationVisible }
......@@ -66,7 +74,13 @@ class GalleryViewModel @Inject constructor(
.filter { itemId -> itemId == R.id.save }
.withLatestFrom(view.pageChanged()) { _, part -> part.id }
.autoDisposable(view.scope())
.subscribe { partId -> saveImage.execute(partId) { context.makeToast(R.string.gallery_toast_saved) } }
.subscribe {
partId ->
if(permissionManager.hasStorage())
saveImage.execute(partId) { context.makeToast(R.string.gallery_toast_saved) }
else
view.requestStoragePermission()
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment