Skip to content

Commit e28eca5

Browse files
author
Mohit Pandey
committed
Cleaned up some stuff. Will be adding git flow next.
1 parent 5021e08 commit e28eca5

15 files changed

+286
-61
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.gradle
22
gradle-wrapper.jar
3+
uiDesigner.xml
4+
key.xml

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
ReactiveAndroid
22
===============
33

4-
Demo app showing reactive development usage in Android along with Lambdas.
4+
This is an experimental Android app. It shows reactive programming paradigm by using RxJava from Netflix. To make coding
5+
more pleasant, it uses Lamdas from Java 8. The app pulls the content from [Rotten Tomatoes API](http://developer.rottentomatoes.com/ "API").
6+
Please use this link and register to get your own developer key. It is needed to run this demo app.
57

8+
Place the key under src/main/res/values/key.xml (or any other name). key.xml is already added to .gitignore so it will
9+
save you some work.
610

7-
This is the intial unclean version. You need to have mashery key for rottentomatoes.
11+
Here is what the contents of key.xml look like:
12+
13+
<?xml version="1.0" encoding="utf-8"?>
14+
<resources>
15+
<string name="ROTTEN_KEY">YOUR DEVELOPER KEY</string>
16+
</resources>
17+
18+
19+
In progress:
20+
21+
The work in this app is still in progress. I want to also demonstrate usage of Dagger, Otto(?) and couple more
22+
useful libraries.

ReactiveAndroid/ReactiveAndroid-ReactiveAndroid.iml

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<excludeFolder url="file://$MODULE_DIR$/build/symbols" />
6969
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
7070
</content>
71-
<orderEntry type="jdk" jdkName="Android API 19 Platform" jdkType="Android SDK" />
71+
<orderEntry type="jdk" jdkName="Android API 19 Platform (3)" jdkType="Android SDK" />
7272
<orderEntry type="sourceFolder" forTests="false" />
7373
<orderEntry type="library" exported="" name="picasso-2.2.0" level="project" />
7474
<orderEntry type="library" exported="" name="dagger-1.2.1" level="project" />

ReactiveAndroid/src/main/AndroidManifest.xml

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<category android:name="android.intent.category.LAUNCHER" />
1919
</intent-filter>
2020
</activity>
21+
<activity
22+
android:name="com.mohit.reactiveandroid.MovieActivity"
23+
android:label="Movie Details" >
24+
</activity>
2125
</application>
2226

2327
</manifest>

ReactiveAndroid/src/main/java/com/mohit/reactiveandroid/GridAdapter.java

+15-16
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33

44
import android.content.Context;
5+
import android.util.Log;
56
import android.view.LayoutInflater;
67
import android.view.View;
78
import android.view.ViewGroup;
89
import android.widget.BaseAdapter;
910
import android.widget.ImageView;
1011
import android.widget.TextView;
12+
import android.widget.Toast;
1113
import com.squareup.picasso.Picasso;
1214
import rx.Observable;
15+
import rx.Subscription;
1316
import rx.android.schedulers.AndroidSchedulers;
1417

1518
import java.util.ArrayList;
@@ -20,13 +23,18 @@ public class GridAdapter extends BaseAdapter {
2023
private Context context;
2124
private static final NetworkManager networkManager = new NetworkManager();
2225
private List<Movie> movies = new ArrayList<Movie>();
26+
private Subscription subscription;
2327

2428
public GridAdapter(Context context, String source) {
2529
this.context = context;
26-
networkManager.getData(source)
30+
Log.e(this.getClass().getName(),"new grid adapter");
31+
32+
subscription = networkManager.getData(source)
2733
.flatMap(data -> new FeedParser().getMovies(data))
2834
.observeOn(AndroidSchedulers.mainThread())
29-
.onErrorFlatMap(e -> Observable.empty())
35+
.onErrorFlatMap(e -> {
36+
return Observable.empty();
37+
})
3038
.subscribe((Movies m) -> {
3139
movies.clear();
3240
movies.addAll(m.movies);
@@ -46,23 +54,14 @@ public long getItemId(int position) {
4654
return position;
4755
}
4856

49-
// create a new ImageView for each item referenced by the Adapter
5057
public View getView(int position, View convertView, ViewGroup parent) {
51-
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
52-
53-
View gridView;
58+
MovieView movieView;
5459
if (convertView == null) {
55-
gridView = inflater.inflate(R.layout.item, null);
60+
movieView = new MovieView(this.context);
5661
} else {
57-
gridView = convertView;
62+
movieView = (MovieView)convertView;
5863
}
59-
60-
Movie movie = getItem(position);
61-
TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
62-
ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
63-
Picasso.with(context).load(movie.getPosters().getProfile()).into(imageView);
64-
textView.setText(movie.getTitle());
65-
66-
return gridView;
64+
movieView.setMovie(getItem(position));
65+
return movieView;
6766
}
6867
}

ReactiveAndroid/src/main/java/com/mohit/reactiveandroid/MainActivity.java

+59-33
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import android.app.*;
44
import android.content.Context;
5+
import android.content.Intent;
56
import android.os.Bundle;
67
import android.support.v13.app.FragmentPagerAdapter;
78
import android.support.v4.view.ViewPager;
9+
import android.util.Log;
810
import android.view.*;
11+
import android.widget.AdapterView;
912
import android.widget.GridView;
1013

1114
public class MainActivity extends Activity implements ActionBar.TabListener {
@@ -25,18 +28,20 @@ public class MainActivity extends Activity implements ActionBar.TabListener {
2528
*/
2629
ViewPager mViewPager;
2730

28-
private static final String TAG = "r3ader";
31+
32+
//ViewPager.setOffscreenPageLimit(int) way to control how many pages
33+
34+
private static final String TAG = MainActivity.class.getName();
35+
private String[] sections;
2936

3037
@Override
3138
protected void onCreate(Bundle savedInstanceState) {
3239
super.onCreate(savedInstanceState);
3340
setContentView(R.layout.activity_main);
34-
35-
// Set up the action bar.
41+
sections = getResources().getStringArray(R.array.sections);
3642
final ActionBar actionBar = getActionBar();
3743
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
3844

39-
int sections = 4;
4045
// Create the adapter that will return a fragment for each of the three
4146
// primary sections of the activity.
4247
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(), this, sections);
@@ -54,13 +59,20 @@ public void onPageSelected(int position) {
5459
actionBar.setSelectedNavigationItem(position);
5560
}
5661
});
57-
String[] sectionNames = {"In Theaters", "DVD Releases", "Opening", "Box Office"};
58-
for (int i = 0; i < sections; i++) {
59-
actionBar.addTab(actionBar.newTab().setText(sectionNames[i]).setTabListener(this));
62+
for (String section : sections) {
63+
actionBar.addTab(actionBar.newTab().setText(getName(section)).setTabListener(this));
6064
}
6165
}
6266

6367

68+
/* String name in resource files can not have spaces,
69+
hence replace the _ with spaces to get the actual desired
70+
values.
71+
*/
72+
public String getName(String withUnderscores) {
73+
return withUnderscores.replaceAll("_", " ");
74+
}
75+
6476
@Override
6577
public boolean onCreateOptionsMenu(Menu menu) {
6678

@@ -103,15 +115,9 @@ public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTrans
103115
public class SectionsPagerAdapter extends FragmentPagerAdapter {
104116

105117
private Context context;
106-
private int sections;
107-
private String[] LIST = {
108-
"http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?page_limit=24&page=1&country=us&apikey=",
109-
"http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/new_releases.json?page_limit=24&page=1&country=us&apikey=",
110-
"http://api.rottentomatoes.com/api/public/v1.0/lists/dvd/upcoming.json?page_limit=24&page=1&country=us&apikey=",
111-
"http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?page_limit=24&page=1&country=us&apikey="
112-
};
113-
114-
public SectionsPagerAdapter(FragmentManager fm, Context context, int sections) {
118+
private String[] sections;
119+
120+
public SectionsPagerAdapter(FragmentManager fm, Context context, String[] sections) {
115121
super(fm);
116122
this.context = context;
117123
this.sections = sections;
@@ -121,54 +127,74 @@ public SectionsPagerAdapter(FragmentManager fm, Context context, int sections) {
121127
public Fragment getItem(int position) {
122128
// getItem is called to instantiate the fragment for the given page.
123129
// Return a PlaceholderFragment (defined as a static inner class below).
124-
GridAdapter gridAdapter = new GridAdapter(this.context, LIST[position]);
125-
return PlaceholderFragment.newInstance(position, context, gridAdapter);
130+
return PlaceholderFragment.newInstance(position, context);
126131
}
127132

128133
@Override
129134
public int getCount() {
130135
// Show 3 total pages.
131-
return sections;
136+
return sections.length;
132137
}
133138
}
134139

135140
/**
136141
* A placeholder fragment containing a simple view.
137142
*/
138143
public static class PlaceholderFragment extends Fragment {
139-
/**
140-
* The fragment argument representing the section number for this
141-
* fragment.
142-
*/
143-
private static final String ARG_SECTION_NUMBER = "section_number";
144-
145144
private int position;
146-
private GridAdapter gridAdapter;
145+
private static String [] sections;
147146

148147
/**
149148
* Returns a new instance of this fragment for the given section
150149
* number.
151150
*/
152-
public static PlaceholderFragment newInstance(int sectionNumber, Context context, GridAdapter gridAdapter) {
151+
public static PlaceholderFragment newInstance(int sectionNumber, Context context) {
153152
PlaceholderFragment fragment = new PlaceholderFragment();
154153
Bundle args = new Bundle();
155-
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
156-
fragment.position = sectionNumber;
154+
args.putInt("position", sectionNumber);
157155
fragment.setArguments(args);
158-
fragment.gridAdapter = gridAdapter;
156+
fragment.sections = context.getResources().getStringArray(R.array.sections);
157+
fragment.position = sectionNumber;
158+
Log.d(TAG, "creating a new fragment");
159159
return fragment;
160160
}
161161

162-
public PlaceholderFragment() {
163-
}
164-
165162
@Override
166163
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
164+
int labelId = getResources().getIdentifier(sections[position], "string", getActivity().getPackageName());
165+
String url = getResources().getString(labelId);
166+
String KEY = getString(R.string.ROTTEN_KEY);
167+
GridAdapter gridAdapter = new GridAdapter(this.getActivity(), url + KEY);
168+
169+
167170
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
168171
GridView gridview = (GridView) rootView.findViewById(R.id.gridView);
172+
Log.w(TAG,gridAdapter.toString());
169173
gridview.setAdapter(gridAdapter);
174+
gridview.setOnItemClickListener((AdapterView<?> parent, View v, int position, long id) -> {
175+
176+
Movie movie = gridAdapter.getItem(position);
177+
Intent intent = new Intent(this.getActivity(), MovieActivity.class);
178+
intent.putExtra("movie", movie);
179+
this.getActivity().startActivity(intent);
180+
});
181+
Log.d(TAG, "creating new grid");
170182
return rootView;
171183
}
184+
185+
@Override
186+
public void onCreate(Bundle savedInstanceState) {
187+
super.onCreate(savedInstanceState);
188+
if(getArguments() != null) {
189+
position = getArguments().getInt("position");
190+
}
191+
}
192+
193+
@Override
194+
public void onDestroy() {
195+
super.onDestroy();
196+
Log.w(TAG,"Destroying");
197+
}
172198
}
173199

174200
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.mohit.reactiveandroid;
2+
3+
import android.app.Activity;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.view.Menu;
7+
import android.view.MenuItem;
8+
import android.widget.ImageView;
9+
import android.widget.TextView;
10+
import com.squareup.picasso.Picasso;
11+
12+
public class MovieActivity extends Activity {
13+
14+
private Movie movie;
15+
16+
@Override
17+
protected void onCreate(Bundle savedInstanceState) {
18+
super.onCreate(savedInstanceState);
19+
setContentView(R.layout.activity_movie);
20+
21+
Intent intent = getIntent();
22+
movie = (Movie) intent.getSerializableExtra("movie");
23+
24+
TextView textView = (TextView) findViewById(R.id.title);
25+
textView.setText(movie.getTitle());
26+
27+
TextView synopsisView = (TextView) findViewById(R.id.synopsis);
28+
synopsisView.setText(movie.getSynopsis());
29+
30+
ImageView posterView = (ImageView) findViewById(R.id.poster);
31+
32+
Picasso.with(this)
33+
.load(movie.getPosters().getProfile())
34+
.into(posterView);
35+
}
36+
37+
38+
@Override
39+
public boolean onCreateOptionsMenu(Menu menu) {
40+
41+
// Inflate the menu; this adds items to the action bar if it is present.
42+
getMenuInflater().inflate(R.menu.main, menu);
43+
return true;
44+
}
45+
46+
@Override
47+
public boolean onOptionsItemSelected(MenuItem item) {
48+
// Handle action bar item clicks here. The action bar will
49+
// automatically handle clicks on the Home/Up button, so long
50+
// as you specify a parent activity in AndroidManifest.xml.
51+
int id = item.getItemId();
52+
if (id == R.id.action_settings) {
53+
return true;
54+
}
55+
return super.onOptionsItemSelected(item);
56+
}
57+
58+
// /**
59+
// * A placeholder fragment containing a simple view.
60+
// */
61+
// public static class PlaceholderFragment extends Fragment {
62+
//
63+
// public PlaceholderFragment() {
64+
// }
65+
//
66+
// @Override
67+
// public View onCreateView(LayoutInflater inflater, ViewGroup container,
68+
// Bundle savedInstanceState) {
69+
// View rootView = inflater.inflate(R.layout.fragment_movie, container, false);
70+
// return rootView;
71+
// }
72+
// }
73+
74+
}

0 commit comments

Comments
 (0)