Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/activity/UserListSelectorActivity.kt

229 lines
8.2 KiB
Kotlin
Raw Normal View History

/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2014 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.activity
import android.app.Activity
import android.content.Intent
import android.os.Bundle
2020-01-26 08:35:15 +01:00
import androidx.loader.app.LoaderManager
import androidx.loader.content.Loader
import android.view.View
import android.widget.AdapterView.OnItemClickListener
2017-01-31 05:04:36 +01:00
import android.widget.TextView
2020-01-26 08:35:15 +01:00
import androidx.loader.app.hasRunningLoadersSafe
2017-01-31 05:04:36 +01:00
import kotlinx.android.synthetic.main.layout_list_with_empty_view.*
import org.mariotaku.ktextension.Bundle
import org.mariotaku.ktextension.contains
2017-01-31 05:04:36 +01:00
import org.mariotaku.ktextension.set
import org.mariotaku.twidere.R
2017-01-31 05:04:36 +01:00
import org.mariotaku.twidere.TwidereConstants.REQUEST_SELECT_USER
import org.mariotaku.twidere.adapter.SimpleParcelableUserListsAdapter
2017-01-31 14:10:20 +01:00
import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter
import org.mariotaku.twidere.adapter.iface.ILoadMoreSupportAdapter.IndicatorPosition
import org.mariotaku.twidere.constant.IntentConstants.*
2017-04-21 11:23:55 +02:00
import org.mariotaku.twidere.loader.iface.IPaginationLoader
import org.mariotaku.twidere.loader.userlists.UserListOwnershipsLoader
2017-01-31 05:04:36 +01:00
import org.mariotaku.twidere.model.ParcelableUser
import org.mariotaku.twidere.model.ParcelableUserList
import org.mariotaku.twidere.model.UserKey
2017-04-21 11:23:55 +02:00
import org.mariotaku.twidere.model.pagination.Pagination
import org.mariotaku.twidere.util.ContentScrollHandler
import org.mariotaku.twidere.util.ListViewScrollHandler
2017-01-31 05:04:36 +01:00
class UserListSelectorActivity : BaseActivity(),
ContentScrollHandler.ContentListSupport<SimpleParcelableUserListsAdapter>,
2017-01-31 05:04:36 +01:00
LoaderManager.LoaderCallbacks<List<ParcelableUserList>> {
override lateinit var adapter: SimpleParcelableUserListsAdapter
2017-01-31 05:04:36 +01:00
2017-04-21 11:23:55 +02:00
override var refreshing: Boolean
get() {
return LoaderManager.getInstance(this).hasRunningLoadersSafe()
2017-04-21 11:23:55 +02:00
}
set(value) {
}
override val reachingStart: Boolean
get() = listView.firstVisiblePosition <= 0
override val reachingEnd: Boolean
get() = listView.lastVisiblePosition >= listView.count - 1
2017-01-31 05:04:36 +01:00
private val accountKey: UserKey?
get() = intent.getParcelableExtra<UserKey>(EXTRA_ACCOUNT_KEY)
private val showMyLists: Boolean
get() = intent.getBooleanExtra(EXTRA_SHOW_MY_LISTS, false)
private var userKey: UserKey? = null
2017-04-21 11:23:55 +02:00
private var nextPagination: Pagination? = null
2017-01-31 05:04:36 +01:00
private var loaderInitialized: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2017-01-31 05:04:36 +01:00
val accountKey = accountKey ?: run {
finish()
return
}
setContentView(R.layout.activity_user_list_selector)
adapter = SimpleParcelableUserListsAdapter(this, requestManager)
2017-01-31 14:10:20 +01:00
adapter.loadMoreSupportedPosition = ILoadMoreSupportAdapter.END
2017-01-31 05:04:36 +01:00
listView.addFooterView(layoutInflater.inflate(R.layout.simple_list_item_activated_1,
listView, false).apply {
2017-06-19 06:11:28 +02:00
findViewById<TextView>(android.R.id.text1).setText(R.string.action_select_user)
2017-01-31 05:04:36 +01:00
}, SelectUserAction, true)
listView.adapter = adapter
2017-01-31 14:10:20 +01:00
val handler = ListViewScrollHandler(this, listView)
listView.setOnScrollListener(handler)
listView.setOnTouchListener(handler.touchListener)
listView.onItemClickListener = OnItemClickListener { view, _, position, _ ->
2020-06-08 23:09:07 +02:00
when (val item = view.getItemAtPosition(position)) {
2017-01-31 05:04:36 +01:00
is ParcelableUserList -> {
val data = Intent()
data.putExtra(EXTRA_USER_LIST, item)
2017-03-20 14:46:18 +01:00
data.putExtra(EXTRA_EXTRAS, intent.getBundleExtra(EXTRA_EXTRAS))
2017-01-31 05:04:36 +01:00
setResult(Activity.RESULT_OK, data)
finish()
}
is SelectUserAction -> {
selectUser()
}
}
}
2017-01-31 05:04:36 +01:00
val userKey = intent.getParcelableExtra<UserKey>(EXTRA_USER_KEY) ?: if (showMyLists) {
accountKey
} else {
null
}
2017-01-31 05:04:36 +01:00
if (userKey != null) {
loadUserLists(accountKey, userKey)
} else if (savedInstanceState == null) {
selectUser()
}
}
override fun onStart() {
super.onStart()
bus.register(this)
}
override fun onStop() {
bus.unregister(this)
super.onStop()
}
2017-01-31 05:04:36 +01:00
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
2020-05-31 08:02:32 +02:00
super.onActivityResult(requestCode, resultCode, data)
2017-01-31 05:04:36 +01:00
when (requestCode) {
REQUEST_SELECT_USER -> {
if (resultCode == Activity.RESULT_OK && data != null) {
val user = data.getParcelableExtra<ParcelableUser>(EXTRA_USER) ?: return
2017-01-31 05:04:36 +01:00
loadUserLists(accountKey!!, user.key)
}
}
}
2017-01-31 05:04:36 +01:00
}
2020-01-26 08:35:15 +01:00
override fun onCreateLoader(id: Int, args: Bundle?): Loader<List<ParcelableUserList>> {
val accountKey = args?.getParcelable<UserKey>(EXTRA_ACCOUNT_KEY)
val userKey = args?.getParcelable<UserKey>(EXTRA_USER_KEY)
2017-04-21 18:32:03 +02:00
return UserListOwnershipsLoader(this, accountKey, userKey, null, adapter.all).apply {
2020-01-26 08:35:15 +01:00
pagination = args?.getParcelable(EXTRA_PAGINATION)
2017-04-21 18:32:03 +02:00
}
2017-01-31 05:04:36 +01:00
}
2020-01-26 08:35:15 +01:00
override fun onLoaderReset(loader: Loader<List<ParcelableUserList>>) {
2017-01-31 05:04:36 +01:00
adapter.setData(null)
}
2017-01-31 14:10:20 +01:00
2020-01-26 08:35:15 +01:00
override fun onLoadFinished(loader: Loader<List<ParcelableUserList>>, data: List<ParcelableUserList>) {
2017-01-31 14:10:20 +01:00
adapter.loadMoreIndicatorPosition = ILoadMoreSupportAdapter.NONE
adapter.loadMoreSupportedPosition = if (adapter.all != data) {
ILoadMoreSupportAdapter.END
} else {
ILoadMoreSupportAdapter.NONE
}
2017-01-31 05:04:36 +01:00
adapter.setData(data)
2017-01-31 14:10:20 +01:00
refreshing = false
2017-04-21 11:23:55 +02:00
if (loader is IPaginationLoader) {
nextPagination = loader.nextPagination
2017-01-31 14:10:20 +01:00
}
2017-01-31 05:04:36 +01:00
showList()
}
override fun setControlVisible(visible: Boolean) {
}
2017-01-31 14:10:20 +01:00
override fun onLoadMoreContents(@IndicatorPosition position: Long) {
val accountKey = this.accountKey ?: return
val userKey = this.userKey ?: return
if (refreshing || position !in adapter.loadMoreSupportedPosition) {
2017-01-31 14:10:20 +01:00
return
}
adapter.loadMoreIndicatorPosition = position
2017-04-21 11:23:55 +02:00
loadUserLists(accountKey, userKey, nextPagination)
}
2017-04-21 11:23:55 +02:00
private fun loadUserLists(accountKey: UserKey, userKey: UserKey, pagination: Pagination? = null) {
2017-01-31 05:04:36 +01:00
if (userKey != this.userKey) {
adapter.clear()
showProgress()
this.userKey = userKey
}
val args = Bundle {
this[EXTRA_ACCOUNT_KEY] = accountKey
this[EXTRA_USER_KEY] = userKey
2017-04-21 11:23:55 +02:00
this[EXTRA_PAGINATION] = pagination
2017-01-31 05:04:36 +01:00
}
if (!loaderInitialized) {
loaderInitialized = true
LoaderManager.getInstance(this).initLoader(0, args, this)
2017-01-31 05:04:36 +01:00
} else {
LoaderManager.getInstance(this).restartLoader(0, args, this)
}
2017-01-31 05:04:36 +01:00
}
private fun showProgress() {
progressContainer.visibility = View.VISIBLE
listContainer.visibility = View.GONE
}
private fun showList() {
progressContainer.visibility = View.GONE
listContainer.visibility = View.VISIBLE
listView.visibility = View.VISIBLE
emptyView.visibility = View.GONE
}
2017-01-31 05:04:36 +01:00
private fun selectUser() {
val selectUserIntent = Intent(this, UserSelectorActivity::class.java)
selectUserIntent.putExtra(EXTRA_ACCOUNT_KEY, accountKey)
startActivityForResult(selectUserIntent, REQUEST_SELECT_USER)
}
2017-01-31 05:04:36 +01:00
object SelectUserAction
}