fixed trends displaying

This commit is contained in:
Mariotaku Lee 2017-02-06 15:51:26 +08:00
parent ab5562d985
commit 682d52adbe
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
2 changed files with 19 additions and 29 deletions

View File

@ -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<TrendsAdapter>(), L
override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> {
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) {

View File

@ -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<Trends>) {
private fun storeTrends(cr: ContentResolver, uri: Uri, trends: Trends) {
val hashtags = ArraySet<String>()
val deleteWhere = Expression.and(Expression.equalsArgs(CachedTrends.ACCOUNT_KEY),
Expression.equalsArgs(CachedTrends.WOEID)).sql
@ -71,18 +71,16 @@ class GetTrendsTask(
val allTrends = ArrayList<ParcelableTrend>()
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,