Skip to content

Commit

Permalink
Handle editing the book from the category controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Jan 5, 2019
1 parent 55a4059 commit 1c41152
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 47 deletions.
40 changes: 40 additions & 0 deletions app/src/main/java/de/ph1b/audiobook/features/GalleryPicker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.ph1b.audiobook.features

import android.app.Activity
import android.content.Intent
import com.bluelinelabs.conductor.Controller
import de.ph1b.audiobook.features.bookOverview.EditCoverDialogController
import java.util.UUID
import javax.inject.Inject

private const val REQUEST_CODE = 7

class GalleryPicker
@Inject constructor() {

private var pickForBookId: UUID? = null

fun pick(bookId: UUID, controller: Controller) {
pickForBookId = bookId
val intent = Intent(Intent.ACTION_PICK)
.setType("image/*")
controller.startActivityForResult(intent, REQUEST_CODE)
}

fun parse(requestCode: Int, resultCode: Int, data: Intent?): EditCoverDialogController.Arguments? {
if (requestCode != REQUEST_CODE) {
return null
}
return if (resultCode == Activity.RESULT_OK) {
val imageUri = data?.data
val bookId = pickForBookId
if (imageUri == null || bookId == null) {
null
} else {
EditCoverDialogController.Arguments(imageUri, bookId)
}
} else {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import de.ph1b.audiobook.features.bookOverview.list.BookOverviewModel
import de.ph1b.audiobook.features.bookOverview.list.GridBookOverviewComponent
import de.ph1b.audiobook.features.bookOverview.list.ListBookOverviewComponent
import de.ph1b.audiobook.misc.recyclerComponent.CompositeListAdapter
import java.util.UUID

class BookCategoryAdapter(listener: BookClickListener) :
CompositeListAdapter<BookOverviewModel>(Diff()) {
Expand All @@ -14,6 +15,16 @@ class BookCategoryAdapter(listener: BookClickListener) :
addComponent(GridBookOverviewComponent(listener))
addComponent(ListBookOverviewComponent(listener))
}

fun notifyCoverChanged(bookId: UUID) {
for (i in 0 until itemCount) {
val item = getItem(i)
if (item.book.id == bookId) {
notifyItemChanged(i)
return
}
}
}
}

private class Diff : DiffUtil.ItemCallback<BookOverviewModel>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
package de.ph1b.audiobook.features.bookCategory

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import de.ph1b.audiobook.R
import de.ph1b.audiobook.data.Book
import de.ph1b.audiobook.features.BaseController
import de.ph1b.audiobook.features.GalleryPicker
import de.ph1b.audiobook.features.bookOverview.EditBookBottomSheetController
import de.ph1b.audiobook.features.bookOverview.EditCoverDialogController
import de.ph1b.audiobook.features.bookOverview.list.BookOverviewClick
import de.ph1b.audiobook.features.bookOverview.list.header.BookOverviewCategory
import de.ph1b.audiobook.features.bookPlaying.BookPlayController
import de.ph1b.audiobook.features.imagepicker.CoverFromInternetController
import de.ph1b.audiobook.injection.appComponent
import de.ph1b.audiobook.misc.conductor.asTransaction
import de.ph1b.audiobook.misc.conductor.clearAfterDestroyView
import de.ph1b.audiobook.misc.conductor.popOrBack
import de.ph1b.audiobook.misc.tint
import de.ph1b.audiobook.uitools.BookChangeHandler
import kotlinx.android.synthetic.main.book_category.*
import java.util.UUID
import javax.inject.Inject

private const val NI_CATEGORY = "ni#category"

class BookCategoryController(bundle: Bundle) : BaseController(bundle) {
class BookCategoryController(bundle: Bundle) : BaseController(bundle), EditBookBottomSheetController.Callback,
CoverFromInternetController.Callback, EditCoverDialogController.Callback {

@Inject
lateinit var viewModel: BookCategoryViewModel
@Inject
lateinit var galleryPicker: GalleryPicker

init {
appComponent.inject(this)
Expand All @@ -32,6 +43,7 @@ class BookCategoryController(bundle: Bundle) : BaseController(bundle) {
})

private val category = bundle.getSerializable(NI_CATEGORY) as BookOverviewCategory
private var adapter by clearAfterDestroyView<BookCategoryAdapter>()

override val layoutRes = R.layout.book_category

Expand All @@ -49,9 +61,10 @@ class BookCategoryController(bundle: Bundle) : BaseController(bundle) {
router.replaceTopController(BookPlayController(book.id).asTransaction(changeHandler, changeHandler))
}
BookOverviewClick.MENU -> {
EditBookBottomSheetController(this, book).showDialog(router)
}
}
}
}.also { adapter = it }
recyclerView.adapter = adapter
val layoutManager = GridLayoutManager(activity, 1)
recyclerView.layoutManager = layoutManager
Expand All @@ -65,4 +78,23 @@ class BookCategoryController(bundle: Bundle) : BaseController(bundle) {
}
.disposeOnDestroyView()
}

