Skip to content

Commit

Permalink
Adding graphs API & improving look of links
Browse files Browse the repository at this point in the history
  • Loading branch information
gotosleep committed May 18, 2014
1 parent 1201709 commit a981422
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
2 changes: 2 additions & 0 deletions res/layout/partial_news_full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
android:layout_height="wrap_content"
android:layout_below="@+id/textViewNewsTitle"
android:maxLines="2"
android:textColorLink="@android:color/black"
android:linksClickable="false"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" />
Expand Down
3 changes: 3 additions & 0 deletions src/io/itch/api/ItchApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.itch.api;

import io.itch.api.responses.GamesResponse;
import io.itch.api.responses.GraphsResponse;
import io.itch.api.responses.KeyResponse;
import retrofit.Callback;
import retrofit.http.GET;
Expand All @@ -16,4 +17,6 @@ public interface ItchApi {
@POST("/login?source=android")
public KeyResponse login(@Query("username") String username, @Query("password") String password);

@GET("/my-games/graphs")
public void listGraphs(Callback<GraphsResponse> callback);
}
44 changes: 42 additions & 2 deletions src/io/itch/api/ItchApiClient.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package io.itch.api;

import io.itch.R;
import io.itch.api.responses.GraphsResponse;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.security.KeyStore;
import java.util.Map;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;

import retrofit.RestAdapter;
import retrofit.client.ApacheClient;
Expand All @@ -24,6 +28,11 @@
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

public class ItchApiClient {

Expand Down Expand Up @@ -61,6 +70,7 @@ public static ItchApi getClient() {
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.registerTypeAdapter(GraphsResponse.class, new GraphsDeserializer())
.create();
String endPoint = "https://itch.io/api/1";
if (!TextUtils.isEmpty(token)) {
Expand Down Expand Up @@ -102,7 +112,7 @@ protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", newSslSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry);
return new ThreadSafeClientConnManager(getParams(), registry);
}

private SSLSocketFactory newSslSocketFactory() {
Expand All @@ -113,4 +123,34 @@ private SSLSocketFactory newSslSocketFactory() {
}
}
}

// This is needed because the graphs API returns inconsistent types.
// When there is data, it returns a json array. When there is no data, it returns an empty object
private static class GraphsDeserializer implements JsonDeserializer<GraphsResponse> {

@Override
public GraphsResponse deserialize(JsonElement json, Type typeOfJson, JsonDeserializationContext context)
throws JsonParseException {
GraphsResponse result = new GraphsResponse();
if (json.isJsonObject()) {
JsonObject object = json.getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
try {
Field field = GraphsResponse.class.getDeclaredField(entry.getKey());
field.setAccessible(true);
Object value;
if (entry.getValue().isJsonArray()) {
value = context.deserialize(entry.getValue(), field.getType());
} else {
value = null;
}
field.set(result, value);
} catch (Exception e) {
Log.e("Itch", "Invalid field: " + e);
}
}
}
return result;
}
}
}
37 changes: 37 additions & 0 deletions src/io/itch/api/responses/GraphsResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.itch.api.responses;

import io.itch.models.GraphPoint;

import java.util.List;

public class GraphsResponse {

private List<GraphPoint> purchases;
private List<GraphPoint> views;
private List<GraphPoint> downloads;

public List<GraphPoint> getPurchases() {
return purchases;
}

public void setPurchases(List<GraphPoint> purchases) {
this.purchases = purchases;
}

public List<GraphPoint> getViews() {
return views;
}

public void setViews(List<GraphPoint> views) {
this.views = views;
}

public List<GraphPoint> getDownloads() {
return downloads;
}

public void setDownloads(List<GraphPoint> downloads) {
this.downloads = downloads;
}

}
25 changes: 25 additions & 0 deletions src/io/itch/models/GraphPoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.itch.models;


public class GraphPoint {

private String date;
private Integer count;

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}

}

0 comments on commit a981422

Please sign in to comment.