From 122209fb283e9175befbc3f6051539a319a2ca80 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 6 Nov 2016 16:08:48 +0100 Subject: [PATCH] convert MyPagerAdapter to kotlin --- .../gallery/adapters/MyPagerAdapter.java | 84 ------------------- .../gallery/adapters/MyPagerAdapter.kt | 67 +++++++++++++++ 2 files changed, 67 insertions(+), 84 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java create mode 100644 app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.kt diff --git a/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java b/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java deleted file mode 100644 index b448abb2a..000000000 --- a/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.simplemobiletools.gallery.adapters; - -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentStatePagerAdapter; - -import com.simplemobiletools.gallery.Constants; -import com.simplemobiletools.gallery.activities.ViewPagerActivity; -import com.simplemobiletools.gallery.fragments.PhotoFragment; -import com.simplemobiletools.gallery.fragments.VideoFragment; -import com.simplemobiletools.gallery.fragments.ViewPagerFragment; -import com.simplemobiletools.gallery.models.Medium; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class MyPagerAdapter extends FragmentStatePagerAdapter { - private final List mMedia; - private final Map mFragments; - private final ViewPagerActivity mActivity; - - public MyPagerAdapter(ViewPagerActivity act, FragmentManager fm, List media) { - super(fm); - mActivity = act; - mMedia = media; - mFragments = new HashMap<>(); - } - - @Override - public int getCount() { - return mMedia.size(); - } - - @Override - public Fragment getItem(int position) { - final Medium medium = mMedia.get(position); - final Bundle bundle = new Bundle(); - bundle.putSerializable(Constants.MEDIUM, medium); - ViewPagerFragment fragment; - - if (medium.getIsVideo()) { - fragment = new VideoFragment(); - } else { - fragment = new PhotoFragment(); - } - - mFragments.put(position, fragment); - fragment.setArguments(bundle); - fragment.setListener(mActivity); - return fragment; - } - - public void itemDragged(int pos) { - if (mFragments.get(pos) != null) { - mFragments.get(pos).itemDragged(); - } - } - - public void updateUiVisibility(boolean isFullscreen, int pos) { - for (int i = -1; i <= 1; i++) { - final ViewPagerFragment fragment = mFragments.get(pos + i); - if (fragment != null) { - fragment.systemUiVisibilityChanged(isFullscreen); - } - } - } - - public void updateItems(int pos) { - for (int i = -1; i <= 1; i++) { - final ViewPagerFragment fragment = mFragments.get(pos + i); - if (fragment != null) { - fragment.updateItem(); - } - } - } - - public void updateItems(List newPaths) { - mMedia.clear(); - mMedia.addAll(newPaths); - notifyDataSetChanged(); - } -} diff --git a/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.kt b/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.kt new file mode 100644 index 000000000..d207c659e --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/gallery/adapters/MyPagerAdapter.kt @@ -0,0 +1,67 @@ +package com.simplemobiletools.gallery.adapters + +import android.os.Bundle +import android.support.v4.app.Fragment +import android.support.v4.app.FragmentManager +import android.support.v4.app.FragmentStatePagerAdapter +import android.util.SparseArray +import com.simplemobiletools.gallery.Constants +import com.simplemobiletools.gallery.activities.ViewPagerActivity +import com.simplemobiletools.gallery.fragments.PhotoFragment +import com.simplemobiletools.gallery.fragments.VideoFragment +import com.simplemobiletools.gallery.fragments.ViewPagerFragment +import com.simplemobiletools.gallery.models.Medium + +class MyPagerAdapter(val activity: ViewPagerActivity, fm: FragmentManager, val media: MutableList) : FragmentStatePagerAdapter(fm) { + private val mFragments: SparseArray + + init { + mFragments = SparseArray() + } + + override fun getCount(): Int { + return media.size + } + + override fun getItem(position: Int): Fragment { + val medium = media[position] + val bundle = Bundle() + bundle.putSerializable(Constants.MEDIUM, medium) + val fragment: ViewPagerFragment + + if (medium.isVideo) { + fragment = VideoFragment() + } else { + fragment = PhotoFragment() + } + + mFragments.put(position, fragment) + fragment.arguments = bundle + fragment.setListener(activity) + return fragment + } + + fun itemDragged(pos: Int) { + mFragments[pos]?.itemDragged() + } + + fun updateUiVisibility(isFullscreen: Boolean, pos: Int) { + for (i in -1..1) { + val fragment = mFragments[pos + i] + fragment?.systemUiVisibilityChanged(isFullscreen) + } + } + + fun updateItems(pos: Int) { + for (i in -1..1) { + val fragment = mFragments[pos + i] + fragment?.updateItem() + } + } + + fun updateItems(newPaths: List) { + media.clear() + media.addAll(newPaths) + notifyDataSetChanged() + } +}