Use MediaStore for saving attachments to device

This commit is contained in:
Moez Bhatti
2023-01-02 16:43:18 -05:00
parent 53b2ac2bf8
commit b5cac5d87d
6 changed files with 54 additions and 31 deletions
@@ -260,23 +260,21 @@ class Navigator @Inject constructor(
startActivityExternal(intent)
}
fun viewFile(file: File) {
val data = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file)
val type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(file.name.split(".").last())
fun viewFile(uri: Uri, mimeType: String) {
val intent = Intent(Intent.ACTION_VIEW)
.setDataAndType(data, type)
.setDataAndType(uri, mimeType.lowercase())
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.let { Intent.createChooser(it, null) }
startActivityExternal(intent)
}
fun shareFile(file: File) {
val data = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file)
val type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(file.name.split(".").last())
fun shareFile(uri: Uri, mimeType: String) {
val intent = Intent(Intent.ACTION_SEND)
.setType(type)
.putExtra(Intent.EXTRA_STREAM, data)
.setType(mimeType.lowercase())
.putExtra(Intent.EXTRA_STREAM, uri)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.let { Intent.createChooser(it, null) }
startActivityExternal(intent)
}
@@ -31,7 +31,6 @@ import androidx.core.view.isVisible
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkController
import com.moez.QKSMS.common.util.DateFormatter
import com.moez.QKSMS.common.util.QkActivityResultContracts
import com.moez.QKSMS.common.util.extensions.getLabel
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
@@ -433,7 +433,7 @@ class ComposeViewModel @Inject constructor(
.autoDisposable(view.scope())
.subscribe { part ->
if (permissionManager.hasStorage()) {
messageRepo.savePart(part.id)?.let(navigator::viewFile)
messageRepo.savePart(part.id)?.let { navigator.viewFile(it, part.type) }
} else {
view.requestStoragePermission()
}
@@ -81,9 +81,11 @@ class GalleryViewModel @Inject constructor(
view.optionsItemSelected()
.filter { itemId -> itemId == R.id.share }
.filter { permissions.hasStorage().also { if (!it) view.requestStoragePermission() } }
.withLatestFrom(view.pageChanged()) { _, part -> part.id }
.withLatestFrom(view.pageChanged()) { _, part -> part }
.autoDisposable(view.scope())
.subscribe { partId -> messageRepo.savePart(partId)?.let(navigator::shareFile) }
.subscribe { part ->
messageRepo.savePart(part.id)?.let { navigator.shareFile(it, part.type) }
}
}
}