Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rotation 반영 #22

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ class PickleDataSource(val context: Context) : PositionalDataSource<PickleMedia>
val dateModified = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED))
val fileSize = cursor.getInt(cursor.getColumnIndex(MediaStore.Files.FileColumns.SIZE))
val mimeType = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE))
val orientation = cursor.getFloat(cursor.getColumnIndex(MediaStore.MediaColumns.ORIENTATION))

logger.d("id = $id bucketId = $bucketId contentUri = $contentUri data = $data mediaType = $mediaType isVideo = $isVideo dateModified = $dateModified fileSize = $fileSize mimeType = $mimeType")
return if(isVideo){
val duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DURATION))
Video(id,contentUri, data, dateModified, fileSize,duration)
}else{
Image(id, contentUri, data, dateModified, fileSize)
Image(id, contentUri, data, dateModified, fileSize, orientation)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ data class Image(
private val uri: Uri?,
private val data: String?,
val dateModified: Long,
val fileSize: Int) :PickleMedia {
val fileSize: Int,
val orientation: Float) :PickleMedia {
override fun getUri() =uri

override fun getData()=data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class CropLayout @JvmOverloads constructor(
}

fun removeOnCropListener(listener: OnCropListener) {
listeners.addIfAbsent(listener)
listeners.remove(listener)
}

/**
Expand Down Expand Up @@ -178,7 +178,7 @@ class CropLayout @JvmOverloads constructor(
}
}

fun setCropScale(uri: Uri) {
fun setCropScale(uri: Uri, orientation: Float) {
cropOverlay.visibility = View.VISIBLE
cropImageView.top = top
cropImageView.left = left
Expand All @@ -188,6 +188,7 @@ class CropLayout @JvmOverloads constructor(
cropImageView.adjustViewBounds = true
cropImageView.setImageURI(uri)
cropImageView.layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT, Gravity.CENTER)
cropImageView.rotation = orientation
cropImageView.requestLayout()
animator = GestureAnimator.of(cropImageView, frame, scale)
animation = GestureAnimation(cropOverlay, animator)
Expand All @@ -196,7 +197,7 @@ class CropLayout @JvmOverloads constructor(
logger.d("setCropScale() : cropImageView" + "(" + position[0] + ", " + position[1] + ") " + cropImageView.width + ", " + cropImageView.height)
}

fun setAspectRatio(uri: Uri) {
fun setAspectRatio(uri: Uri, orientation: Float) {
if (::animation.isInitialized) animation.stop()
cropOverlay.visibility = View.GONE
cropImageView.layoutParams = LayoutParams(MATCH_PARENT, MATCH_PARENT, Gravity.CENTER)
Expand All @@ -212,6 +213,7 @@ class CropLayout @JvmOverloads constructor(
cropImageView.maxHeight = height
cropImageView.scaleX = 1f
cropImageView.scaleY = 1f
cropImageView.rotation = orientation
cropImageView.requestLayout()
}

Expand All @@ -220,7 +222,6 @@ class CropLayout @JvmOverloads constructor(
return false
}


companion object {
private const val DEFAULT_MAX_SCALE = 2f

Expand Down
50 changes: 28 additions & 22 deletions pickle/src/main/java/life/sabujak/pickle/ui/insta/InstaFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package life.sabujak.pickle.ui.insta

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -18,6 +17,7 @@ import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.appbar.AppBarLayout
import life.sabujak.pickle.R
import life.sabujak.pickle.data.entity.Image
import life.sabujak.pickle.data.entity.PickleMedia
import life.sabujak.pickle.databinding.FragmentInstaBinding
import life.sabujak.pickle.ui.common.OptionMenuViewModel
Expand All @@ -36,8 +36,6 @@ class InstaFragment : Fragment(), OnInstaEventListener {
GridLayoutManager(context, 3)
}

var selectedUri: Uri? = null

override fun onAttach(context: Context) {
super.onAttach(context)
activity?.let {
Expand Down Expand Up @@ -78,9 +76,12 @@ class InstaFragment : Fragment(), OnInstaEventListener {
(activity as? AppCompatActivity)?.setSupportActionBar(binding.toolbar)
ivPreview.addOnCropListener(object : OnCropListener {
override fun onSuccess(bitmap: Bitmap) {
val view = layoutInflater.inflate(R.layout.dialog_result, null)
view.findViewById<ImageView>(R.id.iv_image).setImageBitmap(bitmap)
AlertDialog.Builder(ivPreview.context).setView(view).show()
val dialogLayout = layoutInflater.inflate(R.layout.dialog_result, null)
val dialogImageView = dialogLayout.findViewById<ImageView>(R.id.iv_image)
dialogImageView.setImageBitmap(bitmap)
dialogImageView.rotation = (instaViewModel.selectedPickleMedia as Image).orientation
dialogImageView.scaleType = ImageView.ScaleType.FIT_CENTER
AlertDialog.Builder(ivPreview.context).setView(dialogLayout).show()
}

override fun onFailure(e: Exception) {
Expand All @@ -99,7 +100,7 @@ class InstaFragment : Fragment(), OnInstaEventListener {
instaAdapter.submitList(pagedList)
})
instaViewModel.isAspectRatio.observe(viewLifecycleOwner, Observer {
if (!binding.ivPreview.isEmpty()) loadUri(selectedUri)
if (!binding.ivPreview.isEmpty()) loadPickleMedia(instaViewModel.selectedPickleMedia)
})
instaViewModel.initialLoadState.observe(viewLifecycleOwner, Observer {
logger.d("initialLoadState = $it")
Expand All @@ -113,30 +114,35 @@ class InstaFragment : Fragment(), OnInstaEventListener {
if (!binding.ivPreview.isOffFrame() && instaViewModel.isAspectRatio.value == false) {
binding.ivPreview.crop()
} else if (instaViewModel.isAspectRatio.value == true) {
val dialogContentView = layoutInflater.inflate(R.layout.dialog_result, null)
dialogContentView.findViewById<ImageView>(R.id.iv_image)
.setImageURI(selectedUri)
AlertDialog.Builder(it).setView(dialogContentView).show()
val dialogLayout = layoutInflater.inflate(R.layout.dialog_result, null)
val dialogImageView = dialogLayout.findViewById<ImageView>(R.id.iv_image)
dialogImageView.setImageURI(instaViewModel.selectedPickleMedia.getUri())
dialogImageView.rotation = (instaViewModel.selectedPickleMedia as Image).orientation
dialogImageView.scaleType = ImageView.ScaleType.FIT_CENTER
AlertDialog.Builder(it).setView(dialogLayout).show()
}
}
})
}

private fun loadUri(uri: Uri?){
uri?.let{
if (instaViewModel.isAspectRatio.value == true) binding.ivPreview.setAspectRatio(it)
else binding.ivPreview.setCropScale(it)
private fun loadPickleMedia(pickleMedia: PickleMedia) {
if (pickleMedia.getType() == PickleMedia.Type.PHOTO) {
val orientation = (pickleMedia as Image).orientation
pickleMedia.getUri()?.let {
if (instaViewModel.isAspectRatio.value == true) binding.ivPreview.setAspectRatio(
it,
orientation
)
else binding.ivPreview.setCropScale(it, orientation)
}
}
// TODO : VIDEO 에 대한 처리
}

override fun onItemClick(view: View?, pickleMedia: PickleMedia) {
pickleMedia.getUri()?.let{
selectedUri = it
if (instaViewModel.isAspectRatio.value == true) binding.ivPreview.setAspectRatio(it)
else binding.ivPreview.setCropScale(it)
}

view?.let{
instaViewModel.setSelected(pickleMedia)
loadPickleMedia(pickleMedia)
view?.let {
binding.recyclerView.smoothScrollBy(0, it.top)
binding.previewAppbarLayout.setExpanded(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class InstaViewModel(application: Application) : AndroidViewModel(application) {

private val _isAspectRatio = MutableLiveData<Boolean>().apply { postValue(false) }
var isAspectRatio: LiveData<Boolean> = _isAspectRatio
lateinit var selectedPickleMedia: PickleMedia

val selectionManager = InstaSelectionManager()

Expand Down Expand Up @@ -43,4 +44,8 @@ class InstaViewModel(application: Application) : AndroidViewModel(application) {
if (_isAspectRatio.value == true) _isAspectRatio.postValue(false)
else _isAspectRatio.postValue(true)
}

fun setSelected(selected: PickleMedia){
selectedPickleMedia = selected
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class PhotoVideoCursor(context:Context):PickleCursor(context){
MediaStore.Files.FileColumns.DATE_MODIFIED,
MediaStore.Files.FileColumns.SIZE,
MediaStore.Files.FileColumns.MIME_TYPE,
MediaStore.Video.VideoColumns.DURATION
MediaStore.Video.VideoColumns.DURATION,
MediaStore.MediaColumns.ORIENTATION
)

val selection = "" +
Expand Down
2 changes: 1 addition & 1 deletion pickle/src/main/res/layout/dialog_result.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
app:layout_constraintDimensionRatio="h,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title"
app:layout_constraintTop_toBottomOf="@id/tv_title"
/>

</androidx.constraintlayout.widget.ConstraintLayout>