override fun onInternetCoverRequested(book: Book) {
router.pushController(CoverFromInternetController(book.id, this).asTransaction())
}

override fun onBookCoverChanged(bookId: UUID) {
adapter.notifyCoverChanged(bookId)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val arguments = galleryPicker.parse(requestCode, resultCode, data)
if (arguments != null) {
EditCoverDialogController(this, arguments).showDialog(router)
}
}

override fun onFileCoverRequested(book: Book) {
galleryPicker.pick(book.id, this)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.ph1b.audiobook.features.bookOverview

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.view.MenuItem
Expand All @@ -14,6 +13,7 @@ import com.getkeepsafe.taptargetview.TapTargetView
import de.ph1b.audiobook.R
import de.ph1b.audiobook.data.Book
import de.ph1b.audiobook.features.BaseController
import de.ph1b.audiobook.features.GalleryPicker
import de.ph1b.audiobook.features.bookCategory.BookCategoryController
import de.ph1b.audiobook.features.bookOverview.list.BookOverviewAdapter
import de.ph1b.audiobook.features.bookOverview.list.BookOverviewClick
Expand Down Expand Up @@ -44,8 +44,6 @@ import java.util.UUID
import javax.inject.Inject
import javax.inject.Named

private const val COVER_FROM_GALLERY = 1

/**
* Showing the shelf of all the available books and provide a navigation to each book.
*/
Expand All @@ -61,14 +59,14 @@ class BookOverviewController : BaseController(),

@field:[Inject Named(PrefKeys.CURRENT_BOOK)]
lateinit var currentBookIdPref: Pref<UUID>

@Inject
lateinit var viewModel: BookOverviewViewModel
@Inject
lateinit var galleryPicker: GalleryPicker

private var playPauseDrawableSetter: PlayPauseDrawableSetter by clearAfterDestroyView()
private var adapter: BookOverviewAdapter by clearAfterDestroyView()
private var currentTapTarget by clearAfterDestroyViewNullable<TapTargetView>()
private var menuBookId: UUID? = null
private var useGrid = false

override fun onViewCreated() {
Expand Down Expand Up @@ -157,19 +155,9 @@ class BookOverviewController : BaseController(),
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
COVER_FROM_GALLERY -> {
if (resultCode == Activity.RESULT_OK) {
val imageUri = data?.data
val bookId = menuBookId
if (imageUri == null || bookId == null) {
return
}

EditCoverDialogController(this, bookId, imageUri).showDialog(router)
}
}
else -> super.onActivityResult(requestCode, resultCode, data)
val arguments = galleryPicker.parse(requestCode, resultCode, data)
if (arguments != null) {
EditCoverDialogController(this, arguments).showDialog(router)
}
}

Expand Down Expand Up @@ -269,10 +257,7 @@ class BookOverviewController : BaseController(),
}

