From 5ccdb8fd8dffea7842590fc601e1b697407c41b8 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Sun, 10 May 2015 02:35:16 +0800 Subject: [PATCH] fixed refreshing trends crash --- .../api/twitter/api/TrendsResources.java | 101 ++---------------- .../api/twitter/model/GeoLocation.java | 20 +++- .../twidere/api/twitter/model/Trends.java | 8 +- .../api/twitter/model/impl/TrendsImpl.java | 14 +++ .../twidere/util/ContentValuesCreator.java | 4 +- .../twidere/util/AsyncTwitterWrapper.java | 32 +++--- 6 files changed, 65 insertions(+), 114 deletions(-) diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/api/TrendsResources.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/api/TrendsResources.java index b80e44041..9a8effd7c 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/api/TrendsResources.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/api/TrendsResources.java @@ -20,6 +20,7 @@ package org.mariotaku.twidere.api.twitter.api; import org.mariotaku.simplerestapi.method.GET; +import org.mariotaku.simplerestapi.param.Query; import org.mariotaku.twidere.api.twitter.TwitterException; import org.mariotaku.twidere.api.twitter.model.GeoLocation; import org.mariotaku.twidere.api.twitter.model.Location; @@ -32,98 +33,18 @@ import org.mariotaku.twidere.api.twitter.model.Trends; @SuppressWarnings("RedundantThrows") public interface TrendsResources { - @GET("/trends/available.json") - ResponseList getAvailableTrends() throws TwitterException; + @GET("/trends/available.json") + ResponseList getAvailableTrends() throws TwitterException; - /** - * Returns the sorted locations that Twitter has trending topic information - * for. The response is an array of "locations" that encode the - * location's WOEID (a Yahoo! Where On Earth - * ID) and some other human-readable information such as a canonical - * name and country the location belongs in.
- * This method calls http://api.twitter.com/1.1/trends/available.json - * - * @param location the available trend locations will be sorted by distance - * to the lat and long passed in. The sort is nearest to - * furthest. - * @return the locations - * @throws TwitterException when Twitter service or network is - * unavailable - * @see GET - * trends/available | Twitter Developers - * @since Twitter4J 2.1.1 - */ - ResponseList getAvailableTrends(GeoLocation location) throws TwitterException; + @GET("/trends/available.json") + ResponseList getAvailableTrends(@Query GeoLocation location) throws TwitterException; - /** - * Returns the locations that Twitter has trending topic information for, - * closest to a specified location.
- * The response is an array of "locations" that encode the location's WOEID - * and some other human-readable information such as a canonical name and - * country the location belongs in.
- * A WOEID is a Yahoo! - * Where On Earth ID.
- * This method calls http://api.twitter.com/1.1/trends/closest.json - * - * @param location the available trend locations will be sorted by distance - * to the lat and long passed in. The sort is nearest to - * furthest. - * @return the locations - * @throws TwitterException when Twitter service or network is - * unavailable - * @see GET - * trends/closest | Twitter Developers - * @since Twitter4J 3.0.2 - */ - ResponseList getClosestTrends(GeoLocation location) throws TwitterException; + @GET("/trends/closest.json") + ResponseList getClosestTrends(@Query GeoLocation location) throws TwitterException; - /** - * Returns the top 10 trending topics for a specific location Twitter has - * trending topic information for. The response is an array of "trend" - * objects that encode the name of the trending topic, the query parameter - * that can be used to search for the topic on Search, and the direct URL - * that can be issued against Search. This information is cached for five - * minutes, and therefore users are discouraged from querying these - * endpoints faster than once every five minutes. Global trends information - * is also available from this API by using a WOEID of 1.
- * This method calls http://api.twitter.com/1.1/trends/:woeid.json - * - * @param woeid The WOEID of the location to be querying for - * @return trends - * @throws TwitterException when Twitter service or network is - * unavailable - * @see GET - * trends/:woeid | Twitter Developers - * @since Twitter4J 2.1.1 - */ - Trends getLocationTrends(int woeid) throws TwitterException; + @GET("/trends/place.json") + ResponseList getLocationTrends(@Query("id") int woeid) throws TwitterException; - /** - * Returns the top 10 trending topics for a specific WOEID, if trending - * information is available for it.
- * The response is an array of "trend" objects that encode the name of the - * trending topic, the query parameter that can be used to search for the - * topic on Twitter Search, and the - * Twitter Search URL.
- * This information is cached for 5 minutes. Requesting more frequently than - * that will not return any more data, and will count against your rate - * limit usage.
- *
- * This method calls http://api.twitter.com/1.1/trends/place.json - * - * @param woeid The - * Yahoo! Where On Earth ID of the location to return - * trending information for. Global information is available by - * using 1 as the WOEID. - * @return trends - * @throws TwitterException when Twitter service or network is - * unavailable - * @see GET - * trends/place | Twitter Developers - * @since Twitter4J 3.0.2 - */ - Trends getPlaceTrends(int woeid) throws TwitterException; + @GET("/trends/place.json") + Trends getPlaceTrends(@Query("id") int woeid) throws TwitterException; } diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/GeoLocation.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/GeoLocation.java index 399f7f8e6..f4ee75ee4 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/GeoLocation.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/GeoLocation.java @@ -24,6 +24,7 @@ import com.bluelinelabs.logansquare.typeconverters.TypeConverter; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; +import org.mariotaku.simplerestapi.http.ValueMap; import org.mariotaku.twidere.api.twitter.model.impl.GeoPoint; import java.io.IOException; @@ -33,7 +34,7 @@ import java.io.IOException; * * @author Yusuke Yamamoto - yusuke at mac.com */ -public class GeoLocation { +public class GeoLocation implements ValueMap { public static final TypeConverter CONVERTER = new TypeConverter() { @Override @@ -114,4 +115,21 @@ public class GeoLocation { public String toString() { return "GeoLocation{" + "latitude=" + latitude + ", longitude=" + longitude + '}'; } + + @Override + public boolean has(String key) { + return "lat".equals(key) || "long".equals(key); + } + + @Override + public Object get(String key) { + if ("lat".equals(key)) return latitude; + if ("long".equals(key)) return longitude; + return null; + } + + @Override + public String[] keys() { + return new String[]{"lat", "long"}; + } } diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/Trends.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/Trends.java index 12651daa3..4f40d2297 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/Trends.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/Trends.java @@ -30,10 +30,12 @@ import java.util.Date; */ public interface Trends extends TwitterResponse, Comparable, Serializable { - Date getAsOf(); + Date getAsOf(); - Location[] getLocations(); + Date getCreatedAt(); - Trend[] getTrends(); + Location[] getLocations(); + + Trend[] getTrends(); } diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/impl/TrendsImpl.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/impl/TrendsImpl.java index b198c5f2c..55e76b966 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/impl/TrendsImpl.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/api/twitter/model/impl/TrendsImpl.java @@ -21,13 +21,20 @@ package org.mariotaku.twidere.api.twitter.model.impl; import android.support.annotation.NonNull; +import com.bluelinelabs.logansquare.JsonMapper; import com.bluelinelabs.logansquare.annotation.JsonField; import com.bluelinelabs.logansquare.annotation.JsonObject; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; import org.mariotaku.twidere.api.twitter.model.Trends; import org.mariotaku.twidere.api.twitter.util.TwitterTrendsDateConverter; +import java.io.IOException; +import java.util.ArrayList; import java.util.Date; +import java.util.List; /** * Created by mariotaku on 15/5/10. @@ -37,6 +44,8 @@ public class TrendsImpl extends TwitterResponseImpl implements Trends { @JsonField(name = "as_of", typeConverter = TwitterTrendsDateConverter.class) Date asOf; + @JsonField(name = "created_at", typeConverter = TwitterTrendsDateConverter.class) + Date createdAt; @JsonField(name = "trends") TrendImpl[] trends; @JsonField(name = "locations") @@ -57,6 +66,11 @@ public class TrendsImpl extends TwitterResponseImpl implements Trends { return locations; } + @Override + public Date getCreatedAt() { + return createdAt; + } + @Override public int compareTo(@NonNull Trends another) { return asOf.compareTo(another.getAsOf()); diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/util/ContentValuesCreator.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/util/ContentValuesCreator.java index bd4ec6ab2..006f963b9 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/util/ContentValuesCreator.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/util/ContentValuesCreator.java @@ -462,11 +462,11 @@ public final class ContentValuesCreator implements TwidereConstants { if (trendsList == null) return new ContentValues[0]; final List resultList = new ArrayList<>(); for (final Trends trends : trendsList) { - final long timestamp = trends.getAsOf().getTime(); +// final long timestamp = trends.getAsOf().getTime(); for (final Trend trend : trends.getTrends()) { final ContentValues values = new ContentValues(); values.put(CachedTrends.NAME, trend.getName()); - values.put(CachedTrends.TIMESTAMP, timestamp); + values.put(CachedTrends.TIMESTAMP, System.currentTimeMillis()); resultList.add(values); } } diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/AsyncTwitterWrapper.java b/twidere/src/main/java/org/mariotaku/twidere/util/AsyncTwitterWrapper.java index 4b8275859..59b63619e 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/AsyncTwitterWrapper.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/AsyncTwitterWrapper.java @@ -40,7 +40,18 @@ import org.mariotaku.querybuilder.Expression; import org.mariotaku.querybuilder.RawItemArray; import org.mariotaku.querybuilder.SQLFunctions; import org.mariotaku.twidere.R; +import org.mariotaku.twidere.api.twitter.Twitter; +import org.mariotaku.twidere.api.twitter.TwitterException; +import org.mariotaku.twidere.api.twitter.http.HttpResponseCode; +import org.mariotaku.twidere.api.twitter.model.DirectMessage; +import org.mariotaku.twidere.api.twitter.model.Paging; +import org.mariotaku.twidere.api.twitter.model.ResponseList; +import org.mariotaku.twidere.api.twitter.model.SavedSearch; import org.mariotaku.twidere.api.twitter.model.Status; +import org.mariotaku.twidere.api.twitter.model.Trends; +import org.mariotaku.twidere.api.twitter.model.User; +import org.mariotaku.twidere.api.twitter.model.UserList; +import org.mariotaku.twidere.api.twitter.model.UserListUpdate; import org.mariotaku.twidere.app.TwidereApplication; import org.mariotaku.twidere.constant.SharedPreferenceConstants; import org.mariotaku.twidere.model.ListResponse; @@ -88,17 +99,6 @@ import java.util.concurrent.CopyOnWriteArraySet; import edu.tsinghua.spice.Utilies.SpiceProfilingUtil; import edu.tsinghua.spice.Utilies.TypeMappingUtil; import edu.ucdavis.earlybird.ProfilingUtil; -import org.mariotaku.twidere.api.twitter.model.DirectMessage; -import org.mariotaku.twidere.api.twitter.model.Paging; -import org.mariotaku.twidere.api.twitter.model.ResponseList; -import org.mariotaku.twidere.api.twitter.model.SavedSearch; -import org.mariotaku.twidere.api.twitter.model.Trends; -import org.mariotaku.twidere.api.twitter.Twitter; -import org.mariotaku.twidere.api.twitter.TwitterException; -import org.mariotaku.twidere.api.twitter.model.User; -import org.mariotaku.twidere.api.twitter.model.UserList; -import org.mariotaku.twidere.api.twitter.model.UserListUpdate; -import org.mariotaku.twidere.api.twitter.http.HttpResponseCode; import static org.mariotaku.twidere.provider.TwidereDataStore.STATUSES_URIS; import static org.mariotaku.twidere.util.ContentValuesCreator.createDirectMessage; @@ -1934,12 +1934,8 @@ public class AsyncTwitterWrapper extends TwitterWrapper { } @Override - public List getTrends(final Twitter twitter) throws TwitterException { - final ArrayList trends_list = new ArrayList<>(); - if (twitter != null) { - trends_list.add(twitter.getLocationTrends(woeid)); - } - return trends_list; + public List getTrends(@NonNull final Twitter twitter) throws TwitterException { + return twitter.getLocationTrends(woeid); } @Override @@ -2204,7 +2200,7 @@ public class AsyncTwitterWrapper extends TwitterWrapper { this.account_id = account_id; } - public abstract List getTrends(Twitter twitter) throws TwitterException; + public abstract List getTrends(@NonNull Twitter twitter) throws TwitterException; @Override protected ListResponse doInBackground(final Object... params) {