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

193 lines
6.9 KiB
Kotlin
Raw Normal View History

2017-01-29 14:34:22 +01:00
/*
* 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
2017-01-29 14:34:22 +01:00
import android.text.TextUtils.isEmpty
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemClickListener
import android.widget.ListView
import kotlinx.android.synthetic.main.activity_user_selector.*
2017-01-30 04:29:09 +01:00
import kotlinx.android.synthetic.main.layout_list_with_empty_view.*
import org.mariotaku.ktextension.Bundle
import org.mariotaku.ktextension.isNotNullOrEmpty
import org.mariotaku.ktextension.set
2017-01-29 14:34:22 +01:00
import org.mariotaku.twidere.R
import org.mariotaku.twidere.adapter.SimpleParcelableUsersAdapter
2017-04-21 11:23:55 +02:00
import org.mariotaku.twidere.app.TwidereApplication
2017-01-29 14:34:22 +01:00
import org.mariotaku.twidere.constant.IntentConstants.*
2017-01-30 04:29:09 +01:00
import org.mariotaku.twidere.loader.CacheUserSearchLoader
2017-01-29 14:34:22 +01:00
import org.mariotaku.twidere.model.ParcelableUser
import org.mariotaku.twidere.model.UserKey
2017-02-26 09:09:10 +01:00
import org.mariotaku.twidere.util.EditTextEnterHandler
2017-01-29 14:34:22 +01:00
import org.mariotaku.twidere.util.ParseUtils
2017-01-30 04:29:09 +01:00
import org.mariotaku.twidere.util.view.SimpleTextWatcher
2017-01-29 14:34:22 +01:00
2017-01-30 04:29:09 +01:00
class UserSelectorActivity : BaseActivity(), OnItemClickListener, LoaderManager.LoaderCallbacks<List<ParcelableUser>> {
2017-01-29 14:34:22 +01:00
2017-01-31 05:04:36 +01:00
private lateinit var adapter: SimpleParcelableUsersAdapter
2017-01-30 04:29:09 +01:00
private val accountKey: UserKey?
get() = intent.getParcelableExtra<UserKey>(EXTRA_ACCOUNT_KEY)
2017-01-29 14:34:22 +01:00
2017-01-31 05:04:36 +01:00
private var loaderInitialized: Boolean = false
2017-01-29 14:34:22 +01:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2017-01-30 04:29:09 +01:00
val accountKey = this.accountKey ?: run {
2017-01-29 14:34:22 +01:00
finish()
return
}
setContentView(R.layout.activity_user_selector)
2017-01-30 04:29:09 +01:00
2017-02-26 09:09:10 +01:00
val enterHandler = EditTextEnterHandler.attach(editScreenName, object : EditTextEnterHandler.EnterListener {
override fun onHitEnter(): Boolean {
val screenName = ParseUtils.parseString(editScreenName.text)
searchUser(accountKey, screenName, false)
return true
}
override fun shouldCallListener(): Boolean {
return true
}
}, true)
enterHandler.addTextChangedListener(object : SimpleTextWatcher {
2017-01-30 04:29:09 +01:00
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
searchUser(accountKey, s.toString(), true)
}
})
2017-02-26 09:09:10 +01:00
2017-01-30 04:29:09 +01:00
screenNameConfirm.setOnClickListener {
val screenName = ParseUtils.parseString(editScreenName.text)
searchUser(accountKey, screenName, false)
2017-01-29 14:34:22 +01:00
}
2017-01-30 04:29:09 +01:00
if (savedInstanceState == null) {
editScreenName.setText(intent.getStringExtra(EXTRA_SCREEN_NAME))
2017-01-29 14:34:22 +01:00
}
adapter = SimpleParcelableUsersAdapter(this, requestManager = requestManager)
2017-01-31 05:04:36 +01:00
listView.adapter = adapter
2017-01-30 04:29:09 +01:00
listView.onItemClickListener = this
2017-01-29 14:34:22 +01:00
2017-01-30 04:29:09 +01:00
showSearchHint()
2017-01-29 14:34:22 +01:00
}
override fun onItemClick(view: AdapterView<*>, child: View, position: Int, id: Long) {
val list = view as ListView
2017-01-31 05:04:36 +01:00
val user = adapter.getItem(position - list.headerViewsCount) ?: return
2017-01-29 14:34:22 +01:00
val data = Intent()
2017-04-21 11:23:55 +02:00
data.setExtrasClassLoader(TwidereApplication::class.java.classLoader)
2017-01-29 14:34:22 +01:00
data.putExtra(EXTRA_USER, user)
2017-03-20 14:46:18 +01:00
data.putExtra(EXTRA_EXTRAS, intent.getBundleExtra(EXTRA_EXTRAS))
2017-01-29 14:34:22 +01:00
setResult(Activity.RESULT_OK, data)
finish()
}
2020-01-26 08:35:15 +01:00
override fun onCreateLoader(id: Int, args: Bundle?): Loader<List<ParcelableUser>> {
val accountKey = args?.getParcelable<UserKey>(EXTRA_ACCOUNT_KEY)!!
2020-05-31 08:44:38 +02:00
val query = args.getString(EXTRA_QUERY).orEmpty()
val fromCache = args.getBoolean(EXTRA_FROM_CACHE)
2017-01-30 04:29:09 +01:00
if (!fromCache) {
showProgress()
}
return CacheUserSearchLoader(this, accountKey, query, !fromCache,
fromCache = true,
fromUser = true
)
2017-01-29 14:34:22 +01:00
}
2017-01-30 04:29:09 +01:00
override fun onLoaderReset(loader: Loader<List<ParcelableUser>>) {
2017-01-31 05:04:36 +01:00
adapter.setData(null, true)
2017-01-29 14:34:22 +01:00
}
2017-01-30 04:29:09 +01:00
override fun onLoadFinished(loader: Loader<List<ParcelableUser>>, data: List<ParcelableUser>?) {
progressContainer.visibility = View.GONE
listContainer.visibility = View.VISIBLE
2017-01-31 05:04:36 +01:00
adapter.setData(data, true)
2017-01-30 04:29:09 +01:00
loader as CacheUserSearchLoader
2020-06-09 02:21:48 +02:00
when {
data.isNotNullOrEmpty() -> {
showList()
}
loader.query.isEmpty() -> {
showSearchHint()
}
else -> {
showNotFound()
}
2017-01-29 14:34:22 +01:00
}
}
2017-01-30 04:29:09 +01:00
private fun searchUser(accountKey: UserKey, query: String, fromCache: Boolean) {
if (isEmpty(query)) {
showSearchHint()
return
}
val args = Bundle {
this[EXTRA_ACCOUNT_KEY] = accountKey
this[EXTRA_QUERY] = query
this[EXTRA_FROM_CACHE] = fromCache
}
if (loaderInitialized) {
supportLoaderManager.initLoader(0, args, this)
loaderInitialized = true
} else {
supportLoaderManager.restartLoader(0, args, this)
2017-01-29 14:34:22 +01:00
}
}
2017-01-30 04:29:09 +01:00
private fun showProgress() {
progressContainer.visibility = View.VISIBLE
listContainer.visibility = View.GONE
2017-01-29 14:34:22 +01:00
}
2017-01-30 04:29:09 +01:00
private fun showSearchHint() {
progressContainer.visibility = View.GONE
listContainer.visibility = View.VISIBLE
emptyView.visibility = View.VISIBLE
listView.visibility = View.GONE
emptyIcon.setImageResource(R.drawable.ic_info_search)
emptyText.text = getText(R.string.search_hint_users)
2017-01-29 14:34:22 +01:00
}
2017-01-30 04:29:09 +01:00
private fun showNotFound() {
progressContainer.visibility = View.GONE
listContainer.visibility = View.VISIBLE
emptyView.visibility = View.VISIBLE
listView.visibility = View.GONE
emptyIcon.setImageResource(R.drawable.ic_info_search)
emptyText.text = getText(R.string.search_hint_users)
2017-01-29 14:34:22 +01:00
}
2017-01-30 04:29:09 +01:00
private fun showList() {
progressContainer.visibility = View.GONE
listContainer.visibility = View.VISIBLE
listView.visibility = View.VISIBLE
emptyView.visibility = View.GONE
}
2017-01-29 14:34:22 +01:00
}