override fun onFileCoverRequested(book: Book) {
menuBookId = book.id
val galleryPickerIntent = Intent(Intent.ACTION_PICK)
galleryPickerIntent.type = "image/*"
startActivityForResult(galleryPickerIntent, COVER_FROM_GALLERY)
galleryPicker.pick(book.id, this)
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.os.Parcelable
import androidx.core.view.isVisible
import com.afollestad.materialdialogs.DialogAction
import com.afollestad.materialdialogs.MaterialDialog
Expand All @@ -17,11 +18,10 @@ import de.ph1b.audiobook.injection.appComponent
import de.ph1b.audiobook.misc.DialogController
import de.ph1b.audiobook.misc.DialogLayoutContainer
import de.ph1b.audiobook.misc.coverFile
import de.ph1b.audiobook.misc.getUUID
import de.ph1b.audiobook.misc.putUUID
import de.ph1b.audiobook.uitools.CropTransformation
import de.ph1b.audiobook.uitools.ImageHelper
import de.ph1b.audiobook.uitools.SimpleTarget
import kotlinx.android.parcel.Parcelize
import kotlinx.android.synthetic.main.dialog_cover_edit.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
Expand All @@ -33,7 +33,7 @@ import com.squareup.picasso.Callback as PicassoCallback
/**
* Simple dialog to edit the cover of a book.
*/
class EditCoverDialogController(args: Bundle) : DialogController(args) {
class EditCoverDialogController(bundle: Bundle) : DialogController(bundle) {

@Inject
lateinit var repo: BookRepository
Expand All @@ -55,13 +55,12 @@ class EditCoverDialogController(args: Bundle) : DialogController(args) {
)

// init values
val bookId = args.getUUID(NI_BOOK_ID)
val uri = Uri.parse(args.getString(NI_COVER_URI))
val book = repo.bookById(bookId)!!
val arguments = args.getParcelable(NI_ARGS) as Arguments
val book = repo.bookById(arguments.bookId)!!

container.coverReplacement.isVisible = true
container.cropOverlay.selectionOn = false
picasso.load(uri)
picasso.load(arguments.coverUri)
.into(
container.coverImage, object : PicassoCallback {
override fun onError(e: Exception?) {
Expand Down Expand Up @@ -103,7 +102,7 @@ class EditCoverDialogController(args: Bundle) : DialogController(args) {
}
// picasso only holds a weak reference so we have to protect against gc
container.coverImage.tag = target
picasso.load(uri)
picasso.load(arguments.coverUri)
.transform(CropTransformation(container.cropOverlay, container.coverImage))
.into(target)
} else dismissDialog()
Expand All @@ -116,21 +115,21 @@ class EditCoverDialogController(args: Bundle) : DialogController(args) {
}

companion object {
private const val NI_COVER_URI = "ni#coverPath"
private const val NI_BOOK_ID = "ni#id"

operator fun <T> invoke(
target: T,
bookId: UUID,
uri: Uri
): EditCoverDialogController where T : Controller, T : Callback {
val args = Bundle().apply {
putString(NI_COVER_URI, uri.toString())
putUUID(NI_BOOK_ID, bookId)
private const val NI_ARGS = "ni#bundle"

operator fun <T> invoke(target: T, args: Arguments): EditCoverDialogController where T : Controller, T : Callback {
val bundle = Bundle().apply {
putParcelable(NI_ARGS, args)
}
return EditCoverDialogController(args).apply {
return EditCoverDialogController(bundle).apply {
targetController = target
}
}
}

@Parcelize
data class Arguments(
val coverUri: Uri,
val bookId: UUID
) : Parcelable
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import de.ph1b.audiobook.misc.tint
import de.ph1b.audiobook.uitools.ImageHelper
import io.reactivex.subjects.BehaviorSubject
import kotlinx.android.synthetic.main.image_picker.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.net.URLEncoder
import java.util.UUID
Expand Down Expand Up @@ -183,9 +185,11 @@ class CoverFromInternetController(bundle: Bundle) : BaseController(bundle) {
screenShot.recycle()
Picasso.get().invalidate(coverFile)
val targetController = targetController
if (targetController?.isAttached == true) {
targetController as Callback
targetController.onBookCoverChanged(book.id)
withContext(Dispatchers.Main) {
if (targetController?.isAttached == true) {
targetController as Callback
targetController.onBookCoverChanged(book.id)
}
}
}
}
Expand Down

0 comments on commit 1c41152

Please sign in to comment.