Skip to content

Commit

Permalink
Adding news
Browse files Browse the repository at this point in the history
  • Loading branch information
gotosleep committed May 12, 2014
1 parent 0a0c353 commit a0e1808
Show file tree
Hide file tree
Showing 15 changed files with 443 additions and 8 deletions.
1 change: 1 addition & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.MyGamesActivity" />
</activity>
<activity android:name=".activities.NewsActivity" android:parentActivityName=".activities.MyGamesActivity" android:label="@string/news_activity_title"></activity>
</application>

</manifest>
15 changes: 15 additions & 0 deletions res/drawable/news_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
<bitmap
android:gravity="top|clip_vertical"
android:src="@drawable/stock_dark" />
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#AAFFFFFF" />
</shape>
</item>

</layer-list>
Binary file added res/drawable/stock_dark.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions res/layout/activity_news.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<WebView
android:id="@+id/webViewNews"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<ProgressBar
android:id="@+id/progressBarWeb"
style="@android:style/Widget.Holo.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="-7dp" />

</RelativeLayout>
67 changes: 67 additions & 0 deletions res/layout/news_header.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/imageViewGameImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/viewGroupNewsHeaderInfo"
android:layout_alignTop="@+id/viewGroupNewsHeaderInfo"
android:scaleType="centerCrop"
android:src="@drawable/news_background" />

<RelativeLayout
android:id="@+id/viewGroupNewsHeaderInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/news_header_padding_vertical"
android:paddingLeft="@dimen/news_header_padding_horizontal"
android:paddingRight="@dimen/news_header_padding_horizontal"
android:paddingTop="@dimen/news_header_padding_vertical" >

<TextView
android:id="@+id/textViewNewsHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/news_latest_margin_bottom"
android:text="@string/news_latest"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black"
android:textStyle="italic|bold" />

<TextView
android:id="@+id/textViewNewsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewNewsHeader"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#A33446"
android:textStyle="bold" />

<TextView
android:id="@+id/textViewNewsBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewNewsTitle"
android:maxLines="2"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" />

<TextView
android:id="@+id/textViewNewsReadMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textViewNewsBody"
android:layout_marginTop="@dimen/news_header_read_more_margin_top"
android:text="@string/news_read_more"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" />
</RelativeLayout>

</RelativeLayout>
6 changes: 5 additions & 1 deletion res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
<dimen name="activity_empty_image_margin_bottom">30dp</dimen>
<dimen name="text_size_large">24sp</dimen>
<dimen name="activity_scroll_padding_bottom">20dp</dimen>
<dimen name="news_header_padding_vertical">10dp</dimen>
<dimen name="news_header_padding_horizontal">30dp</dimen>
<dimen name="news_latest_margin_bottom">10dp</dimen>
<dimen name="news_header_read_more_margin_top">10dp</dimen>

