rewrote trends fragment

This commit is contained in:
Mariotaku Lee 2017-02-03 01:09:28 +08:00
parent 5b6db13f7a
commit 9e30071cc4
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
7 changed files with 54 additions and 30 deletions

View File

@ -40,6 +40,10 @@ public class ParcelableTrend implements Parcelable {
@JsonField(name = "timestamp") @JsonField(name = "timestamp")
@CursorField(CachedTrends.TIMESTAMP) @CursorField(CachedTrends.TIMESTAMP)
public long timestamp; public long timestamp;
@ParcelableThisPlease
@JsonField(name = "trend_order")
@CursorField(CachedTrends.TREND_ORDER)
public int trend_order;
@JsonField(name = "name") @JsonField(name = "name")
@CursorField(value = CachedTrends.NAME) @CursorField(value = CachedTrends.NAME)
public String name; public String name;
@ -51,6 +55,7 @@ public class ParcelableTrend implements Parcelable {
", account_key=" + account_key + ", account_key=" + account_key +
", woe_id=" + woe_id + ", woe_id=" + woe_id +
", timestamp=" + timestamp + ", timestamp=" + timestamp +
", trend_order=" + trend_order +
", name='" + name + '\'' + ", name='" + name + '\'' +
'}'; '}';
} }

View File

@ -33,6 +33,9 @@ public abstract class TabExtras implements Parcelable {
case CustomTabType.HOME_TIMELINE: { case CustomTabType.HOME_TIMELINE: {
return LoganSquare.parse(json, HomeTabExtras.class); return LoganSquare.parse(json, HomeTabExtras.class);
} }
case CustomTabType.TRENDS_SUGGESTIONS: {
return LoganSquare.parse(json, TrendsTabExtras.class);
}
} }
return null; return null;
} }

View File

