Skip to content

Commit

Permalink
Merge pull request #157 from shockbytes/update-image-picker-dep
Browse files Browse the repository at this point in the history
Update image picker dep
  • Loading branch information
shockbytes authored Sep 23, 2023
2 parents 1118fd4 + c3019ad commit 626fd7c
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 69 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "at.shockbytes.dante"
minSdkVersion 21
targetSdkVersion 33
versionCode 48
versionName "4.2.0"
versionCode 49
versionName "4.2.1"

multiDexEnabled true
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package at.shockbytes.dante.ui.fragment

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.ViewModelProvider
import android.graphics.drawable.BitmapDrawable
Expand Down Expand Up @@ -66,16 +67,32 @@ class ManualAddFragment : BaseFragment<FragmentManualAddBinding>(), ImageLoading
.let(viewModel::initialize)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (resultCode == Activity.RESULT_OK) {
val url = data?.data
if (url != null) {
viewModel.imagePicked(url)
} else {
showSnackbar(getString(R.string.pick_image_error))
}
} else {
showSnackbar(getString(R.string.pick_image_error))
}
}

override fun setupViews() {

vb.cardImageManualAdd.setOnClickListener { v ->
v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
viewModel.pickImage(requireActivity())

viewModel.pickImage(this, PICK_IMAGE_REQ_CODE)
}

vb.editTextManualAddTitle.doOnTextChanged { text, _, _, _ ->
(activity as? TintableBackNavigableActivity<*>)
?.tintTitle(text.toString().toUpperCase(Locale.getDefault()))
?.tintTitle(text.toString().uppercase(Locale.getDefault()))
}

vb.btnManualAddUpcoming.setOnClickListener { v ->
Expand Down Expand Up @@ -178,6 +195,7 @@ class ManualAddFragment : BaseFragment<FragmentManualAddBinding>(), ImageLoading
callbackHandleValues = Pair(first = false, second = true)
)
}

ManualAddViewModel.ImageState.NoImage -> {
imageLoader.loadImageResource(
requireContext(),
Expand All @@ -194,10 +212,12 @@ class ManualAddFragment : BaseFragment<FragmentManualAddBinding>(), ImageLoading
vb.pbManualAddImageUpload.setVisible(true)
vb.imgViewManualAdd.setVisible(false)
}

is ManualAddViewModel.ImageLoadingState.Error -> {
vb.pbManualAddImageUpload.setVisible(false)
vb.imgViewManualAdd.setVisible(true)
}

ManualAddViewModel.ImageLoadingState.Success -> Unit // Not needed...
}
}
Expand All @@ -208,6 +228,7 @@ class ManualAddFragment : BaseFragment<FragmentManualAddBinding>(), ImageLoading
vb.containerManualAddButtons.setVisible(true)
vb.containerUpdateBookButtons.setVisible(false)
}

is ManualAddViewModel.ViewState.UpdateBook -> {
vb.containerManualAddButtons.setVisible(false)
vb.containerUpdateBookButtons.setVisible(true)
Expand All @@ -222,10 +243,14 @@ class ManualAddFragment : BaseFragment<FragmentManualAddBinding>(), ImageLoading
activity?.onBackPressed()
sendBookCreatedBroadcast(event.createdBookState)
}

is ManualAddViewModel.AddEvent.Error -> {
showSnackbar(getString(R.string.manual_add_error),
getString(android.R.string.ok), true) { this.dismiss() }
showSnackbar(
getString(R.string.manual_add_error),
getString(android.R.string.ok), true
) { this.dismiss() }
}

