fixed refreshing trends crash

This commit is contained in:
Mariotaku Lee 2015-05-10 02:35:16 +08:00
parent 41995a819d
commit 5ccdb8fd8d
6 changed files with 65 additions and 114 deletions

View File

@ -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<Location> getAvailableTrends() throws TwitterException;
@GET("/trends/available.json")
ResponseList<Location> getAvailableTrends() throws TwitterException;
/**
* Returns the sorted locations that Twitter has trending topic information
* for. The response is an array of &quot;locations&quot; that encode the
* location's WOEID (a <a
* href="http://developer.yahoo.com/geo/geoplanet/">Yahoo! Where On Earth
* ID</a>) and some other human-readable information such as a canonical
* name and country the location belongs in. <br>
* 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 <a
* href="https://dev.twitter.com/docs/api/1.1/get/trends/available">GET
* trends/available | Twitter Developers</a>
* @since Twitter4J 2.1.1
*/
ResponseList<Location> getAvailableTrends(GeoLocation location) throws TwitterException;
@GET("/trends/available.json")
ResponseList<Location> getAvailableTrends(@Query GeoLocation location) throws TwitterException;
/**
* Returns the locations that Twitter has trending topic information for,
* closest to a specified location.<br>
* 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.<br>
* A WOEID is a <a href="http://developer.yahoo.com/geo/geoplanet/">Yahoo!
* Where On Earth ID</a>. <br>
* 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 <a
* href="https://dev.twitter.com/docs/api/1.1/get/trends/closest">GET
* trends/closest | Twitter Developers</a>
* @since Twitter4J 3.0.2
*/
ResponseList<Location> getClosestTrends(GeoLocation location) throws TwitterException;
@GET("/trends/closest.json")
ResponseList<Location> 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. <br>
* 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 <a href="https://dev.twitter.com/docs/api/1.1/get/trends/:woeid">GET
* trends/:woeid | Twitter Developers</a>
* @since Twitter4J 2.1.1
*/
Trends getLocationTrends(int woeid) throws TwitterException;
@GET("/trends/place.json")
ResponseList<Trends> getLocationTrends(@Query("id") int woeid) throws TwitterException;
/**
* Returns the top 10 trending topics for a specific WOEID, if trending
* information is available for it.<br>
* 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 <a href="http://search.twitter.com/">Twitter Search</a>, and the
* Twitter Search URL.<br>
* 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.<br>
* <br>
* This method calls http://api.twitter.com/1.1/trends/place.json
*
* @param woeid <a href="http://developer.yahoo.com/geo/geoplanet/">The
* Yahoo! Where On Earth ID</a> 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 <a href="https://dev.twitter.com/docs/api/1.1/get/trends/place">GET
* trends/place | Twitter Developers</a>
* @since Twitter4J 3.0.2
*/
Trends getPlaceTrends(int woeid) throws TwitterException;
@GET("/trends/place.json")
Trends getPlaceTrends(@Query("id") int woeid) throws TwitterException;
}

View File

@ -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<GeoLocation> CONVERTER = new TypeConverter<GeoLocation>() {
@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"};
}
}

View File

@ -30,10 +30,12 @@ import java.util.Date;
*/
public interface Trends extends TwitterResponse, Comparable<Trends>, Serializable {
Date getAsOf();
Date getAsOf();
Location[] getLocations();
Date getCreatedAt();
Trend[] getTrends();
Location[] getLocations();
Trend[] getTrends();
}

View File

@ -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());

View File

@ -462,11 +462,11 @@ public final class ContentValuesCreator implements TwidereConstants {
if (trendsList == null) return new ContentValues[0];
final List<ContentValues> 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);
}
}

View File

@ -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<Trends> getTrends(final Twitter twitter) throws TwitterException {
final ArrayList<Trends> trends_list = new ArrayList<>();
if (twitter != null) {
trends_list.add(twitter.getLocationTrends(woeid));
}
return trends_list;
public List<Trends> 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<Trends> getTrends(Twitter twitter) throws TwitterException;
public abstract List<Trends> getTrends(@NonNull Twitter twitter) throws TwitterException;
@Override
protected ListResponse<Trends> doInBackground(final Object... params) {