@ -30,6 +30,7 @@ import org.mariotaku.twidere.model.FiltersSubscriptionTableInfo;
import org.mariotaku.twidere.model.ParcelableActivityTableInfo; import org.mariotaku.twidere.model.ParcelableActivityTableInfo;
import org.mariotaku.twidere.model.ParcelableDirectMessageTableInfo; import org.mariotaku.twidere.model.ParcelableDirectMessageTableInfo;
import org.mariotaku.twidere.model.ParcelableStatusTableInfo; import org.mariotaku.twidere.model.ParcelableStatusTableInfo;
import org.mariotaku.twidere.model.ParcelableTrendTableInfo;
import org.mariotaku.twidere.model.ParcelableUserTableInfo; import org.mariotaku.twidere.model.ParcelableUserTableInfo;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -190,9 +191,10 @@ public interface TwidereDataStore {
String TIMESTAMP = "timestamp"; String TIMESTAMP = "timestamp";
String WOEID = "woeid"; String WOEID = "woeid";
String TREND_ORDER = "trend_order";
String[] COLUMNS = {_ID, ACCOUNT_KEY, WOEID, NAME, TIMESTAMP}; String[] COLUMNS = ParcelableTrendTableInfo.COLUMNS;
String[] TYPES = {TYPE_PRIMARY_KEY, TYPE_TEXT, TYPE_INT, TYPE_TEXT, TYPE_INT}; String[] TYPES = ParcelableTrendTableInfo.TYPES;
interface Local extends CachedTrends { interface Local extends CachedTrends {
String TABLE_NAME = "local_trends"; String TABLE_NAME = "local_trends";

View File

@ -34,7 +34,7 @@ import static org.mariotaku.twidere.annotation.PreferenceType.STRING;
public interface Constants extends TwidereConstants { public interface Constants extends TwidereConstants {
String DATABASES_NAME = "twidere.sqlite"; String DATABASES_NAME = "twidere.sqlite";
int DATABASES_VERSION = 163; int DATABASES_VERSION = 164;
int EXTRA_FEATURES_NOTICE_VERSION = 0; int EXTRA_FEATURES_NOTICE_VERSION = 0;

View File

@ -15,7 +15,6 @@ import org.mariotaku.twidere.model.tab.conf.TrendsLocationExtraConfiguration;
import org.mariotaku.twidere.model.tab.extra.TrendsTabExtras; 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_PLACE;
import static org.mariotaku.twidere.constant.IntentConstants.EXTRA_WOEID;
/** /**
* Created by mariotaku on 2016/11/27. * Created by mariotaku on 2016/11/27.
@ -44,7 +43,7 @@ public class TrendsTabConfiguration extends TabConfiguration {
@Override @Override
public ExtraConfiguration[] getExtraConfigurations(Context context) { public ExtraConfiguration[] getExtraConfigurations(Context context) {
return new ExtraConfiguration[]{ 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.setWoeId(place.getWoeId());
extras.setPlaceName(place.getName()); extras.setPlaceName(place.getName());
} else { } else {
extras.setWoeId(0); return false;
extras.setPlaceName(null);
} }
break; break;
} }

View File

@ -31,10 +31,12 @@ import android.widget.ListView
import com.squareup.otto.Subscribe import com.squareup.otto.Subscribe
import kotlinx.android.synthetic.main.fragment_content_listview.* import kotlinx.android.synthetic.main.fragment_content_listview.*
import org.mariotaku.sqliteqb.library.* import org.mariotaku.sqliteqb.library.*
import org.mariotaku.twidere.R
import org.mariotaku.twidere.adapter.TrendsAdapter 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.UserKey
import org.mariotaku.twidere.model.message.TrendsRefreshedEvent 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.provider.TwidereDataStore.CachedTrends
import org.mariotaku.twidere.util.DataStoreUtils.getTableNameByUri import org.mariotaku.twidere.util.DataStoreUtils.getTableNameByUri
import org.mariotaku.twidere.util.IntentUtils.openTweetSearch import org.mariotaku.twidere.util.IntentUtils.openTweetSearch
@ -44,7 +46,12 @@ class TrendsSuggestionsFragment : AbsContentListViewFragment<TrendsAdapter>(), L
private val accountKey: UserKey? get() = Utils.getAccountKeys(context, arguments)?.firstOrNull() private val accountKey: UserKey? get() = Utils.getAccountKeys(context, arguments)?.firstOrNull()
?: Utils.getDefaultAccountKey(context) ?: 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?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
@ -69,7 +76,7 @@ class TrendsSuggestionsFragment : AbsContentListViewFragment<TrendsAdapter>(), L
Expression.equalsArgs(CachedTrends.WOEID), Expression.equalsArgs(CachedTrends.WOEID),
Expression.equals(Columns.Column(CachedTrends.TIMESTAMP), timestampQuery)).sql Expression.equals(Columns.Column(CachedTrends.TIMESTAMP), timestampQuery)).sql
val whereArgs = arrayOf(accountKey?.toString() ?: "", woeId.toString()) 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) { override fun onItemClick(view: AdapterView<*>, child: View, position: Int, id: Long) {
@ -91,8 +98,12 @@ class TrendsSuggestionsFragment : AbsContentListViewFragment<TrendsAdapter>(), L
override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor) { override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor) {
adapter.swapCursor(cursor) adapter.swapCursor(cursor)
if (adapter.isEmpty) {
showEmpty(R.drawable.ic_info_refresh, getString(R.string.swipe_down_to_refresh))
} else {
showContent() showContent()
} }
}
override fun onRefresh() { override fun onRefresh() {
if (refreshing) return if (refreshing) return

View File

@ -4,16 +4,18 @@ import android.content.ContentResolver
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.support.v4.util.ArraySet
import com.squareup.otto.Bus import com.squareup.otto.Bus
import org.mariotaku.abstask.library.AbstractTask import org.mariotaku.abstask.library.AbstractTask
import org.mariotaku.microblog.library.MicroBlogException import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.microblog.library.twitter.model.Trends import org.mariotaku.microblog.library.twitter.model.Trends
import org.mariotaku.sqliteqb.library.Expression 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.UserKey
import org.mariotaku.twidere.model.message.TrendsRefreshedEvent import org.mariotaku.twidere.model.message.TrendsRefreshedEvent
import org.mariotaku.twidere.provider.TwidereDataStore.CachedHashtags import org.mariotaku.twidere.provider.TwidereDataStore.CachedHashtags
import org.mariotaku.twidere.provider.TwidereDataStore.CachedTrends import org.mariotaku.twidere.provider.TwidereDataStore.CachedTrends
import org.mariotaku.twidere.util.ContentValuesCreator
import org.mariotaku.twidere.util.MicroBlogAPIFactory import org.mariotaku.twidere.util.MicroBlogAPIFactory
import org.mariotaku.twidere.util.content.ContentResolverUtils import org.mariotaku.twidere.util.content.ContentResolverUtils
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper import org.mariotaku.twidere.util.dagger.GeneralComponentHelper
@ -53,31 +55,34 @@ class GetTrendsTask(
} }
private fun storeTrends(cr: ContentResolver, uri: Uri, trendsList: List<Trends>) { private fun storeTrends(cr: ContentResolver, uri: Uri, trendsList: List<Trends>) {
val hashtags = ArrayList<String>() val hashtags = ArraySet<String>()
val hashtagValues = ArrayList<ContentValues>()
val deleteWhere = Expression.and(Expression.equalsArgs(CachedTrends.ACCOUNT_KEY), val deleteWhere = Expression.and(Expression.equalsArgs(CachedTrends.ACCOUNT_KEY),
Expression.equalsArgs(CachedTrends.WOEID)).sql Expression.equalsArgs(CachedTrends.WOEID)).sql
val deleteWhereArgs = arrayOf(accountKey.toString(), woeId.toString()) val deleteWhereArgs = arrayOf(accountKey.toString(), woeId.toString())
cr.delete(CachedTrends.Local.CONTENT_URI, deleteWhere, deleteWhereArgs) cr.delete(CachedTrends.Local.CONTENT_URI, deleteWhere, deleteWhereArgs)
trendsList.forEach {
} val allTrends = ArrayList<ParcelableTrend>()
if (trendsList.isNotEmpty()) {
val valuesArray = ContentValuesCreator.createTrends(trendsList) trendsList.forEach { trends ->
for (values in valuesArray) { trends.trends.forEachIndexed { idx, trend ->
val hashtag = values.getAsString(CachedTrends.NAME).replaceFirst("#", "") val hashtag = trend.name.replaceFirst("#", "")
if (hashtags.contains(hashtag)) {
continue
}
hashtags.add(hashtag) hashtags.add(hashtag)
val hashtagValue = ContentValues() allTrends.add(ParcelableTrend().apply {
hashtagValue.put(CachedHashtags.NAME, hashtag) this.account_key = accountKey
hashtagValues.add(hashtagValue) this.woe_id = woeId
} this.name = trend.name
cr.delete(uri, null, null) this.timestamp = System.currentTimeMillis()
ContentResolverUtils.bulkInsert(cr, uri, valuesArray) this.trend_order = idx
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
})
}
} }