2016-06-29 15:47:52 +02:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.mariotaku.twidere.fragment
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.support.v4.app.LoaderManager.LoaderCallbacks
|
2016-11-26 09:13:00 +01:00
|
|
|
import android.support.v4.app.hasRunningLoadersSafe
|
2016-06-29 15:47:52 +02:00
|
|
|
import android.support.v4.content.Loader
|
|
|
|
import android.support.v7.widget.RecyclerView
|
|
|
|
import android.view.KeyEvent
|
2017-09-03 15:23:45 +02:00
|
|
|
import com.bumptech.glide.RequestManager
|
2016-06-29 15:47:52 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_content_recyclerview.*
|
|
|
|
import org.mariotaku.twidere.adapter.ParcelableGroupsAdapter
|
|
|
|
import org.mariotaku.twidere.adapter.iface.IGroupsAdapter.GroupAdapterListener
|
|
|
|
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.iface.IExtendedLoader
|
2017-04-21 11:23:55 +02:00
|
|
|
import org.mariotaku.twidere.loader.iface.IPaginationLoader
|
2016-06-29 15:47:52 +02:00
|
|
|
import org.mariotaku.twidere.model.ParcelableGroup
|
|
|
|
import org.mariotaku.twidere.model.UserKey
|
2017-04-21 11:23:55 +02:00
|
|
|
import org.mariotaku.twidere.model.pagination.Pagination
|
2016-06-29 15:47:52 +02:00
|
|
|
import org.mariotaku.twidere.util.IntentUtils
|
|
|
|
import org.mariotaku.twidere.util.KeyboardShortcutsHandler
|
|
|
|
import org.mariotaku.twidere.util.KeyboardShortcutsHandler.KeyboardShortcutCallback
|
|
|
|
import org.mariotaku.twidere.util.RecyclerViewNavigationHelper
|
|
|
|
import org.mariotaku.twidere.view.holder.GroupViewHolder
|
|
|
|
|
|
|
|
abstract class ParcelableGroupsFragment : AbsContentListRecyclerViewFragment<ParcelableGroupsAdapter>(),
|
|
|
|
LoaderCallbacks<List<ParcelableGroup>?>, GroupAdapterListener, KeyboardShortcutCallback {
|
|
|
|
|
2017-01-30 14:41:34 +01:00
|
|
|
private lateinit var navigationHelper: RecyclerViewNavigationHelper
|
2017-04-21 11:23:55 +02:00
|
|
|
|
|
|
|
protected var nextPagination: Pagination? = null
|
|
|
|
protected var prevPagination: Pagination? = null
|
|
|
|
|
|
|
|
protected val data: List<ParcelableGroup>?
|
|
|
|
get() = adapter.getData()
|
2016-06-29 15:47:52 +02:00
|
|
|
|
2016-07-03 08:42:28 +02:00
|
|
|
override var refreshing: Boolean
|
2016-06-29 15:47:52 +02:00
|
|
|
get() {
|
|
|
|
if (context == null || isDetached) return false
|
2016-11-26 09:13:00 +01:00
|
|
|
return loaderManager.hasRunningLoadersSafe()
|
2016-06-29 15:47:52 +02:00
|
|
|
}
|
2016-07-03 08:42:28 +02:00
|
|
|
set(value) {
|
|
|
|
super.refreshing = value
|
|
|
|
}
|
2016-06-29 15:47:52 +02:00
|
|
|
|
2017-09-03 15:23:45 +02:00
|
|
|
override fun onCreateAdapter(context: Context, requestManager: RequestManager): ParcelableGroupsAdapter {
|
|
|
|
return ParcelableGroupsAdapter(context, this.requestManager)
|
2016-06-29 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected val accountKey: UserKey?
|
2017-04-12 14:58:08 +02:00
|
|
|
get() = arguments.getParcelable<UserKey?>(EXTRA_ACCOUNT_KEY)
|
2016-06-29 15:47:52 +02:00
|
|
|
|
|
|
|
protected fun hasMoreData(data: List<ParcelableGroup>?): Boolean {
|
|
|
|
return data == null || !data.isEmpty()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoadFinished(loader: Loader<List<ParcelableGroup>?>, data: List<ParcelableGroup>?) {
|
2016-12-08 15:43:18 +01:00
|
|
|
adapter.setData(data)
|
2016-08-13 16:04:31 +02:00
|
|
|
if (loader !is IExtendedLoader || loader.fromUser) {
|
2016-06-29 15:47:52 +02:00
|
|
|
adapter.loadMoreSupportedPosition = if (hasMoreData(data)) ILoadMoreSupportAdapter.END else ILoadMoreSupportAdapter.NONE
|
2016-07-03 08:42:28 +02:00
|
|
|
refreshEnabled = true
|
2016-06-29 15:47:52 +02:00
|
|
|
}
|
|
|
|
if (loader is IExtendedLoader) {
|
2016-08-13 16:04:31 +02:00
|
|
|
loader.fromUser = false
|
2016-06-29 15:47:52 +02:00
|
|
|
}
|
2017-04-21 11:23:55 +02:00
|
|
|
if (loader is IPaginationLoader) {
|
|
|
|
nextPagination = loader.nextPagination
|
|
|
|
prevPagination = loader.prevPagination
|
|
|
|
}
|
2016-06-29 15:47:52 +02:00
|
|
|
showContent()
|
2016-07-03 08:42:28 +02:00
|
|
|
refreshEnabled = true
|
2016-06-29 15:47:52 +02:00
|
|
|
refreshing = false
|
|
|
|
setLoadMoreIndicatorPosition(ILoadMoreSupportAdapter.NONE)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoadMoreContents(@IndicatorPosition position: Long) {
|
|
|
|
// Only supports load from end, skip START flag
|
2017-03-02 09:44:48 +01:00
|
|
|
if (position and ILoadMoreSupportAdapter.START != 0L) return
|
2016-12-06 06:15:22 +01:00
|
|
|
super.onLoadMoreContents(position)
|
2016-06-29 15:47:52 +02:00
|
|
|
if (position == 0L) return
|
|
|
|
val loaderArgs = Bundle(arguments)
|
|
|
|
loaderArgs.putBoolean(EXTRA_FROM_USER, true)
|
2017-04-21 11:23:55 +02:00
|
|
|
loaderArgs.putParcelable(EXTRA_PAGINATION, nextPagination)
|
2016-06-29 15:47:52 +02:00
|
|
|
loaderManager.restartLoader(0, loaderArgs, this)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun handleKeyboardShortcutSingle(handler: KeyboardShortcutsHandler, keyCode: Int, event: KeyEvent, metaState: Int): Boolean {
|
2017-01-30 14:41:34 +01:00
|
|
|
return navigationHelper.handleKeyboardShortcutSingle(handler, keyCode, event, metaState)
|
2016-06-29 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun handleKeyboardShortcutRepeat(handler: KeyboardShortcutsHandler, keyCode: Int, repeatCount: Int, event: KeyEvent, metaState: Int): Boolean {
|
2017-01-30 14:41:34 +01:00
|
|
|
return navigationHelper.handleKeyboardShortcutRepeat(handler, keyCode, repeatCount, event, metaState)
|
2016-06-29 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun isKeyboardShortcutHandled(handler: KeyboardShortcutsHandler, keyCode: Int, event: KeyEvent, metaState: Int): Boolean {
|
2017-01-30 14:41:34 +01:00
|
|
|
return navigationHelper.isKeyboardShortcutHandled(handler, keyCode, event, metaState)
|
2016-06-29 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
|
|
|
super.onActivityCreated(savedInstanceState)
|
|
|
|
val layoutManager = layoutManager
|
2016-12-08 15:43:18 +01:00
|
|
|
adapter.groupAdapterListener = this
|
2016-06-29 15:47:52 +02:00
|
|
|
|
2017-01-30 14:41:34 +01:00
|
|
|
navigationHelper = RecyclerViewNavigationHelper(recyclerView, layoutManager, adapter,
|
2016-06-29 15:47:52 +02:00
|
|
|
this)
|
|
|
|
val loaderArgs = Bundle(arguments)
|
|
|
|
loaderArgs.putBoolean(EXTRA_FROM_USER, true)
|
|
|
|
loaderManager.initLoader(0, loaderArgs, this)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateLoader(id: Int, args: Bundle): Loader<List<ParcelableGroup>?> {
|
|
|
|
val fromUser = args.getBoolean(EXTRA_FROM_USER)
|
|
|
|
args.remove(EXTRA_FROM_USER)
|
|
|
|
return onCreateUserListsLoader(activity, args, fromUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoaderReset(loader: Loader<List<ParcelableGroup>?>) {
|
|
|
|
if (loader is IExtendedLoader) {
|
2016-08-13 16:04:31 +02:00
|
|
|
loader.fromUser = false
|
2016-06-29 15:47:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onGroupClick(holder: GroupViewHolder, position: Int) {
|
2016-12-08 15:43:18 +01:00
|
|
|
IntentUtils.openGroupDetails(context, adapter.getGroup(position)!!)
|
2016-06-29 15:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onGroupLongClick(holder: GroupViewHolder, position: Int): Boolean {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract fun onCreateUserListsLoader(context: Context, args: Bundle, fromUser: Boolean): Loader<List<ParcelableGroup>?>
|
|
|
|
}
|