From 682d52adbe596322c4ac31ad343b358af5f2598e Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Mon, 6 Feb 2017 15:51:26 +0800 Subject: [PATCH] fixed trends displaying --- .../fragment/TrendsSuggestionsFragment.kt | 18 ++++------- .../mariotaku/twidere/task/GetTrendsTask.kt | 30 +++++++++---------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt index 4f655d3fc..c9d6075bb 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt @@ -30,7 +30,7 @@ import android.widget.AdapterView import android.widget.ListView import com.squareup.otto.Subscribe import kotlinx.android.synthetic.main.fragment_content_listview.* -import org.mariotaku.sqliteqb.library.* +import org.mariotaku.sqliteqb.library.Expression import org.mariotaku.twidere.R import org.mariotaku.twidere.adapter.TrendsAdapter import org.mariotaku.twidere.constant.IntentConstants.EXTRA_EXTRAS @@ -38,7 +38,6 @@ import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.model.message.TrendsRefreshedEvent import org.mariotaku.twidere.model.tab.extra.TrendsTabExtras import org.mariotaku.twidere.provider.TwidereDataStore.CachedTrends -import org.mariotaku.twidere.util.DataStoreUtils.getTableNameByUri import org.mariotaku.twidere.util.IntentUtils.openTweetSearch import org.mariotaku.twidere.util.Utils @@ -66,17 +65,10 @@ class TrendsSuggestionsFragment : AbsContentListViewFragment(), L override fun onCreateLoader(id: Int, args: Bundle?): Loader { val uri = CachedTrends.Local.CONTENT_URI - val table = getTableNameByUri(uri)!! - val timestampQuery = SQLQueryBuilder.select(Columns.Column(CachedTrends.TIMESTAMP)) - .from(Table(table)) - .orderBy(OrderBy(CachedTrends.TIMESTAMP, false)) - .limit(1) - .build() - val where = Expression.and(Expression.equalsArgs(CachedTrends.ACCOUNT_KEY), - Expression.equalsArgs(CachedTrends.WOEID), - Expression.equals(Columns.Column(CachedTrends.TIMESTAMP), timestampQuery)).sql - val whereArgs = arrayOf(accountKey?.toString() ?: "", woeId.toString()) - return CursorLoader(activity, uri, CachedTrends.COLUMNS, where, whereArgs, CachedTrends.TREND_ORDER) + val loaderWhere = Expression.and(Expression.equalsArgs(CachedTrends.ACCOUNT_KEY), + Expression.equalsArgs(CachedTrends.WOEID)).sql + val loaderWhereArgs = arrayOf(accountKey?.toString() ?: "", woeId.toString()) + return CursorLoader(activity, uri, CachedTrends.COLUMNS, loaderWhere, loaderWhereArgs, CachedTrends.TREND_ORDER) } override fun onItemClick(view: AdapterView<*>, child: View, position: Int, id: Long) { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetTrendsTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetTrendsTask.kt index e4b1fda61..030ca2dce 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetTrendsTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetTrendsTask.kt @@ -49,9 +49,9 @@ class GetTrendsTask( val twitter = details.newMicroBlogInstance(context, cls = MicroBlog::class.java) try { val trends = when { - details.type == AccountType.FANFOU -> listOf(twitter.fanfouTrends) - else -> twitter.getLocationTrends(woeId) - } + details.type == AccountType.FANFOU -> twitter.fanfouTrends + else -> twitter.getLocationTrends(woeId).firstOrNull() + } ?: return storeTrends(context.contentResolver, CachedTrends.Local.CONTENT_URI, trends) } catch (e: MicroBlogException) { DebugLog.w(LOGTAG, tr = e) @@ -62,7 +62,7 @@ class GetTrendsTask( bus.post(TrendsRefreshedEvent()) } - private fun storeTrends(cr: ContentResolver, uri: Uri, trendsList: List) { + private fun storeTrends(cr: ContentResolver, uri: Uri, trends: Trends) { val hashtags = ArraySet() val deleteWhere = Expression.and(Expression.equalsArgs(CachedTrends.ACCOUNT_KEY), Expression.equalsArgs(CachedTrends.WOEID)).sql @@ -71,18 +71,16 @@ class GetTrendsTask( val allTrends = ArrayList() - trendsList.forEach { trends -> - trends.trends.forEachIndexed { idx, trend -> - val hashtag = trend.name.replaceFirst("#", "") - hashtags.add(hashtag) - allTrends.add(ParcelableTrend().apply { - this.account_key = accountKey - this.woe_id = woeId - this.name = trend.name - this.timestamp = System.currentTimeMillis() - this.trend_order = idx - }) - } + trends.trends.forEachIndexed { idx, trend -> + val hashtag = trend.name.replaceFirst("#", "") + hashtags.add(hashtag) + allTrends.add(ParcelableTrend().apply { + this.account_key = accountKey + this.woe_id = woeId + this.name = trend.name + this.timestamp = System.currentTimeMillis() + this.trend_order = idx + }) } ContentResolverUtils.bulkInsert(cr, uri, allTrends.map(ParcelableTrendValuesCreator::create)) ContentResolverUtils.bulkDelete(cr, CachedHashtags.CONTENT_URI, CachedHashtags.NAME, false,