</resources>
</resources>
7 changes: 5 additions & 2 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
<string name="game_created_at_label">Created At</string>
<string name="game_platforms_label">Platforms</string>
<string name="game_type_label">Game Type</string>
<string name="my_games_activity_title">My Games</string>
<string name="my_games_activity_title">Itch.io</string>
<string name="news_activity_title">News</string>
<string name="game_earnings_label">Earnings</string>
<string name="button_login">Log in</string>
<string name="activity_empty_default">Sorry, nothing to see here :(</string>
<string name="my_games_activity_empty">You have no games!!! ? :(</string>
<string name="menu_share">Share</string>
<string name="menu_open_browser">Open in browser</string>

<string name="news_latest">LATEST NEWS</string>
<string name="news_read_more">Read More...</string>

</resources>
63 changes: 58 additions & 5 deletions src/io/itch/activities/MyGamesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
import io.itch.R.id;
import io.itch.api.ItchApi;
import io.itch.api.ItchApiClient;
import io.itch.api.TumblrApi;
import io.itch.api.TumblrApiClient;
import io.itch.api.responses.GamesResponse;
import io.itch.api.responses.PostsResponse;
import io.itch.authentication.SessionHelper;
import io.itch.authentication.SessionHelper.SessionCallback;
import io.itch.lists.GameAdapter;
import io.itch.models.Game;
import io.itch.models.tumblr.Post;
import io.itch.views.PostViewHelper;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -28,6 +34,7 @@ public class MyGamesActivity extends BaseActivity {

private ListView gamesList;
private ArrayAdapter<Game> gamesAdapter;
private View header;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -41,12 +48,18 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public void onItemClick(AdapterView<?> list, View item, int position, long id) {
Game game = gamesAdapter.getItem(position);
if (game != null) {
Intent i = new Intent(MyGamesActivity.this, GameActivity.class);
i.putExtra(Extras.EXTRA_GAME, game);
// Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(game.getUrl()));
Integer headerCount = gamesList.getHeaderViewsCount();
if (headerCount > 0 && position < headerCount) {
Intent i = new Intent(MyGamesActivity.this, NewsActivity.class);
startActivity(i);
} else {
position -= headerCount;
Game game = gamesAdapter.getItem(position);
if (game != null) {
Intent i = new Intent(MyGamesActivity.this, GameActivity.class);
i.putExtra(Extras.EXTRA_GAME, game);
startActivity(i);
}
}
}
});
Expand All @@ -56,6 +69,7 @@ public void onItemClick(AdapterView<?> list, View item, int position, long id) {
protected void onStart() {
super.onStart();
this.updateGames();
this.updateNews();
}

@Override
Expand Down Expand Up @@ -125,6 +139,45 @@ public void failure(RetrofitError e) {
});
}

private void updateNews() {
TumblrApi api = TumblrApiClient.getClient();
api.listPosts(1, new Callback<PostsResponse>() {

@Override
public void failure(RetrofitError e) {
Log.e("Itch", "Failed to retrieve news", e);
}

@Override
public void success(PostsResponse result, Response arg1) {
Log.i("Itch", "Got news: " + result);
if (result != null && result.getResponse() != null && result.getResponse().getPosts() != null
&& result.getResponse().getPosts().size() > 0) {
loadNewsHeader(result.getResponse().getPosts().get(0));
}
}
});
}

private void loadNewsHeader(Post post) {
View header = LayoutInflater.from(this).inflate(R.layout.news_header, null);
PostViewHelper.populateView(this, header, post);
ListView list = (ListView) findViewById(R.id.listViewGames);
setHeader(header, list);
}

private void setHeader(View header, ListView list) {
if (header != this.header) {
if (this.header != null) {
list.removeHeaderView(this.header);
}
this.header = header;
if (this.header != null) {
list.addHeaderView(this.header);
}
}
}

@Override
protected String getScreenPath() {
return "My Games";
Expand Down
57 changes: 57 additions & 0 deletions src/io/itch/activities/NewsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.itch.activities;

import io.itch.R;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;

@SuppressLint("SetJavaScriptEnabled")
public class NewsActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
WebView webview = (WebView) findViewById(R.id.webViewNews);
webview.getSettings().setJavaScriptEnabled(true);
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBarWeb);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);

// remove some of the tumblr crap that we don't want
view.loadUrl("javascript:(function(){ " +
"try {" +
" var header = document.getElementById('header');" +
" header.style.display = 'none';" +
" var hider = function(name) {" +
" var a = document.getElementsByClassName(name); " +
" var item;" +
" for (var i = 0; i < a.length; ++i) {" +
" item = a[i];" +
" item.style.display = 'none';" +
" }" +
" };" +
" hider('nav-menu-wrapper');" +
" hider('nav-menu-bg');" +
"} catch (e) { " +
"}" +
"})();");
if (progress >= 100) {
progressBar.setVisibility(View.GONE);
}
}
});

webview.loadUrl("http://itchio.tumblr.com");
}

@Override
protected String getScreenPath() {
return "News";
}

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

import io.itch.api.responses.PostsResponse;
import retrofit.Callback;
import retrofit.http.GET;
import retrofit.http.Query;

public interface TumblrApi {

@GET("/posts")
public void listPosts(@Query("limit") Integer limit, Callback<PostsResponse> callback);

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

import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.converter.GsonConverter;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class TumblrApiClient {

private static final String KEY = "fWnvNwLMtOaJxKhMJuJPUXa4n0SyNled0byq9dSwRN5hoQJPED";
private static final Object INSTANCE_LOCK = new Object();
private static TumblrApi SHARED_INSTANCE;

// http://api.tumblr.com/v2/blog/citriccomics.tumblr.com/posts/text?api_key={key}
public static TumblrApi getClient() {
if (SHARED_INSTANCE == null) {
synchronized (INSTANCE_LOCK) {
if (SHARED_INSTANCE == null) {
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
String endPoint = "http://api.tumblr.com/v2/blog/itchio.tumblr.com/";
RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addQueryParam("api_key", KEY);
}
};
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(endPoint)
.setConverter(new GsonConverter(gson))
.setRequestInterceptor(requestInterceptor)
.build();
SHARED_INSTANCE = adapter.create(TumblrApi.class);
}
}
}
return SHARED_INSTANCE;
}
}
Loading

0 comments on commit a0e1808

Please sign in to comment.