Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt

156 lines
5.6 KiB
Kotlin
Raw Normal View History

2016-06-29 15:47:52 +02: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.fragment
import android.content.Context
2017-03-22 05:39:15 +01:00
import android.content.Intent
2016-06-29 15:47:52 +02:00
import android.database.Cursor
import android.os.Bundle
2020-01-26 08:35:15 +01:00
import androidx.loader.app.LoaderManager.LoaderCallbacks
import androidx.loader.content.CursorLoader
import androidx.loader.content.Loader
2016-06-29 15:47:52 +02:00
import android.view.View
import android.widget.AdapterView
import android.widget.ListView
2017-10-05 05:50:35 +02:00
import com.bumptech.glide.RequestManager
2016-06-29 15:47:52 +02:00
import com.squareup.otto.Subscribe
import kotlinx.android.synthetic.main.fragment_content_listview.*
2017-02-07 18:17:00 +01:00
import org.mariotaku.kpreferences.get
2017-02-06 08:51:26 +01:00
import org.mariotaku.sqliteqb.library.Expression
2017-02-02 18:09:28 +01:00
import org.mariotaku.twidere.R
2017-04-07 06:12:34 +02:00
import org.mariotaku.twidere.TwidereConstants.EXTRA_ACCOUNT_KEY
2017-03-22 05:39:15 +01:00
import org.mariotaku.twidere.activity.QuickSearchBarActivity
2016-06-29 15:47:52 +02:00
import org.mariotaku.twidere.adapter.TrendsAdapter
2017-02-02 18:09:28 +01:00
import org.mariotaku.twidere.constant.IntentConstants.EXTRA_EXTRAS
2017-02-07 18:17:00 +01:00
import org.mariotaku.twidere.constant.localTrendsWoeIdKey
2017-03-22 05:39:15 +01:00
import org.mariotaku.twidere.fragment.iface.IFloatingActionButtonFragment
import org.mariotaku.twidere.fragment.iface.IFloatingActionButtonFragment.ActionInfo
2016-06-29 15:47:52 +02:00
import org.mariotaku.twidere.model.UserKey
2017-02-09 09:00:12 +01:00
import org.mariotaku.twidere.model.event.TrendsRefreshedEvent
2017-02-02 18:09:28 +01:00
import org.mariotaku.twidere.model.tab.extra.TrendsTabExtras
2016-06-29 15:47:52 +02:00
import org.mariotaku.twidere.provider.TwidereDataStore.CachedTrends
import org.mariotaku.twidere.util.IntentUtils.openTweetSearch
2017-02-02 17:27:21 +01:00
import org.mariotaku.twidere.util.Utils
2016-06-29 15:47:52 +02:00
2017-03-22 05:39:15 +01:00
class TrendsSuggestionsFragment : AbsContentListViewFragment<TrendsAdapter>(), LoaderCallbacks<Cursor>,
AdapterView.OnItemClickListener, IFloatingActionButtonFragment {
2016-06-29 15:47:52 +02:00
private val tabExtras: TrendsTabExtras?
2020-01-26 08:35:15 +01:00
get() = arguments?.getParcelable(EXTRA_EXTRAS)
2017-02-02 18:09:28 +01:00
2017-02-07 18:17:00 +01:00
private val accountKey: UserKey? get() {
2020-01-26 08:35:15 +01:00
return context?.let { Utils.getAccountKeys(it, arguments)?.firstOrNull() }
?: context?.let { Utils.getDefaultAccountKey(it) }
2017-02-07 18:17:00 +01:00
}
2017-02-02 18:09:28 +01:00
private val woeId: Int get() {
2017-02-07 18:17:00 +01:00
val id = tabExtras?.woeId ?: 0
return if (id > 0) id else preferences[localTrendsWoeIdKey]
2017-02-02 18:09:28 +01:00
}
2016-06-29 15:47:52 +02:00
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
listView.onItemClickListener = this
loaderManager.initLoader(0, null, this)
showProgress()
}
2017-10-05 05:50:35 +02:00
override fun onCreateAdapter(context: Context, requestManager: RequestManager): TrendsAdapter {
return TrendsAdapter(requireActivity())
2016-06-29 15:47:52 +02:00
}
2016-06-30 07:11:50 +02:00
override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> {
2016-06-29 15:47:52 +02:00
val uri = CachedTrends.Local.CONTENT_URI
2017-02-06 08:51:26 +01:00
val loaderWhere = Expression.and(Expression.equalsArgs(CachedTrends.ACCOUNT_KEY),
Expression.equalsArgs(CachedTrends.WOEID)).sql
2017-08-27 16:28:44 +02:00
val loaderWhereArgs = arrayOf(accountKey?.toString().orEmpty(), woeId.toString())
return CursorLoader(requireActivity(), uri, CachedTrends.COLUMNS, loaderWhere, loaderWhereArgs, CachedTrends.TREND_ORDER)
2016-06-29 15:47:52 +02:00
}
override fun onItemClick(view: AdapterView<*>, child: View, position: Int, id: Long) {
if (multiSelectManager.isActive) return
val trend: String = (if (view is ListView) {
2020-06-08 23:19:10 +02:00
adapter.getItem(position - view.headerViewsCount)
2016-06-29 15:47:52 +02:00
} else {
2020-06-08 23:19:10 +02:00
adapter.getItem(position)
2016-06-29 15:47:52 +02:00
})
?: return
2020-01-26 08:35:15 +01:00
activity?.let { openTweetSearch(it, accountKey, trend) }
2016-06-29 15:47:52 +02:00
}
override fun onLoaderReset(loader: Loader<Cursor>) {
2016-12-15 02:06:50 +01:00
adapter.swapCursor(null)
2016-06-29 15:47:52 +02:00
}
override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor) {
2016-12-15 02:06:50 +01:00
adapter.swapCursor(cursor)
2017-02-02 18:09:28 +01:00
if (adapter.isEmpty) {
showEmpty(R.drawable.ic_info_refresh, getString(R.string.swipe_down_to_refresh))
} else {
showContent()
}
2016-06-29 15:47:52 +02:00
}
override fun onRefresh() {
if (refreshing) return
2017-02-02 17:27:21 +01:00
val accountKey = this.accountKey ?: return
twitterWrapper.getLocalTrendsAsync(accountKey, woeId)
2016-06-29 15:47:52 +02:00
}
2016-08-22 10:11:42 +02:00
override var refreshing: Boolean
2016-06-29 15:47:52 +02:00
get() = false
2016-08-22 10:11:42 +02:00
set(value) {
super.refreshing = value
}
2016-06-29 15:47:52 +02:00
override fun onStart() {
super.onStart()
loaderManager.restartLoader(0, null, this)
bus.register(this)
}
override fun onStop() {
bus.unregister(this)
super.onStop()
}
@Subscribe
fun onTrendsRefreshedEvent(event: TrendsRefreshedEvent) {
refreshing = false
}
2017-03-22 05:39:15 +01:00
override fun getActionInfo(tag: String): ActionInfo? {
when (tag) {
"home" -> {
return ActionInfo(R.drawable.ic_action_search, getString(R.string.action_search))
}
}
return null
}
override fun onActionClick(tag: String): Boolean {
val intent = Intent(activity, QuickSearchBarActivity::class.java)
intent.putExtra(EXTRA_ACCOUNT_KEY, accountKey)
startActivity(intent)
return true
}
2016-06-29 15:47:52 +02:00
}