From 0f74a8c8ec1968ffc2df8528fb846837192c1659 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Tue, 31 Jan 2017 21:10:20 +0800 Subject: [PATCH] implemented user list selector --- .../twidere/adapter/ArrayAdapter.java | 2 +- .../activity/UserListSelectorActivity.kt | 44 +++++++++++---- .../twidere/adapter/BaseArrayAdapter.kt | 25 ++++++++- .../twidere/adapter/MessageEntriesAdapter.kt | 2 +- .../adapter/ParcelableActivitiesAdapter.kt | 2 +- .../adapter/ParcelableGroupsAdapter.kt | 2 +- .../adapter/ParcelableStatusesAdapter.kt | 11 ++-- .../adapter/ParcelableUserListsAdapter.kt | 2 +- .../twidere/adapter/ParcelableUsersAdapter.kt | 2 +- .../SimpleParcelableUserListsAdapter.kt | 54 +++++++++++++++---- .../adapter/iface/IItemCountsAdapter.kt | 15 ++---- .../adapter/iface/ILoadMoreSupportAdapter.kt | 2 + .../twidere/extension/ListViewExtensions.kt | 8 +-- .../twidere/fragment/StatusFragment.kt | 2 +- .../fragment/UserListsOwnershipsFragment.kt | 32 +++++------ .../filter/BaseFiltersImportFragment.kt | 2 +- .../fragment/filter/FilteredUsersFragment.kt | 1 - .../twidere/loader/BaseUserListsLoader.kt | 9 ++++ .../loader/UserListOwnershipsLoader.kt | 3 +- .../org/mariotaku/twidere/model/ItemCounts.kt | 37 +++++++++++++ .../twidere/util/ContentScrollHandler.kt | 4 +- .../twidere/util/dagger/GeneralComponent.kt | 7 ++- ...cator.xml => list_item_load_indicator.xml} | 0 23 files changed, 193 insertions(+), 75 deletions(-) create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/model/ItemCounts.kt rename twidere/src/main/res/layout/{card_item_load_indicator.xml => list_item_load_indicator.xml} (100%) diff --git a/twidere/src/main/java/org/mariotaku/twidere/adapter/ArrayAdapter.java b/twidere/src/main/java/org/mariotaku/twidere/adapter/ArrayAdapter.java index bc3717919..38c70eeaf 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/adapter/ArrayAdapter.java +++ b/twidere/src/main/java/org/mariotaku/twidere/adapter/ArrayAdapter.java @@ -348,7 +348,7 @@ public class ArrayAdapter extends BaseAdapter implements Filterable { return createViewFromResource(position, convertView, parent, mResource); } - private View createViewFromResource(int position, View convertView, ViewGroup parent, + protected View createViewFromResource(int position, View convertView, ViewGroup parent, int resource) { View view; TextView text; diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/UserListSelectorActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/UserListSelectorActivity.kt index efdec5d9f..2bb04a2a1 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/UserListSelectorActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/UserListSelectorActivity.kt @@ -34,8 +34,11 @@ import org.mariotaku.ktextension.set import org.mariotaku.twidere.R import org.mariotaku.twidere.TwidereConstants.REQUEST_SELECT_USER import org.mariotaku.twidere.adapter.SimpleParcelableUserListsAdapter +import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter +import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter.IndicatorPosition import org.mariotaku.twidere.constant.IntentConstants.* import org.mariotaku.twidere.loader.UserListOwnershipsLoader +import org.mariotaku.twidere.loader.iface.ICursorSupportLoader import org.mariotaku.twidere.model.ParcelableUser import org.mariotaku.twidere.model.ParcelableUserList import org.mariotaku.twidere.model.UserKey @@ -54,6 +57,7 @@ class UserListSelectorActivity : BaseActivity(), get() = intent.getBooleanExtra(EXTRA_SHOW_MY_LISTS, false) private var userKey: UserKey? = null + private var nextCursor: Long = -1 private var loaderInitialized: Boolean = false @@ -66,12 +70,15 @@ class UserListSelectorActivity : BaseActivity(), setContentView(R.layout.activity_user_list_selector) adapter = SimpleParcelableUserListsAdapter(this) + adapter.loadMoreSupportedPosition = ILoadMoreSupportAdapter.END listView.addFooterView(layoutInflater.inflate(R.layout.simple_list_item_activated_1, listView, false).apply { (findViewById(android.R.id.text1) as TextView).setText(R.string.action_select_user) }, SelectUserAction, true) listView.adapter = adapter - listView.setOnScrollListener(ListViewScrollHandler(this, listView)) + val handler = ListViewScrollHandler(this, listView) + listView.setOnScrollListener(handler) + listView.setOnTouchListener(handler.touchListener) listView.onItemClickListener = OnItemClickListener { view, child, position, id -> val item = view.getItemAtPosition(position) when (item) { @@ -124,15 +131,27 @@ class UserListSelectorActivity : BaseActivity(), override fun onCreateLoader(id: Int, args: Bundle): Loader> { val accountKey = args.getParcelable(EXTRA_ACCOUNT_KEY) val userKey = args.getParcelable(EXTRA_USER_KEY) - return UserListOwnershipsLoader(this, accountKey, userKey, null, adapter.all) + val nextCursor = args.getLong(EXTRA_NEXT_CURSOR) + return UserListOwnershipsLoader(this, accountKey, userKey, null, nextCursor, adapter.all) } override fun onLoaderReset(loader: Loader>?) { adapter.setData(null) } + override fun onLoadFinished(loader: Loader>?, data: List?) { + adapter.loadMoreIndicatorPosition = ILoadMoreSupportAdapter.NONE + adapter.loadMoreSupportedPosition = if (adapter.all != data) { + ILoadMoreSupportAdapter.END + } else { + ILoadMoreSupportAdapter.NONE + } adapter.setData(data) + refreshing = false + if (loader is ICursorSupportLoader) { + nextCursor = loader.nextCursor + } showList() } @@ -144,19 +163,25 @@ class UserListSelectorActivity : BaseActivity(), return supportLoaderManager.hasRunningLoadersSafe() } set(value) { - } override val reachingStart: Boolean - get() = listView.firstVisiblePosition < 0 - override val reachingEnd: Boolean - get() = listView.lastVisiblePosition > (listView.count + listView.headerViewsCount - + listView.footerViewsCount) + get() = listView.firstVisiblePosition <= 0 - override fun onLoadMoreContents(position: Long) { + override val reachingEnd: Boolean + get() = listView.lastVisiblePosition >= listView.count - 1 + + override fun onLoadMoreContents(@IndicatorPosition position: Long) { + val accountKey = this.accountKey ?: return + val userKey = this.userKey ?: return + if (refreshing || position and adapter.loadMoreSupportedPosition == 0L) { + return + } + adapter.loadMoreIndicatorPosition = position + loadUserLists(accountKey, userKey, nextCursor) } - private fun loadUserLists(accountKey: UserKey, userKey: UserKey) { + private fun loadUserLists(accountKey: UserKey, userKey: UserKey, nextCursor: Long = -1) { if (userKey != this.userKey) { adapter.clear() showProgress() @@ -165,6 +190,7 @@ class UserListSelectorActivity : BaseActivity(), val args = Bundle { this[EXTRA_ACCOUNT_KEY] = accountKey this[EXTRA_USER_KEY] = userKey + this[EXTRA_NEXT_CURSOR] = nextCursor } if (!loaderInitialized) { loaderInitialized = true diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseArrayAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseArrayAdapter.kt index e581e07bd..15ab8f2f1 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseArrayAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/BaseArrayAdapter.kt @@ -23,7 +23,10 @@ import android.content.Context import android.support.v4.text.BidiFormatter import org.mariotaku.kpreferences.get import org.mariotaku.twidere.adapter.iface.IContentAdapter +import org.mariotaku.twidere.adapter.iface.IItemCountsAdapter +import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter import org.mariotaku.twidere.constant.* +import org.mariotaku.twidere.model.ItemCounts import org.mariotaku.twidere.util.* import org.mariotaku.twidere.util.dagger.GeneralComponentHelper import javax.inject.Inject @@ -32,7 +35,8 @@ open class BaseArrayAdapter( context: Context, layoutRes: Int, collection: Collection? = null -) : ArrayAdapter(context, layoutRes, collection), IContentAdapter { +) : ArrayAdapter(context, layoutRes, collection), IContentAdapter, ILoadMoreSupportAdapter, + IItemCountsAdapter { val linkify: TwidereLinkify @Inject @@ -54,6 +58,24 @@ open class BaseArrayAdapter( final override val showAbsoluteTime: Boolean val nameFirst: Boolean + override val itemCounts: ItemCounts = ItemCounts(1) + + @ILoadMoreSupportAdapter.IndicatorPosition + override var loadMoreSupportedPosition: Long = 0 + set(value) { + field = value + loadMoreIndicatorPosition = ILoadMoreSupportAdapter.apply(loadMoreIndicatorPosition, value) + notifyDataSetChanged() + } + + @ILoadMoreSupportAdapter.IndicatorPosition + override var loadMoreIndicatorPosition: Long = 0 + set(value) { + if (field == value) return + field = ILoadMoreSupportAdapter.apply(value, loadMoreSupportedPosition) + notifyDataSetChanged() + } + init { @Suppress("UNCHECKED_CAST") GeneralComponentHelper.build(context).inject(this as BaseArrayAdapter) @@ -67,5 +89,4 @@ open class BaseArrayAdapter( override fun getItemCount(): Int = count - } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessageEntriesAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessageEntriesAdapter.kt index 17c51b0e1..b39418606 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessageEntriesAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessageEntriesAdapter.kt @@ -69,7 +69,7 @@ class MessageEntriesAdapter(context: Context) : LoadMoreSupportAdapter { - val view = inflater.inflate(R.layout.card_item_load_indicator, parent, false) + val view = inflater.inflate(R.layout.list_item_load_indicator, parent, false) return LoadIndicatorViewHolder(view) } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableActivitiesAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableActivitiesAdapter.kt index 660542d36..9039389a3 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableActivitiesAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableActivitiesAdapter.kt @@ -208,7 +208,7 @@ class ParcelableActivitiesAdapter( return GapViewHolder(this, view) } ITEM_VIEW_TYPE_LOAD_INDICATOR -> { - val view = inflater.inflate(R.layout.card_item_load_indicator, parent, false) + val view = inflater.inflate(R.layout.list_item_load_indicator, parent, false) return LoadIndicatorViewHolder(view) } ITEM_VIEW_TYPE_STUB -> { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableGroupsAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableGroupsAdapter.kt index e3d4def4a..0f7b15923 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableGroupsAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableGroupsAdapter.kt @@ -102,7 +102,7 @@ class ParcelableGroupsAdapter(context: Context) : LoadMoreSupportAdapter { - val view = inflater.inflate(R.layout.card_item_load_indicator, parent, false) + val view = inflater.inflate(R.layout.list_item_load_indicator, parent, false) return LoadIndicatorViewHolder(view) } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableStatusesAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableStatusesAdapter.kt index 69a6e3060..cab6b292d 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableStatusesAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableStatusesAdapter.kt @@ -39,10 +39,7 @@ import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter.Companion.ITE import org.mariotaku.twidere.adapter.iface.IStatusesAdapter import org.mariotaku.twidere.constant.* import org.mariotaku.twidere.constant.SharedPreferenceConstants.KEY_DISPLAY_SENSITIVE_CONTENTS -import org.mariotaku.twidere.model.ObjectId -import org.mariotaku.twidere.model.ParcelableStatus -import org.mariotaku.twidere.model.ParcelableStatusCursorIndices -import org.mariotaku.twidere.model.UserKey +import org.mariotaku.twidere.model.* import org.mariotaku.twidere.util.MediaLoadingHandler import org.mariotaku.twidere.util.StatusAdapterLinkClickHandler import org.mariotaku.twidere.util.TwidereLinkify @@ -112,7 +109,7 @@ abstract class ParcelableStatusesAdapter( private var showingActionCardId = RecyclerView.NO_ID private var lastItemFiltered: Boolean = false - override val itemCounts: IntArray = IntArray(4) + override val itemCounts = ItemCounts(4) protected abstract val progressViewIds: IntArray @@ -263,7 +260,7 @@ abstract class ParcelableStatusesAdapter( return GapViewHolder(this, view) } ITEM_VIEW_TYPE_LOAD_INDICATOR -> { - val view = inflater.inflate(R.layout.card_item_load_indicator, parent, false) + val view = inflater.inflate(R.layout.list_item_load_indicator, parent, false) return LoadIndicatorViewHolder(view) } ITEM_VIEW_TYPE_EMPTY -> { @@ -344,7 +341,7 @@ abstract class ParcelableStatusesAdapter( itemCounts[1] = pinnedStatuses?.size ?: 0 itemCounts[2] = statusCount itemCounts[3] = if (position and ILoadMoreSupportAdapter.END != 0L) 1 else 0 - return itemCounts.sum() + return itemCounts.itemCount } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableUserListsAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableUserListsAdapter.kt index 24574127c..5406673cd 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableUserListsAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableUserListsAdapter.kt @@ -93,7 +93,7 @@ class ParcelableUserListsAdapter( return createUserListViewHolder(this, inflater, parent) } ITEM_VIEW_TYPE_LOAD_INDICATOR -> { - val view = inflater.inflate(R.layout.card_item_load_indicator, parent, false) + val view = inflater.inflate(R.layout.list_item_load_indicator, parent, false) return LoadIndicatorViewHolder(view) } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableUsersAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableUsersAdapter.kt index 751af70b8..c7ae30be9 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableUsersAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/ParcelableUsersAdapter.kt @@ -135,7 +135,7 @@ class ParcelableUsersAdapter(context: Context) : LoadMoreSupportAdapter { - val view = inflater.inflate(R.layout.card_item_load_indicator, parent, false) + val view = inflater.inflate(R.layout.list_item_load_indicator, parent, false) return LoadIndicatorViewHolder(view) } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/SimpleParcelableUserListsAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/SimpleParcelableUserListsAdapter.kt index 30acdd2fc..7609eb404 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/SimpleParcelableUserListsAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/SimpleParcelableUserListsAdapter.kt @@ -23,6 +23,8 @@ import android.content.Context import android.view.View import android.view.ViewGroup import org.mariotaku.twidere.R +import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter +import org.mariotaku.twidere.model.ItemCounts import org.mariotaku.twidere.model.ParcelableUserList import org.mariotaku.twidere.util.view.display import org.mariotaku.twidere.view.holder.SimpleUserListViewHolder @@ -31,21 +33,53 @@ class SimpleParcelableUserListsAdapter( context: Context ) : BaseArrayAdapter(context, R.layout.list_item_simple_user_list) { + override val itemCounts: ItemCounts = ItemCounts(2) + override fun getItemId(position: Int): Long { - return (if (getItem(position) != null) getItem(position).hashCode() else -1).toLong() + when (itemCounts.getItemCountIndex(position)) { + 0 -> { + return getItem(position - itemCounts.getItemStartPosition(0)).hashCode().toLong() + } + 1 -> { + return Integer.MAX_VALUE + 1L + } + } + throw UnsupportedOperationException() } override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { - val view = super.getView(position, convertView, parent) - val tag = view.tag - val holder = tag as? SimpleUserListViewHolder ?: run { - val h = SimpleUserListViewHolder(view) - view.tag = h - return@run h + when (getItemViewType(position)) { + 0 -> { + val view = super.getView(position, convertView, parent) + val holder = view.tag as? SimpleUserListViewHolder ?: run { + val h = SimpleUserListViewHolder(view) + view.tag = h + return@run h + } + val userList = getItem(position) + holder.display(userList, mediaLoader, userColorNameManager, profileImageEnabled) + return view + } + 1 -> { + val view = createViewFromResource(position, convertView, parent, R.layout.list_item_load_indicator) + return view + } } - val userList = getItem(position) - holder.display(userList, mediaLoader, userColorNameManager, profileImageEnabled) - return view + throw UnsupportedOperationException() + } + + override fun getItemViewType(position: Int): Int { + return itemCounts.getItemCountIndex(position) + } + + override fun getViewTypeCount(): Int { + return itemCounts.size + } + + override fun getCount(): Int { + itemCounts[0] = super.getCount() + itemCounts[1] = if (loadMoreIndicatorPosition and ILoadMoreSupportAdapter.END != 0L) 1 else 0 + return itemCounts.itemCount } fun setData(data: List?) { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/IItemCountsAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/IItemCountsAdapter.kt index c8c6d4370..6baeae7b9 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/IItemCountsAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/IItemCountsAdapter.kt @@ -1,25 +1,20 @@ package org.mariotaku.twidere.adapter.iface; +import org.mariotaku.twidere.model.ItemCounts + /** * Created by mariotaku on 16/8/19. */ interface IItemCountsAdapter { - val itemCounts: IntArray + val itemCounts: ItemCounts fun getItemCountIndex(position: Int): Int { - var sum: Int = 0 - itemCounts.forEachIndexed { idx, count -> - sum += count - if (position < sum) { - return idx - } - } - return -1 + return itemCounts.getItemCountIndex(position) } fun getItemStartPosition(index: Int): Int { - return itemCounts.slice(0 until index).sum() + return itemCounts.getItemStartPosition(index) } } \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/ILoadMoreSupportAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/ILoadMoreSupportAdapter.kt index 4361c82be..1f49763ff 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/ILoadMoreSupportAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/iface/ILoadMoreSupportAdapter.kt @@ -26,8 +26,10 @@ import android.support.annotation.IntDef */ interface ILoadMoreSupportAdapter { + @IndicatorPosition var loadMoreIndicatorPosition: Long + @IndicatorPosition var loadMoreSupportedPosition: Long @IntDef(flag = true, value = *longArrayOf(NONE, START, END, BOTH)) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/ListViewExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/ListViewExtensions.kt index 0f28ae382..5ee7ef73c 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/ListViewExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/ListViewExtensions.kt @@ -5,15 +5,11 @@ import android.widget.ListView import org.mariotaku.ktextension.setItemAvailability import org.mariotaku.twidere.R -/** - * Created by mariotaku on 2017/1/1. - */ - - fun ListView.selectAll() { for (i in 0 until count) { setItemChecked(i, true) } + } fun ListView.selectNone() { @@ -35,4 +31,4 @@ fun ListView.updateSelectionItems(menu: Menu) { menu.setItemAvailability(R.id.select_none, checkedCount > 0) menu.setItemAvailability(R.id.select_all, checkedCount < listCount) menu.setItemAvailability(R.id.invert_selection, checkedCount > 0 && checkedCount < listCount) -} \ No newline at end of file +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt index c8d9b5020..d9cb55f38 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/StatusFragment.kt @@ -1687,7 +1687,7 @@ class StatusFragment : BaseFragment(), LoaderCallbacks { - val view = inflater.inflate(R.layout.card_item_load_indicator, parent, + val view = inflater.inflate(R.layout.list_item_load_indicator, parent, false) return LoadIndicatorViewHolder(view) } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserListsOwnershipsFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserListsOwnershipsFragment.kt index e114b351a..31cc03dbc 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserListsOwnershipsFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/UserListsOwnershipsFragment.kt @@ -37,12 +37,17 @@ import org.mariotaku.twidere.util.Utils class UserListsOwnershipsFragment : ParcelableUserListsFragment() { - override fun onCreateUserListsLoader(context: Context, - args: Bundle, fromUser: Boolean): Loader> { + private val screenName: String? + get() = arguments.getString(EXTRA_SCREEN_NAME) + + private val userId: UserKey? + get() = arguments.getParcelable(EXTRA_USER_KEY) + + override fun onCreateUserListsLoader(context: Context, args: Bundle, fromUser: Boolean): Loader> { val accountKey = args.getParcelable(EXTRA_ACCOUNT_KEY) val userKey = args.getParcelable(EXTRA_USER_KEY) val screenName = args.getString(EXTRA_SCREEN_NAME) - return UserListOwnershipsLoader(activity, accountKey, userKey, screenName, data) + return UserListOwnershipsLoader(activity, accountKey, userKey, screenName, nextCursor, data) } override fun onActivityCreated(savedInstanceState: Bundle?) { @@ -50,12 +55,12 @@ class UserListsOwnershipsFragment : ParcelableUserListsFragment() { setHasOptionsMenu(true) } - override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) { - inflater!!.inflate(R.menu.menu_user_lists_owned, menu) + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + inflater.inflate(R.menu.menu_user_lists_owned, menu) } - override fun onOptionsItemSelected(item: MenuItem?): Boolean { - when (item!!.itemId) { + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { R.id.new_user_list -> { val f = CreateUserListDialogFragment() val args = Bundle() @@ -68,22 +73,19 @@ class UserListsOwnershipsFragment : ParcelableUserListsFragment() { return super.onOptionsItemSelected(item) } - override fun onPrepareOptionsMenu(menu: Menu?) { - val item = menu!!.findItem(R.id.new_user_list) + override fun onPrepareOptionsMenu(menu: Menu) { + val item = menu.findItem(R.id.new_user_list) val accountId = accountKey if (accountId == null || item == null) return if (accountId == userId) { MenuUtils.setItemAvailability(menu, R.id.new_user_list, true) } else { - MenuUtils.setItemAvailability(menu, R.id.new_user_list, Utils.isMyAccount(activity, screenName)) + val screenName = this.screenName + MenuUtils.setItemAvailability(menu, R.id.new_user_list, screenName != null && + Utils.isMyAccount(activity, screenName)) } } - private val screenName: String - get() = arguments.getString(EXTRA_SCREEN_NAME) - - private val userId: UserKey - get() = arguments.getParcelable(EXTRA_USER_KEY) override fun onStart() { super.onStart() diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/BaseFiltersImportFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/BaseFiltersImportFragment.kt index 558292ae7..76f1dff1a 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/BaseFiltersImportFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/BaseFiltersImportFragment.kt @@ -336,7 +336,7 @@ abstract class BaseFiltersImportFragment : AbsContentListRecyclerViewFragment { - val view = inflater.inflate(R.layout.card_item_load_indicator, parent, false) + val view = inflater.inflate(R.layout.list_item_load_indicator, parent, false) return LoadIndicatorViewHolder(view) } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/FilteredUsersFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/FilteredUsersFragment.kt index 3a5a5b063..9c701347b 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/FilteredUsersFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/FilteredUsersFragment.kt @@ -19,7 +19,6 @@ import android.view.View import android.widget.TextView import org.mariotaku.kpreferences.KPreferences import org.mariotaku.ktextension.setItemAvailability -import org.mariotaku.sqliteqb.library.Expression import org.mariotaku.twidere.R import org.mariotaku.twidere.TwidereConstants.* import org.mariotaku.twidere.activity.AccountSelectorActivity diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/BaseUserListsLoader.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/BaseUserListsLoader.kt index e52bb67f4..1911a3cfd 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/BaseUserListsLoader.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/BaseUserListsLoader.kt @@ -22,6 +22,7 @@ package org.mariotaku.twidere.loader import android.content.Context import android.support.v4.content.AsyncTaskLoader import android.util.Log +import org.mariotaku.kpreferences.get import org.mariotaku.microblog.library.MicroBlog import org.mariotaku.microblog.library.MicroBlogException import org.mariotaku.microblog.library.twitter.model.CursorSupport @@ -29,13 +30,17 @@ import org.mariotaku.microblog.library.twitter.model.PageableResponseList import org.mariotaku.microblog.library.twitter.model.Paging import org.mariotaku.microblog.library.twitter.model.UserList import org.mariotaku.twidere.TwidereConstants.LOGTAG +import org.mariotaku.twidere.constant.loadItemLimitKey import org.mariotaku.twidere.loader.iface.ICursorSupportLoader import org.mariotaku.twidere.model.ParcelableUserList import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.model.util.ParcelableUserListUtils import org.mariotaku.twidere.util.MicroBlogAPIFactory +import org.mariotaku.twidere.util.SharedPreferencesWrapper import org.mariotaku.twidere.util.collection.NoDuplicatesArrayList +import org.mariotaku.twidere.util.dagger.GeneralComponentHelper import java.util.* +import javax.inject.Inject abstract class BaseUserListsLoader( @@ -44,6 +49,8 @@ abstract class BaseUserListsLoader( override val cursor: Long, data: List? ) : AsyncTaskLoader>(context), ICursorSupportLoader { + @Inject + lateinit var preferences: SharedPreferencesWrapper protected val data = NoDuplicatesArrayList() @@ -51,6 +58,7 @@ abstract class BaseUserListsLoader( override var prevCursor: Long = 0 init { + GeneralComponentHelper.build(context).inject(this) if (data != null) { this.data.addAll(data) } @@ -64,6 +72,7 @@ abstract class BaseUserListsLoader( var listLoaded: List? = null try { val paging = Paging() + paging.count(preferences[loadItemLimitKey].coerceIn(0, 100)) if (cursor > 0) { paging.cursor(cursor) } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserListOwnershipsLoader.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserListOwnershipsLoader.kt index df595b988..9fa2f92dc 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserListOwnershipsLoader.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/loader/UserListOwnershipsLoader.kt @@ -32,8 +32,9 @@ class UserListOwnershipsLoader( accountKey: UserKey, private val userKey: UserKey?, private val screenName: String?, + nextCursor: Long, data: List? -) : BaseUserListsLoader(context, accountKey, 0, data) { +) : BaseUserListsLoader(context, accountKey, nextCursor, data) { @Throws(MicroBlogException::class) override fun getUserLists(twitter: MicroBlog, paging: Paging): List { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/ItemCounts.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/ItemCounts.kt new file mode 100644 index 000000000..581caeb9f --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/ItemCounts.kt @@ -0,0 +1,37 @@ +package org.mariotaku.twidere.model + +/** + * Created by mariotaku on 11/22/16. + */ + +class ItemCounts(counts: Int) { + private val data: IntArray = IntArray(counts) + + fun getItemCountIndex(itemPosition: Int): Int { + var sum = 0 + for (i in data.indices) { + sum += data[i] + if (itemPosition < sum) { + return i + } + } + return -1 + } + + fun getItemStartPosition(countIndex: Int): Int { + var sum = 0 + for (i in 0..countIndex - 1) { + sum += data[i] + } + return sum + } + + val itemCount: Int get() = data.sum() + + val size: Int get() = data.size + + operator fun set(countIndex: Int, value: Int) { + data[countIndex] = value + } + +} \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/ContentScrollHandler.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/ContentScrollHandler.kt index 537da12a4..046d9b805 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/ContentScrollHandler.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/ContentScrollHandler.kt @@ -74,10 +74,10 @@ open class ContentScrollHandler( if (!contentListSupport.refreshing && adapter.loadMoreSupportedPosition != ILoadMoreSupportAdapter.NONE && adapter.loadMoreIndicatorPosition == ILoadMoreSupportAdapter.NONE) { var position: Long = 0 - if (contentListSupport.reachingEnd && scrollDirection >= 0) { + if (contentListSupport.reachingEnd && scrollDirection > 0) { position = position or ILoadMoreSupportAdapter.END } - if (contentListSupport.reachingStart && scrollDirection <= 0) { + if (contentListSupport.reachingStart && scrollDirection < 0) { position = position or ILoadMoreSupportAdapter.START } resetScrollDirection() diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/GeneralComponent.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/GeneralComponent.kt index 77d2fe831..68714fd89 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/GeneralComponent.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/GeneralComponent.kt @@ -28,10 +28,7 @@ import org.mariotaku.twidere.adapter.* import org.mariotaku.twidere.app.TwidereApplication import org.mariotaku.twidere.fragment.* import org.mariotaku.twidere.fragment.filter.FilteredUsersFragment -import org.mariotaku.twidere.loader.CacheUserSearchLoader -import org.mariotaku.twidere.loader.MicroBlogAPIStatusesLoader -import org.mariotaku.twidere.loader.ParcelableStatusLoader -import org.mariotaku.twidere.loader.ParcelableUserLoader +import org.mariotaku.twidere.loader.* import org.mariotaku.twidere.preference.AccountsListPreference import org.mariotaku.twidere.preference.KeyboardShortcutPreference import org.mariotaku.twidere.preference.PremiumEntryPreference @@ -153,4 +150,6 @@ interface GeneralComponent { fun inject(preference: PremiumEntryPreference) fun inject(loader: CacheUserSearchLoader) + + fun inject(loader: BaseUserListsLoader) } diff --git a/twidere/src/main/res/layout/card_item_load_indicator.xml b/twidere/src/main/res/layout/list_item_load_indicator.xml similarity index 100% rename from twidere/src/main/res/layout/card_item_load_indicator.xml rename to twidere/src/main/res/layout/list_item_load_indicator.xml