convert PhotoFragment to kotlin

This commit is contained in:
tibbi 2016-11-13 22:00:33 +01:00
parent 8cc9c27416
commit aa36c42f13
5 changed files with 92 additions and 96 deletions

View file

@ -1,79 +0,0 @@
package com.simplemobiletools.gallery.fragments;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.models.Medium;
public class PhotoFragment extends ViewPagerFragment implements View.OnClickListener {
private SubsamplingScaleImageView mSubsamplingView;
private Medium mMedium;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.pager_photo_item, container, false);
mMedium = (Medium) getArguments().getSerializable(Constants.MEDIUM);
if (mMedium.getPath().startsWith("content://"))
mMedium.setPath(Utils.Companion.getRealPathFromURI(getContext(), Uri.parse(mMedium.getPath())));
if (mMedium == null)
return view;
mSubsamplingView = (SubsamplingScaleImageView) view.findViewById(R.id.photo_view);
if (mMedium.isGif()) {
mSubsamplingView.setVisibility(View.GONE);
final ImageView imageView = (ImageView) view.findViewById(R.id.gif_view);
imageView.setVisibility(View.VISIBLE);
Glide.with(getContext()).load(mMedium.getPath()).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView);
imageView.setOnClickListener(this);
} else {
mSubsamplingView.setDoubleTapZoomScale(1.2f);
mSubsamplingView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF);
mSubsamplingView.setImage(ImageSource.uri(mMedium.getPath()));
mSubsamplingView.setMaxScale(4f);
mSubsamplingView.setMinimumTileDpi(200);
mSubsamplingView.setOnClickListener(this);
}
return view;
}
@Override
public void itemDragged() {
}
@Override
public void systemUiVisibilityChanged(boolean toFullscreen) {
}
@Override
public void updateItem() {
mSubsamplingView.setImage(ImageSource.uri(mMedium.getPath()));
}
@Override
public void onClick(View v) {
photoClicked();
}
private void photoClicked() {
if (mListener == null)
mListener = (FragmentClickListener) getActivity();
mListener.fragmentClicked();
}
}

View file

@ -4,10 +4,8 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.KeyCharacterMap
@ -98,19 +96,6 @@ class Utils {
activity.startActivity(Intent.createChooser(intent, shareTitle))
}
fun getRealPathFromURI(context: Context, uri: Uri): String? {
var cursor: Cursor? = null
try {
val projection = arrayOf(MediaStore.Images.Media.DATA)
cursor = context.contentResolver.query(uri, projection, null, null, null)
val index = cursor!!.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor.moveToFirst()
return cursor.getString(index)
} finally {
cursor?.close()
}
}
fun isAStorageRootFolder(context: Context, path: String) = context.isAStorageRootFolder(path)
fun isPhotoVideo(file: File) = file.isPhotoVideo()

View file

@ -12,8 +12,8 @@ import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.scanPath
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.dialogs.SaveAsDialog
import com.simplemobiletools.gallery.extensions.getRealPathFromURI
import com.theartofdev.edmodo.cropper.CropImageView
import kotlinx.android.synthetic.main.activity_edit.*
import java.io.File
@ -80,7 +80,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
}
})
} else if (uri.scheme == "content") {
val newPath = Utils.getRealPathFromURI(applicationContext, uri) ?: ""
val newPath = applicationContext.getRealPathFromURI(uri) ?: ""
if (!newPath.isEmpty()) {
SaveAsDialog(this, newPath, object : SaveAsDialog.OnSaveAsListener {
override fun onSaveAsSuccess(filename: String) {

View file

@ -0,0 +1,19 @@
package com.simplemobiletools.gallery.extensions
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.MediaStore
fun Context.getRealPathFromURI(uri: Uri): String? {
var cursor: Cursor? = null
try {
val projection = arrayOf(MediaStore.Images.Media.DATA)
cursor = contentResolver.query(uri, projection, null, null, null)
val index = cursor!!.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor.moveToFirst()
return cursor.getString(index)
} finally {
cursor?.close()
}
}

View file

@ -0,0 +1,71 @@
package com.simplemobiletools.gallery.fragments
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.davemorrissey.labs.subscaleview.ImageSource
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import com.simplemobiletools.gallery.Constants
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.getRealPathFromURI
import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.pager_photo_item.view.*
class PhotoFragment : ViewPagerFragment(), View.OnClickListener {
lateinit var subsamplingView: SubsamplingScaleImageView
lateinit var medium: Medium
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val view = inflater.inflate(R.layout.pager_photo_item, container, false)
medium = arguments.getSerializable(Constants.MEDIUM) as Medium
if (medium.path.startsWith("content://"))
medium.path = context.getRealPathFromURI(Uri.parse(medium.path)) ?: ""
subsamplingView = view.photo_view
if (medium.isGif) {
subsamplingView.visibility = View.GONE
view.gif_view.apply {
visibility = View.VISIBLE
Glide.with(context).load(medium.path).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).into(this)
this.setOnClickListener(this@PhotoFragment)
}
} else {
subsamplingView.apply {
setDoubleTapZoomScale(1.2f)
orientation = SubsamplingScaleImageView.ORIENTATION_USE_EXIF
setImage(ImageSource.uri(medium.path))
maxScale = 4f
setMinimumTileDpi(200)
setOnClickListener(this@PhotoFragment)
}
}
return view
}
override fun itemDragged() {
}
override fun systemUiVisibilityChanged(toFullscreen: Boolean) {
}
override fun updateItem() {
subsamplingView.setImage(ImageSource.uri(medium.path))
}
override fun onClick(v: View) {
photoClicked()
}
private fun photoClicked() {
if (mListener == null)
mListener = activity as ViewPagerFragment.FragmentClickListener
mListener.fragmentClicked()
}
}