From 9e30071cc41a3684b67f05daa8e19c0e1c5a7fbd Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Fri, 3 Feb 2017 01:09:28 +0800 Subject: [PATCH] rewrote trends fragment --- .../twidere/model/ParcelableTrend.java | 5 +++ .../twidere/model/tab/extra/TabExtras.java | 3 ++ .../twidere/provider/TwidereDataStore.java | 6 ++- .../java/org/mariotaku/twidere/Constants.java | 2 +- .../tab/impl/TrendsTabConfiguration.java | 6 +-- .../fragment/TrendsSuggestionsFragment.kt | 19 ++++++-- .../mariotaku/twidere/task/GetTrendsTask.kt | 43 +++++++++++-------- 7 files changed, 54 insertions(+), 30 deletions(-) diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableTrend.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableTrend.java index 7d95842b0..b9332411b 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableTrend.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableTrend.java @@ -40,6 +40,10 @@ public class ParcelableTrend implements Parcelable { @JsonField(name = "timestamp") @CursorField(CachedTrends.TIMESTAMP) public long timestamp; + @ParcelableThisPlease + @JsonField(name = "trend_order") + @CursorField(CachedTrends.TREND_ORDER) + public int trend_order; @JsonField(name = "name") @CursorField(value = CachedTrends.NAME) public String name; @@ -51,6 +55,7 @@ public class ParcelableTrend implements Parcelable { ", account_key=" + account_key + ", woe_id=" + woe_id + ", timestamp=" + timestamp + + ", trend_order=" + trend_order + ", name='" + name + '\'' + '}'; } diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/tab/extra/TabExtras.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/tab/extra/TabExtras.java index cbcbaa852..ee5d5c711 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/tab/extra/TabExtras.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/tab/extra/TabExtras.java @@ -33,6 +33,9 @@ public abstract class TabExtras implements Parcelable { case CustomTabType.HOME_TIMELINE: { return LoganSquare.parse(json, HomeTabExtras.class); } + case CustomTabType.TRENDS_SUGGESTIONS: { + return LoganSquare.parse(json, TrendsTabExtras.class); + } } return null; } diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java index 8e32c7e00..1a189d153 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java @@ -30,6 +30,7 @@ import org.mariotaku.twidere.model.FiltersSubscriptionTableInfo; import org.mariotaku.twidere.model.ParcelableActivityTableInfo; import org.mariotaku.twidere.model.ParcelableDirectMessageTableInfo; import org.mariotaku.twidere.model.ParcelableStatusTableInfo; +import org.mariotaku.twidere.model.ParcelableTrendTableInfo; import org.mariotaku.twidere.model.ParcelableUserTableInfo; @SuppressWarnings("unused") @@ -190,9 +191,10 @@ public interface TwidereDataStore { String TIMESTAMP = "timestamp"; String WOEID = "woeid"; + String TREND_ORDER = "trend_order"; - String[] COLUMNS = {_ID, ACCOUNT_KEY, WOEID, NAME, TIMESTAMP}; - String[] TYPES = {TYPE_PRIMARY_KEY, TYPE_TEXT, TYPE_INT, TYPE_TEXT, TYPE_INT}; + String[] COLUMNS = ParcelableTrendTableInfo.COLUMNS; + String[] TYPES = ParcelableTrendTableInfo.TYPES; interface Local extends CachedTrends { String TABLE_NAME = "local_trends"; diff --git a/twidere/src/main/java/org/mariotaku/twidere/Constants.java b/twidere/src/main/java/org/mariotaku/twidere/Constants.java index 87b59f09c..5bf7adcfe 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/Constants.java +++ b/twidere/src/main/java/org/mariotaku/twidere/Constants.java @@ -34,7 +34,7 @@ import static org.mariotaku.twidere.annotation.PreferenceType.STRING; public interface Constants extends TwidereConstants { String DATABASES_NAME = "twidere.sqlite"; - int DATABASES_VERSION = 163; + int DATABASES_VERSION = 164; int EXTRA_FEATURES_NOTICE_VERSION = 0; diff --git a/twidere/src/main/java/org/mariotaku/twidere/model/tab/impl/TrendsTabConfiguration.java b/twidere/src/main/java/org/mariotaku/twidere/model/tab/impl/TrendsTabConfiguration.java index c2628b6b2..55f2840f3 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/model/tab/impl/TrendsTabConfiguration.java +++ b/twidere/src/main/java/org/mariotaku/twidere/model/tab/impl/TrendsTabConfiguration.java @@ -15,7 +15,6 @@ import org.mariotaku.twidere.model.tab.conf.TrendsLocationExtraConfiguration; import org.mariotaku.twidere.model.tab.extra.TrendsTabExtras; import static org.mariotaku.twidere.constant.IntentConstants.EXTRA_PLACE; -import static org.mariotaku.twidere.constant.IntentConstants.EXTRA_WOEID; /** * Created by mariotaku on 2016/11/27. @@ -44,7 +43,7 @@ public class TrendsTabConfiguration extends TabConfiguration { @Override public ExtraConfiguration[] getExtraConfigurations(Context context) { return new ExtraConfiguration[]{ - new TrendsLocationExtraConfiguration(EXTRA_WOEID).title(R.string.trends_location).mutable(true), + new TrendsLocationExtraConfiguration(EXTRA_PLACE).title(R.string.trends_location).mutable(true), }; } @@ -60,8 +59,7 @@ public class TrendsTabConfiguration extends TabConfiguration { extras.setWoeId(place.getWoeId()); extras.setPlaceName(place.getName()); } else { - extras.setWoeId(0); - extras.setPlaceName(null); + return false; } break; } 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 b15323479..4f655d3fc 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/TrendsSuggestionsFragment.kt @@ -31,10 +31,12 @@ 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.twidere.R import org.mariotaku.twidere.adapter.TrendsAdapter -import org.mariotaku.twidere.constant.IntentConstants.EXTRA_WOEID +import org.mariotaku.twidere.constant.IntentConstants.EXTRA_EXTRAS 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 @@ -44,7 +46,12 @@ class TrendsSuggestionsFragment : AbsContentListViewFragment(), L private val accountKey: UserKey? get() = Utils.getAccountKeys(context, arguments)?.firstOrNull() ?: Utils.getDefaultAccountKey(context) - private val woeId: Int get() = arguments.getInt(EXTRA_WOEID, 1) + private val tabExtras: TrendsTabExtras? get() = arguments.getParcelable(EXTRA_EXTRAS) + + private val woeId: Int get() { + val id = tabExtras?.woeId ?: return 1 + return if (id > 0) id else 1 + } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) @@ -69,7 +76,7 @@ class TrendsSuggestionsFragment : AbsContentListViewFragment(), L 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.NAME) + return CursorLoader(activity, uri, CachedTrends.COLUMNS, where, whereArgs, CachedTrends.TREND_ORDER) } override fun onItemClick(view: AdapterView<*>, child: View, position: Int, id: Long) { @@ -91,7 +98,11 @@ class TrendsSuggestionsFragment : AbsContentListViewFragment(), L override fun onLoadFinished(loader: Loader, cursor: Cursor) { adapter.swapCursor(cursor) - showContent() + if (adapter.isEmpty) { + showEmpty(R.drawable.ic_info_refresh, getString(R.string.swipe_down_to_refresh)) + } else { + showContent() + } } override fun onRefresh() { 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 1ad1b455a..5009968eb 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetTrendsTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetTrendsTask.kt @@ -4,16 +4,18 @@ import android.content.ContentResolver import android.content.ContentValues import android.content.Context import android.net.Uri +import android.support.v4.util.ArraySet import com.squareup.otto.Bus import org.mariotaku.abstask.library.AbstractTask import org.mariotaku.microblog.library.MicroBlogException import org.mariotaku.microblog.library.twitter.model.Trends import org.mariotaku.sqliteqb.library.Expression +import org.mariotaku.twidere.model.ParcelableTrend +import org.mariotaku.twidere.model.ParcelableTrendValuesCreator import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.model.message.TrendsRefreshedEvent import org.mariotaku.twidere.provider.TwidereDataStore.CachedHashtags import org.mariotaku.twidere.provider.TwidereDataStore.CachedTrends -import org.mariotaku.twidere.util.ContentValuesCreator import org.mariotaku.twidere.util.MicroBlogAPIFactory import org.mariotaku.twidere.util.content.ContentResolverUtils import org.mariotaku.twidere.util.dagger.GeneralComponentHelper @@ -53,31 +55,34 @@ class GetTrendsTask( } private fun storeTrends(cr: ContentResolver, uri: Uri, trendsList: List) { - val hashtags = ArrayList() - val hashtagValues = ArrayList() + val hashtags = ArraySet() val deleteWhere = Expression.and(Expression.equalsArgs(CachedTrends.ACCOUNT_KEY), Expression.equalsArgs(CachedTrends.WOEID)).sql val deleteWhereArgs = arrayOf(accountKey.toString(), woeId.toString()) cr.delete(CachedTrends.Local.CONTENT_URI, deleteWhere, deleteWhereArgs) - trendsList.forEach { - } - if (trendsList.isNotEmpty()) { - val valuesArray = ContentValuesCreator.createTrends(trendsList) - for (values in valuesArray) { - val hashtag = values.getAsString(CachedTrends.NAME).replaceFirst("#", "") - if (hashtags.contains(hashtag)) { - continue - } + val allTrends = ArrayList() + + trendsList.forEach { trends -> + trends.trends.forEachIndexed { idx, trend -> + val hashtag = trend.name.replaceFirst("#", "") hashtags.add(hashtag) - val hashtagValue = ContentValues() - hashtagValue.put(CachedHashtags.NAME, hashtag) - hashtagValues.add(hashtagValue) + allTrends.add(ParcelableTrend().apply { + this.account_key = accountKey + this.woe_id = woeId + this.name = trend.name + this.timestamp = System.currentTimeMillis() + this.trend_order = idx + }) } - cr.delete(uri, null, null) - ContentResolverUtils.bulkInsert(cr, uri, valuesArray) - ContentResolverUtils.bulkDelete(cr, CachedHashtags.CONTENT_URI, CachedHashtags.NAME, false, hashtags, null) - ContentResolverUtils.bulkInsert(cr, CachedHashtags.CONTENT_URI, hashtagValues.toTypedArray()) } + ContentResolverUtils.bulkInsert(cr, uri, allTrends.map(ParcelableTrendValuesCreator::create)) + ContentResolverUtils.bulkDelete(cr, CachedHashtags.CONTENT_URI, CachedHashtags.NAME, false, + hashtags, null) + ContentResolverUtils.bulkInsert(cr, CachedHashtags.CONTENT_URI, hashtags.map { + val values = ContentValues() + values.put(CachedHashtags.NAME, it) + return@map values + }) } }