From 9db537d339faa3ad44dfdcadb23f4c14bd8aceb4 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Thu, 5 Oct 2017 15:58:59 +0900 Subject: kotlin work part. 1 --- .../view/adapter/CustomListAdapter.java | 50 -------- .../smileessence/view/adapter/CustomListAdapter.kt | 45 +++++++ .../view/adapter/EventListAdapter.java | 116 ----------------- .../smileessence/view/adapter/EventListAdapter.kt | 102 +++++++++++++++ .../view/adapter/MessageListAdapter.java | 53 -------- .../view/adapter/MessageListAdapter.kt | 43 +++++++ .../view/adapter/OrderedCustomListAdapter.java | 41 ------ .../view/adapter/OrderedCustomListAdapter.kt | 35 ++++++ .../smileessence/view/adapter/PageListAdapter.java | 140 --------------------- .../smileessence/view/adapter/PageListAdapter.kt | 110 ++++++++++++++++ .../smileessence/view/adapter/TimelineAdapter.java | 39 ------ .../smileessence/view/adapter/TimelineAdapter.kt | 29 +++++ 12 files changed, 364 insertions(+), 439 deletions(-) delete mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.java create mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.kt delete mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/EventListAdapter.java create mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/EventListAdapter.kt delete mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/MessageListAdapter.java create mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/MessageListAdapter.kt delete mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.java create mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.kt delete mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.java create mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.kt delete mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/TimelineAdapter.java create mode 100644 app/src/main/java/net/lacolaco/smileessence/view/adapter/TimelineAdapter.kt (limited to 'app/src/main/java/net/lacolaco/smileessence/view/adapter') diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.java b/app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.java deleted file mode 100644 index 6d7420fb..00000000 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.java +++ /dev/null @@ -1,50 +0,0 @@ -package net.lacolaco.smileessence.view.adapter; - -import android.widget.BaseAdapter; -import net.lacolaco.smileessence.util.UIHandler; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public abstract class CustomListAdapter extends BaseAdapter { - private boolean isNotifiable = true; - private List frozenList = new ArrayList<>(); - - public final void setNotifiable(boolean notifiable) { - isNotifiable = notifiable; - } - - @Override - public final int getCount() { - return frozenList.size(); - } - - @Override - public final T getItem(int position) { - return frozenList.get(position); - } - - @Override - public long getItemId(int position) { - return position; - } - - @Override - public final void notifyDataSetChanged() { - frozenList = Collections.unmodifiableList(getList()); - super.notifyDataSetChanged(); - } - - protected abstract List getList(); - - public void update() { - if (isNotifiable) { - updateForce(); - } - } - - public void updateForce() { - new UIHandler().post(this::notifyDataSetChanged); - } -} \ No newline at end of file diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.kt b/app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.kt new file mode 100644 index 00000000..3b022012 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/view/adapter/CustomListAdapter.kt @@ -0,0 +1,45 @@ +package net.lacolaco.smileessence.view.adapter + +import android.widget.BaseAdapter +import net.lacolaco.smileessence.util.UIHandler + +import java.util.ArrayList +import java.util.Collections + +abstract class CustomListAdapter : BaseAdapter() { + private var isNotifiable = true + private var frozenList: List = ArrayList() + + fun setNotifiable(notifiable: Boolean) { + isNotifiable = notifiable + } + + override fun getCount(): Int { + return frozenList.size + } + + override fun getItem(position: Int): T { + return frozenList[position] + } + + override fun getItemId(position: Int): Long { + return position.toLong() + } + + override fun notifyDataSetChanged() { + frozenList = Collections.unmodifiableList(list) + super.notifyDataSetChanged() + } + + protected abstract val list: List + + fun update() { + if (isNotifiable) { + updateForce() + } + } + + fun updateForce() { + UIHandler().post { this.notifyDataSetChanged() } + } +} \ No newline at end of file diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/EventListAdapter.java b/app/src/main/java/net/lacolaco/smileessence/view/adapter/EventListAdapter.java deleted file mode 100644 index 3f98cfaa..00000000 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/EventListAdapter.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2012-2014 lacolaco.net - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package net.lacolaco.smileessence.view.adapter; - -import android.app.Activity; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; -import com.android.volley.toolbox.NetworkImageView; -import net.lacolaco.smileessence.R; -import net.lacolaco.smileessence.World; -import net.lacolaco.smileessence.data.ImageCache; -import net.lacolaco.smileessence.entity.Event; -import net.lacolaco.smileessence.entity.RBinding; -import net.lacolaco.smileessence.preference.UserPreferenceHelper; -import net.lacolaco.smileessence.util.StringUtils; -import net.lacolaco.smileessence.util.UIObserverBundle; -import net.lacolaco.smileessence.view.DialogHelper; -import net.lacolaco.smileessence.view.dialog.UserDetailDialogFragment; -import net.lacolaco.smileessence.view.listener.ListItemClickListener; - -import java.lang.ref.WeakReference; -import java.util.List; - -public class EventListAdapter extends CustomListAdapter { - private final World world; - private final Activity activity; - - public EventListAdapter(World world, Activity activity) { - super(); - this.world = world; - this.activity = activity; - } - - @Override - protected List getList() { - return world.getEvents(); - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - Event event = getItem(position); - if (convertView == null) { - convertView = activity.getLayoutInflater().inflate(R.layout.list_item_status, null); - } - UIObserverBundle bundle = (UIObserverBundle) convertView.getTag(); - if (bundle != null) { - bundle.detachAll(); - } else { - bundle = new UIObserverBundle(); - convertView.setTag(bundle); - } - - int textSize = UserPreferenceHelper.getInstance().getTextSize(); - - TextView header = (TextView) convertView.findViewById(R.id.textview_status_header); - header.setTextSize(textSize); - - updateViewUser(event, convertView); - - TextView content = (TextView) convertView.findViewById(R.id.textview_status_text); - content.setTextSize(textSize); - content.setText(event.getTargetObject() != null ? event.getTargetObject().getText() : ""); - TextView footer = (TextView) convertView.findViewById(R.id.textview_status_footer); - footer.setTextSize(textSize - 2); - footer.setText(StringUtils.dateToString(event.getCreatedAt())); - ImageView favorited = (ImageView) convertView.findViewById(R.id.imageview_status_favorited); - favorited.setVisibility(View.GONE); - convertView.setOnClickListener(new ListItemClickListener(activity, () -> { - UserDetailDialogFragment fragment = new UserDetailDialogFragment(); - fragment.setUserID(event.getSource().getId()); - DialogHelper.showDialog(activity, fragment); - })); - - final WeakReference weakView = new WeakReference<>(convertView); - bundle.attach(event.getSource(), changes -> { - View strongView = weakView.get(); - if (strongView != null && changes.contains(RBinding.BASIC)) - updateViewUser(event, strongView); - }); - - return convertView; - } - - private void updateViewUser(Event event, View convertedView) { - NetworkImageView icon = (NetworkImageView) convertedView.findViewById(R.id.imageview_status_icon); - String iconUrl = event.getSource().getProfileImageUrlOriginal(); - icon.setImageUrl(iconUrl, ImageCache.getImageLoader()); - - TextView header = (TextView) convertedView.findViewById(R.id.textview_status_header); - header.setText(event.getFormattedString()); - } -} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/EventListAdapter.kt b/app/src/main/java/net/lacolaco/smileessence/view/adapter/EventListAdapter.kt new file mode 100644 index 00000000..1c19d4ec --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/view/adapter/EventListAdapter.kt @@ -0,0 +1,102 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.view.adapter + +import android.app.Activity +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import com.android.volley.toolbox.NetworkImageView +import net.lacolaco.smileessence.R +import net.lacolaco.smileessence.World +import net.lacolaco.smileessence.data.ImageCache +import net.lacolaco.smileessence.entity.Event +import net.lacolaco.smileessence.entity.RBinding +import net.lacolaco.smileessence.preference.UserPreferenceHelper +import net.lacolaco.smileessence.util.StringUtils +import net.lacolaco.smileessence.util.UIObserverBundle +import net.lacolaco.smileessence.view.DialogHelper +import net.lacolaco.smileessence.view.dialog.UserDetailDialogFragment +import net.lacolaco.smileessence.view.listener.ListItemClickListener + +import java.lang.ref.WeakReference + +class EventListAdapter(private val world: World, private val activity: Activity) : CustomListAdapter() { + override val list: List + get() = world.events + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + var convertView = convertView + val event = getItem(position) + if (convertView == null) { + convertView = activity.layoutInflater.inflate(R.layout.list_item_status, null) + } + var bundle: UIObserverBundle? = convertView!!.tag as UIObserverBundle? + if (bundle != null) { + bundle.detachAll() + } else { + bundle = UIObserverBundle() + convertView.tag = bundle + } + + val textSize = UserPreferenceHelper.instance.textSize + + val header = convertView.findViewById(R.id.textview_status_header) as TextView + header.textSize = textSize.toFloat() + + updateViewUser(event, convertView) + + val content = convertView.findViewById(R.id.textview_status_text) as TextView + content.textSize = textSize.toFloat() + content.text = event.targetObject?.text ?: "" + val footer = convertView.findViewById(R.id.textview_status_footer) as TextView + footer.textSize = (textSize - 2).toFloat() + footer.text = StringUtils.dateToString(event.createdAt) + val favorited = convertView.findViewById(R.id.imageview_status_favorited) as ImageView + favorited.visibility = View.GONE + convertView.setOnClickListener(ListItemClickListener(activity) { + DialogHelper.showDialog(activity, UserDetailDialogFragment.newInstance(event.source)) + }) + + val weakView = WeakReference(convertView) + bundle.attach(event.source) { changes -> + val strongView = weakView.get() + if (strongView != null && changes.contains(RBinding.BASIC)) + updateViewUser(event, strongView) + } + + return convertView + } + + private fun updateViewUser(event: Event, convertedView: View) { + val icon = convertedView.findViewById(R.id.imageview_status_icon) as NetworkImageView + val iconUrl = event.source.profileImageUrl + icon.setImageUrl(iconUrl, ImageCache.getImageLoader()) + + val header = convertedView.findViewById(R.id.textview_status_header) as TextView + header.text = event.formattedString + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/MessageListAdapter.java b/app/src/main/java/net/lacolaco/smileessence/view/adapter/MessageListAdapter.java deleted file mode 100644 index 9f8e5039..00000000 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/MessageListAdapter.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2012-2014 lacolaco.net - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package net.lacolaco.smileessence.view.adapter; - -import android.app.Activity; -import android.view.View; -import android.view.ViewGroup; -import net.lacolaco.smileessence.entity.DirectMessage; -import net.lacolaco.smileessence.view.Partials; - -public class MessageListAdapter extends OrderedCustomListAdapter { - private final Activity activity; - - public MessageListAdapter(Activity activity) { - super(); - this.activity = activity; - } - - public long getLastID() { - return getCount() > 0 ? getItem(getCount() - 1).getId() : -1; - } - - public long getTopID() { - return getCount() > 0 ? getItem(0).getId() : -1; - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - return Partials.getDirectMessageView(getItem(position), activity, convertView); - } -} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/MessageListAdapter.kt b/app/src/main/java/net/lacolaco/smileessence/view/adapter/MessageListAdapter.kt new file mode 100644 index 00000000..d72ab564 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/view/adapter/MessageListAdapter.kt @@ -0,0 +1,43 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.view.adapter + +import android.app.Activity +import android.view.View +import android.view.ViewGroup +import net.lacolaco.smileessence.entity.DirectMessage +import net.lacolaco.smileessence.view.Partials + +class MessageListAdapter(private val activity: Activity) : OrderedCustomListAdapter() { + val lastID: Long + get() = if (count > 0) getItem(count - 1).id else -1 + + val topID: Long + get() = if (count > 0) getItem(0).id else -1 + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + return Partials.getDirectMessageView(getItem(position), activity, convertView) + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.java b/app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.java deleted file mode 100644 index 3de58658..00000000 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.java +++ /dev/null @@ -1,41 +0,0 @@ -package net.lacolaco.smileessence.view.adapter; - -import net.lacolaco.smileessence.entity.IdObject; - -import java.util.*; - -public abstract class OrderedCustomListAdapter extends CustomListAdapter { - private final Map treeMap; - - public OrderedCustomListAdapter() { - this(Long::compare); - } - - public OrderedCustomListAdapter(Comparator comparator) { - super(); - this.treeMap = new TreeMap<>(Collections.reverseOrder(comparator)); - } - - @Override - protected synchronized List getList() { - return new ArrayList<>(treeMap.values()); - } - - public synchronized void add(T item) { - treeMap.put(item.getId(), item); - } - - public synchronized void addAll(Collection items) { - for (T item : items) { - treeMap.put(item.getId(), item); - } - } - - public synchronized void clear() { - treeMap.clear(); - } - - public synchronized T remove(T item) { - return treeMap.remove(item.getId()); - } -} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.kt b/app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.kt new file mode 100644 index 00000000..53b1c824 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/view/adapter/OrderedCustomListAdapter.kt @@ -0,0 +1,35 @@ +package net.lacolaco.smileessence.view.adapter + +import net.lacolaco.smileessence.entity.IdObject + +import java.util.* + +abstract class OrderedCustomListAdapter +constructor(comparator: Comparator = Comparator { x, y -> java.lang.Long.compare(x, y) }) : CustomListAdapter() { + private val treeMap: MutableMap = TreeMap(Collections.reverseOrder(comparator)) + + override val list: List + @Synchronized get() = ArrayList(treeMap.values) + + @Synchronized + fun add(item: T) { + treeMap.put(item.id, item) + } + + @Synchronized + fun addAll(items: Collection) { + for (item in items) { + treeMap.put(item.id, item) + } + } + + @Synchronized + fun clear() { + treeMap.clear() + } + + @Synchronized + fun remove(item: T): T { + return treeMap.remove(item.id)!! + } +} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.java b/app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.java deleted file mode 100644 index 76e72093..00000000 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2012-2014 lacolaco.net - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package net.lacolaco.smileessence.view.adapter; - -import android.os.Bundle; -import android.support.v13.app.FragmentPagerAdapter; -import net.lacolaco.smileessence.activity.MainActivity; -import net.lacolaco.smileessence.logging.Logger; -import net.lacolaco.smileessence.view.page.PageFragment; - -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class PageListAdapter extends FragmentPagerAdapter { - - // ------------------------------ FIELDS ------------------------------ - - private final List pages = new ArrayList<>(); - private final Map> fragmentCache = new HashMap<>(); - - // --------------------------- FragmentPagerAdapter --------------------------- - - public PageListAdapter(MainActivity _activity) { - super(_activity.getFragmentManager()); - } - - @Override - public synchronized PageFragment getItem(int position) { - PageFragment pf; - PageInfo info = pages.get(position); - try { - pf = info.getFragmentClass().newInstance(); - } catch (Exception e) { - Logger.error("should not happen: fragmentClass is private or Android is broken?"); - e.printStackTrace(); - throw new RuntimeException(e); - } - pf.setArguments(info.getArgs()); - fragmentCache.put(position, new WeakReference<>(pf)); - return pf; - } - - public synchronized PageFragment getCachedFragment(int pos) { - WeakReference wpf = fragmentCache.get(pos); - if (wpf == null) { - return null; - } else { - return wpf.get(); - } - } - - @Override - public synchronized int getCount() { - return pages.size(); - } - - // ------------------------ INTERFACE METHODS ------------------------ - - // -------------------------- OTHER METHODS -------------------------- - - public void addPage(Class klass, String name, Bundle args) { - this.addPage(klass, name, args, true); - } - - public void addPage(Class klass, String name, Bundle args, boolean notifyChanged) { - pages.add(new PageInfo(klass, name, args)); - if (notifyChanged) notifyDataSetChanged(); - } - - public synchronized boolean removePage(int position) { - //if (removePageWithoutNotify(position)) { - // refreshListNavigation(); - // return true; - //} - return pages.remove(position) != null; // TODO - } - - public String getName(int pos) { - return pages.get(pos).getName(); - } - - @Deprecated - public int getIndex(Class fragmentClass) { - for (int i = 0; i < pages.size(); ++i) { - if (pages.get(i).getFragmentClass() == fragmentClass) { - return i; - } - } - return -1; - } - - private static final class PageInfo { - private final Class fragmentClass; - private final Bundle args; - private final String name; - - PageInfo(Class _fragmentClass, String _name, Bundle _args) { - fragmentClass = _fragmentClass; - name = _name; - args = _args; - } - - public Class getFragmentClass() { - return fragmentClass; - } - - public String getName() { - return name; - } - - public Bundle getArgs() { - return args; - } - } -} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.kt b/app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.kt new file mode 100644 index 00000000..34d158f0 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/view/adapter/PageListAdapter.kt @@ -0,0 +1,110 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.view.adapter + +import android.os.Bundle +import android.support.v13.app.FragmentPagerAdapter +import net.lacolaco.smileessence.activity.MainActivity +import net.lacolaco.smileessence.logging.Logger +import net.lacolaco.smileessence.view.page.PageFragment + +import java.lang.ref.WeakReference +import java.util.ArrayList +import java.util.HashMap + +class PageListAdapter +// --------------------------- FragmentPagerAdapter --------------------------- + +(_activity: MainActivity) : FragmentPagerAdapter(_activity.fragmentManager) { + + // ------------------------------ FIELDS ------------------------------ + + private val pages = ArrayList() + private val fragmentCache = HashMap>>() + + @Synchronized override fun getItem(position: Int): PageFragment<*> { + val pf: PageFragment<*> + val info = pages[position] + try { + pf = info.fragmentClass.newInstance() + } catch (e: Exception) { + Logger.error("should not happen: fragmentClass is private or Android is broken?") + e.printStackTrace() + throw RuntimeException(e) + } + + pf.arguments = info.args + fragmentCache.put(position, WeakReference(pf)) + return pf + } + + @Synchronized + fun getCachedFragment(pos: Int): PageFragment<*>? { + val wpf = fragmentCache[pos] + return wpf?.get() + } + + @Synchronized override fun getCount(): Int { + return pages.size + } + + // ------------------------ INTERFACE METHODS ------------------------ + + // -------------------------- OTHER METHODS -------------------------- + + fun addPage(klass: Class>, name: String, args: Bundle) { + this.addPage(klass, name, args, true) + } + + fun addPage(klass: Class>, name: String, args: Bundle, notifyChanged: Boolean) { + pages.add(PageInfo(klass, name, args)) + if (notifyChanged) notifyDataSetChanged() + } + + @Synchronized + fun removePage(position: Int): Boolean { + //if (removePageWithoutNotify(position)) { + // refreshListNavigation(); + // return true; + //} + return pages.removeAt(position) != null // TODO + } + + fun getName(pos: Int): String { + return pages[pos].name + } + + @Deprecated("") + fun getIndex(fragmentClass: Class>): Int { + for (i in pages.indices) { + if (pages[i].fragmentClass == fragmentClass) { + return i + } + } + return -1 + } + + private class PageInfo internal constructor(val fragmentClass: Class>, val name: String, val args: Bundle) +} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/TimelineAdapter.java b/app/src/main/java/net/lacolaco/smileessence/view/adapter/TimelineAdapter.java deleted file mode 100644 index 0dbe8d53..00000000 --- a/app/src/main/java/net/lacolaco/smileessence/view/adapter/TimelineAdapter.java +++ /dev/null @@ -1,39 +0,0 @@ -package net.lacolaco.smileessence.view.adapter; - -import android.app.Activity; -import android.view.View; -import android.view.ViewGroup; -import net.lacolaco.smileessence.entity.Tweet; -import net.lacolaco.smileessence.view.Partials; - -import java.util.*; - -public class TimelineAdapter extends OrderedCustomListAdapter { - private final Activity activity; - - public TimelineAdapter(Activity activity) { - super(); - this.activity = activity; - } - - public long getLastID() { - if (getCount() > 0) { - return getItem(getCount() - 1).getId(); - } else { - return -1; - } - } - - public long getTopID() { - if (getCount() > 0) { - return getItem(0).getId(); - } else { - return -1; - } - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - return Partials.getTweetView(getItem(position), activity, convertView, true); - } -} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/adapter/TimelineAdapter.kt b/app/src/main/java/net/lacolaco/smileessence/view/adapter/TimelineAdapter.kt new file mode 100644 index 00000000..750b63a0 --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/view/adapter/TimelineAdapter.kt @@ -0,0 +1,29 @@ +package net.lacolaco.smileessence.view.adapter + +import android.app.Activity +import android.view.View +import android.view.ViewGroup +import net.lacolaco.smileessence.entity.Tweet +import net.lacolaco.smileessence.view.Partials + +import java.util.* + +class TimelineAdapter(private val activity: Activity) : OrderedCustomListAdapter() { + val lastID: Long + get() = if (count > 0) { + getItem(count - 1).id + } else { + -1 + } + + val topID: Long + get() = if (count > 0) { + getItem(0).id + } else { + -1 + } + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + return Partials.getTweetView(getItem(position), activity, convertView, true) + } +} -- cgit v1.2.3