is ManualAddViewModel.AddEvent.Updated -> {
sendBookUpdatedBroadcast(event.updateBookState)
activity?.onBackPressed()
Expand Down Expand Up @@ -317,6 +342,8 @@ class ManualAddFragment : BaseFragment<FragmentManualAddBinding>(), ImageLoading

companion object {

private const val PICK_IMAGE_REQ_CODE = 0x4821

private const val ARG_BOOK_ENTITY_UPDATE = "arg_book_entity_update"

fun newInstance(updatedBookEntity: BookEntity?): ManualAddFragment {
Expand Down
70 changes: 41 additions & 29 deletions app/src/main/java/at/shockbytes/dante/ui/fragment/MenuFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package at.shockbytes.dante.ui.fragment

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.content.Intent
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import android.text.InputType
Expand Down Expand Up @@ -45,6 +48,7 @@ import javax.inject.Inject
* Author: Martin Macheiner
* Date: 06.06.2018
*/
@SuppressLint("CheckResult")
class MenuFragment : BaseBottomSheetFragment<BottomSheetMenuBinding>() {

@Inject
Expand All @@ -63,17 +67,13 @@ class MenuFragment : BaseBottomSheetFragment<BottomSheetMenuBinding>() {
override fun bindViewModel() {
userViewModel.getUserViewState().observe(this, Observer(::handleUserViewState))

userViewModel.onUserEvent()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(::handleUserEvent)
.addTo(compositeDisposable)
userViewModel.onUserEvent().observeOn(AndroidSchedulers.mainThread())
.subscribe(::handleUserEvent).addTo(compositeDisposable)
}


override fun createViewBinding(
inflater: LayoutInflater,
root: ViewGroup?,
attachToRoot: Boolean
inflater: LayoutInflater, root: ViewGroup?, attachToRoot: Boolean
): BottomSheetMenuBinding {
return BottomSheetMenuBinding.inflate(inflater, root, attachToRoot)
}
Expand All @@ -90,12 +90,11 @@ class MenuFragment : BaseBottomSheetFragment<BottomSheetMenuBinding>() {

val photoUrl = event.user.photoUrl
if (photoUrl != null) {
photoUrl.loadRoundedBitmap(requireContext())
.subscribe({ image ->
vb.profileHeaderMenu.imageView.setImageBitmap(image)
}, { throwable ->
throwable.printStackTrace()
})
photoUrl.loadRoundedBitmap(requireContext()).subscribe({ image ->
vb?.profileHeaderMenu?.imageView?.setImageBitmap(image)
}, { throwable ->
throwable.printStackTrace()
})
} else {
vb.profileHeaderMenu.imageView.setImageResource(R.drawable.ic_user_template_dark)
}
Expand Down Expand Up @@ -133,9 +132,7 @@ class MenuFragment : BaseBottomSheetFragment<BottomSheetMenuBinding>() {
ActivityNavigator.navigateTo(
context,
Destination.Login,
requireActivity()
.let(ActivityOptionsCompat::makeSceneTransitionAnimation)
.toBundle()
requireActivity().let(ActivityOptionsCompat::makeSceneTransitionAnimation).toBundle()
)

/*
Expand Down Expand Up @@ -233,20 +230,32 @@ class MenuFragment : BaseBottomSheetFragment<BottomSheetMenuBinding>() {
navigateToAndDismiss(Destination.Settings)
}

vb.profileActionViewMenu.onActionButtonClicked()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(::handleProfileClick)
.addTo(compositeDisposable)
vb.profileActionViewMenu.onActionButtonClicked().observeOn(AndroidSchedulers.mainThread())
.subscribe(::handleProfileClick).addTo(compositeDisposable)
}

private fun handleProfileClick(profileActionViewClick: ProfileActionViewClick) {
when (profileActionViewClick) {
ProfileActionViewClick.UPGRADE_ANONYMOUS_ACCOUNT -> showUpgradeBottomSheet()
ProfileActionViewClick.CHANGE_NAME -> showChangeNameScreen()
ProfileActionViewClick.CHANGE_IMAGE -> userViewModel.changeUserImage(requireActivity())
ProfileActionViewClick.CHANGE_IMAGE -> userViewModel.changeUserImage(this, REQ_CODE_IMAGE)
ProfileActionViewClick.CHANGE_PASSWORD -> showChangePasswordScreen()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (resultCode == Activity.RESULT_OK) {
val url = data?.data
if (url != null) {
userViewModel.userImagePicked(url)
} else {
showToast(R.string.pick_image_error)
}
} else {
showToast(R.string.pick_image_error)
}
}

private fun showChangeNameScreen() {
MaterialDialog(requireContext()).show {
Expand Down Expand Up @@ -281,16 +290,19 @@ class MenuFragment : BaseBottomSheetFragment<BottomSheetMenuBinding>() {
}

private fun showUpgradeBottomSheet() {
MailLoginBottomSheetDialogFragment
.newInstance(MailLoginViewModel.MailLoginState.ShowEmailAndPassword(isSignUp = true, R.string.anonymous_upgrade))
.setOnCredentialsEnteredListener(userViewModel::anonymousUpgrade)
.show(parentFragmentManager, "anonymous-upgrade-fragment")
MailLoginBottomSheetDialogFragment.newInstance(
MailLoginViewModel.MailLoginState.ShowEmailAndPassword(
isSignUp = true, R.string.anonymous_upgrade
)
).setOnCredentialsEnteredListener(userViewModel::anonymousUpgrade)
.show(parentFragment?.fragmentManager!!, "anonymous-upgrade-fragment")
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setOnShowListener {
val bottomSheet = dialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)!!
val bottomSheet =
dialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)!!
BottomSheetBehavior.from(bottomSheet).state = BottomSheetBehavior.STATE_EXPANDED
}
return dialog
Expand All @@ -301,16 +313,16 @@ class MenuFragment : BaseBottomSheetFragment<BottomSheetMenuBinding>() {
ActivityNavigator.navigateTo(
activity,
destination,
ActivityOptionsCompat
.makeSceneTransitionAnimation(requireActivity())
.toBundle()
ActivityOptionsCompat.makeSceneTransitionAnimation(requireActivity()).toBundle()
)

dismiss()
}

companion object {

private const val REQ_CODE_IMAGE = 0x7582

fun newInstance() = MenuFragment()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package at.shockbytes.dante.ui.viewmodel
import androidx.lifecycle.MutableLiveData
import android.net.Uri
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LiveData
import at.shockbytes.dante.core.book.BookEntity
Expand All @@ -14,6 +15,7 @@ import at.shockbytes.dante.ui.viewmodel.ManualAddViewModel.ImageState.ThumbnailU
import at.shockbytes.dante.util.ExceptionHandlers
import at.shockbytes.dante.util.addTo
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.subjects.PublishSubject
import timber.log.Timber
import javax.inject.Inject
Expand Down Expand Up @@ -91,9 +93,13 @@ class ManualAddViewModel @Inject constructor(
}
}

fun pickImage(activity: FragmentActivity) {
fun pickImage(fragment: Fragment, requestCode: Int) {
imagePicker
.openGallery(activity)
.openGallery(fragment, requestCode)
}

fun imagePicked(uri: Uri) {
Single.just(uri)
.flatMap { imageUri ->
imageUploadStorage.uploadCustomImage(imageUri, ::progressUpdate)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package at.shockbytes.dante.ui.viewmodel

import android.net.Uri
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
Expand All @@ -21,6 +23,7 @@ import at.shockbytes.tracking.event.DanteTrackingEvent
import at.shockbytes.tracking.properties.LoginSource
import io.reactivex.Completable
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.subjects.PublishSubject
import timber.log.Timber
import javax.inject.Inject
Expand Down Expand Up @@ -208,9 +211,13 @@ class UserViewModel @Inject constructor(
.addTo(compositeDisposable)
}

fun changeUserImage(activity: FragmentActivity) {
fun changeUserImage(fragment: Fragment, requestCode: Int) {
imagePicker
.openGallery(activity)
.openGallery(fragment, requestCode)
}

fun userImagePicked(uri: Uri) {
Single.just(uri)
.subscribeOn(schedulers.ui)
.observeOn(schedulers.ui)
.flatMap(imageUploadStorage::uploadUserImage)
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ dependencies {
api "com.jakewharton.timber:timber:$timberVersion"

// Image picker
api 'com.github.dhaval2404:imagepicker:1.7.5'
api 'com.github.dhaval2404:imagepicker:2.0'
api 'com.github.raquezha:InlineActivityResult:1.0.0-jitpack'

api "com.github.bumptech.glide:glide:$glideVersion"
Expand Down
2 changes: 1 addition & 1 deletion core/src/debug/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<resources>

<string name="app_name" translatable="false">Dante BETA</string>
<string name="app_version" translatable="false">4.2</string>
<string name="app_version" translatable="false">4.2.1</string>

</resources>
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
package at.shockbytes.dante.core.image.picker

import android.app.Activity
import android.net.Uri
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.Fragment
import com.github.dhaval2404.imagepicker.ImagePicker
import io.reactivex.Single

class DefaultImagePicking(private val imagePickerConfig: ImagePickerConfig) : ImagePicking {

override fun openGallery(activity: FragmentActivity): Single<Uri> {
return Single.create { emitter ->
ImagePicker.with(activity)
.galleryOnly()
.compress(imagePickerConfig.maxSize)
.maxResultSize(imagePickerConfig.maxWidth, imagePickerConfig.maxHeight)
.start { resultCode, data ->
if (resultCode == Activity.RESULT_OK) {
val url = data?.data
if (url != null) {
emitter.onSuccess(url)
} else {
emitter.tryOnError(NullPointerException("Data url is null!"))
}
} else {
emitter.tryOnError(IllegalStateException("Cannot open gallery! Activity result canceled!"))
}
}
}
override fun openGallery(fragment: Fragment, requestCode: Int) {
ImagePicker.with(fragment)
.galleryOnly()
.compress(imagePickerConfig.maxSize)
.maxResultSize(imagePickerConfig.maxWidth, imagePickerConfig.maxHeight)
.start(requestCode)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package at.shockbytes.dante.core.image.picker

import android.net.Uri
import androidx.fragment.app.FragmentActivity
import io.reactivex.Single
import androidx.fragment.app.Fragment

interface ImagePicking {

fun openGallery(activity: FragmentActivity): Single<Uri>
fun openGallery(fragment: Fragment, requestCode: Int)
}
Loading

0 comments on commit 626fd7c

Please sign in to comment.