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

227 lines
8.1 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
2017-01-31 05:04:36 +01:00
import android.support.v4.app.LoaderManager
import android.support.v4.app.hasRunningLoadersSafe
2017-01-31 05:04:36 +01:00
import android.support.v4.content.Loader
import android.view.View
import android.widget.AdapterView.OnItemClickListener
2017-01-31 05:04:36 +01:00
import android.widget.TextView
2017-03-01 15:12:25 +01:00
import com.bumptech.glide.Glide
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-01-31 05:04:36 +01:00
import org.mariotaku.twidere.loader.UserListOwnershipsLoader
2017-01-31 14:10:20 +01:00
import org.mariotaku.twidere.loader.iface.ICursorSupportLoader
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
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
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-01-31 14:10:20 +01:00
private var nextCursor: Long = -1
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)
2017-03-02 07:59:19 +01:00
adapter = SimpleParcelableUserListsAdapter(this, Glide.with(this))
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 {
(findViewById(android.R.id.text1) as TextView).setText(R.string.action_select_user)
}, 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)
2017-01-31 05:04:36 +01:00
listView.onItemClickListener = OnItemClickListener { view, child, position, id ->
val item = view.getItemAtPosition(position)
when (item) {
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?) {
when (requestCode) {
REQUEST_SELECT_USER -> {
if (resultCode == Activity.RESULT_OK && data != null) {
val user = data.getParcelableExtra<ParcelableUser>(EXTRA_USER)
loadUserLists(accountKey!!, user.key)
}
}
}
2017-01-31 05:04:36 +01:00
}
2017-01-31 05:04:36 +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-01-31 14:10:20 +01:00
val nextCursor = args.getLong(EXTRA_NEXT_CURSOR)
return UserListOwnershipsLoader(this, accountKey, userKey, null, nextCursor, adapter.all)
2017-01-31 05:04:36 +01:00
}
2017-01-31 05:04:36 +01:00
override fun onLoaderReset(loader: Loader<List<ParcelableUserList>>?) {
adapter.setData(null)
}
2017-01-31 14:10:20 +01:00
2017-01-31 05:04:36 +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
if (loader is ICursorSupportLoader) {
nextCursor = loader.nextCursor
}
2017-01-31 05:04:36 +01:00
showList()
}
override fun setControlVisible(visible: Boolean) {
}
override var refreshing: Boolean
get() {
return supportLoaderManager.hasRunningLoadersSafe()
}
set(value) {
}
override val reachingStart: Boolean
2017-01-31 14:10:20 +01:00
get() = listView.firstVisiblePosition <= 0
override val reachingEnd: Boolean
2017-01-31 14:10:20 +01:00
get() = listView.lastVisiblePosition >= listView.count - 1
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
loadUserLists(accountKey, userKey, nextCursor)
}
2017-01-31 14:10:20 +01:00
private fun loadUserLists(accountKey: UserKey, userKey: UserKey, nextCursor: Long = -1) {
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-01-31 14:10:20 +01:00
this[EXTRA_NEXT_CURSOR] = nextCursor
2017-01-31 05:04:36 +01:00
}
if (!loaderInitialized) {
loaderInitialized = true
supportLoaderManager.initLoader(0, args, this)
} else {
supportLoaderManager.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
}