Surface errors when saving/sharing covers

This commit is contained in:
Jobobby04 2021-02-26 21:06:56 -05:00
parent d0561705fe
commit 9457b832fc
2 changed files with 22 additions and 29 deletions

View File

@ -528,16 +528,18 @@ class MangaController :
R.id.action_migrate -> migrateManga()
// SY -->
R.id.action_save -> {
if (presenter.saveCover(activity!!)) {
try {
presenter.saveCover(activity!!)
activity?.toast(R.string.cover_saved)
} else {
activity?.toast(R.string.error_saving_cover)
} catch (e: Exception) {
e.message?.let { activity?.toast(it) } ?: activity?.toast(R.string.error_saving_cover)
}
}
R.id.action_share_cover -> {
val cover = presenter.shareCover(activity!!)
if (cover != null) {
val stream = cover.getUriCompat(activity!!)
try {
val activity = activity!!
val cover = presenter.shareCover(activity)
val stream = cover.getUriCompat(activity)
val intent = Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_STREAM, stream)
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
@ -545,8 +547,8 @@ class MangaController :
type = "image/*"
}
startActivity(Intent.createChooser(intent, activity?.getString(R.string.action_share)))
} else {
activity?.toast(R.string.error_sharing_cover)
} catch (e: Exception) {
e.message?.let { activity?.toast(it) } ?: activity?.toast(R.string.error_sharing_cover)
}
}
// SY <--

View File

@ -542,32 +542,23 @@ class MangaPresenter(
// I cant find any way to call the chapter list subscription to get the chapters again
}
fun shareCover(context: Context): File? {
return try {
val destDir = File(context.cacheDir, "shared_image")
val file = saveCover(destDir)
file
} catch (e: Exception) {
null
}
fun shareCover(context: Context): File {
val destDir = File(context.cacheDir, "shared_image")
return saveCover(destDir)
}
fun saveCover(context: Context): Boolean {
return try {
val directory = File(
Environment.getExternalStorageDirectory().absolutePath +
File.separator + Environment.DIRECTORY_PICTURES +
File.separator + context.getString(R.string.app_name)
)
saveCover(directory)
true
} catch (e: Exception) {
false
}
fun saveCover(context: Context) {
val directory = File(
Environment.getExternalStorageDirectory().absolutePath +
File.separator + Environment.DIRECTORY_PICTURES +
File.separator + context.getString(R.string.app_name)
)
saveCover(directory)
}
private fun saveCover(directory: File): File {
val cover = coverCache.getCoverFile(manga)!!
val cover = coverCache.getCoverFile(manga) ?: throw Exception("Cover url was null")
if (!cover.exists()) throw Exception("Cover not in cache")
val type = ImageUtil.findImageType(cover.inputStream())
?: throw Exception("Not an image")