Skip to content

Commit 8d70d78

Browse files
committed
Switch Marvel API to star wars because it's completely public
1 parent a977836 commit 8d70d78

File tree

34 files changed

+315
-495
lines changed

34 files changed

+315
-495
lines changed

README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
Android Boilerplate
22
===================
33

4-
After finding no examples online of Espresso / Robolectric setup in Android Studio with the new Unit Testing feature
5-
(without using a third party plugin) I decided to create this boilerplate project to document the setup. After
6-
recreating the same project setup over and over again I decided it was also about time to do so! The project structure is as follows:
7-
8-
<p align="center"><img src="http://i617.photobucket.com/albums/tt254/joeyerrr/project_structure.png" /></p>
9-
10-
All ready to go with:
11-
12-
- Espresso testing
13-
- Robolectric testing
14-
- Network requests using retrofit and Rx Java
15-
- Database management using Rx java and pure SQLite
16-
- Holders for list items using EasyAdapter
4+
A simple boilerplate application which demonstrates the downloading, persisting and syncing of data - displayed with a common layout used amongst applications.
5+
6+
The project is setup to use:
7+
8+
- Espresso automated tests, contained in a seperate androidTest module
9+
- Robolectric Unit tests
10+
- Dagger 2 for dependancy injection
11+
- Networking using retrofit with Rx Java
12+
- Database management using Rx java, SQLBrite and pure SQLite
13+
- Data Sync Service
14+
- Design Support library integrated for Material Style view animations (includes TabLayout, CollapsingToolbar & CoordinatorLayout implementations)
15+
- Holders (adapters) for recycler views using EasyAdapter
1716
- Butterknife for easy view injection
18-
- Picasso for easy image downloading / display
17+
- Glide for easy image downloading / display
18+
- Timber for simple Debug logging
1919

2020
Requirements
2121
------------
2222

2323
- [Android SDK](http://developer.android.com/sdk/index.html).
2424
- Android [5.1 (API 22) ](http://developer.android.com/tools/revisions/platforms.html#5.1).
2525
- Android SDK Tools
26-
- Android SDK Build tools 22.0.1
26+
- Android SDK Build tools 23.0.0.0 rc3
2727
- Android Support Repository
2828
- Android Support library
29-
- Enabled [Unit Test support] (http://tools.android.com/tech-docs/unit-testing-support)
3029

3130
Building
3231
--------

app/build.gradle

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,27 @@ dependencies {
3737
final DAGGER_VERSION = '2.0.1'
3838
final HAMCREST_VERSION = '1.3'
3939
final MOCKITO_VERSION = '1.10.19'
40-
4140
compile fileTree(dir: 'libs', include: ['*.jar'])
42-
4341
compile 'com.android.support:support-v4:22.2.1'
4442
compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION"
4543
compile "com.android.support:cardview-v7:$SUPPORT_LIBRARY_VERSION"
4644
compile "com.android.support:design:$SUPPORT_LIBRARY_VERSION"
4745
compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION"
4846
compile "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION"
49-
5047
compile 'com.squareup.sqlbrite:sqlbrite:0.2.1'
5148
compile 'com.squareup.retrofit:retrofit:1.9.0'
5249
compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'
5350
compile 'com.squareup.okhttp:okhttp:2.4.0'
5451
compile 'com.squareup:otto:1.3.8'
5552
compile 'com.github.bumptech.glide:glide:3.6.1'
5653
compile 'de.hdodenhof:circleimageview:1.3.0'
57-
5854
compile 'io.reactivex:rxandroid:0.25.0'
5955
compile 'com.jakewharton:butterknife:7.0.1'
6056
compile 'com.jakewharton.timber:timber:3.1.0'
6157
compile 'uk.co.ribot:easyadapter:1.5.0@aar'
62-
63-
compile "com.google.dagger:dagger:$DAGGER_VERSION"
64-
apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
58+
compile "com.google.dagger:dagger:$DAGGER_VERSION"
59+
apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
6560
provided 'org.glassfish:javax.annotation:10.0-b28'
66-
6761
testCompile 'junit:junit:4.12'
6862
testCompile "org.hamcrest:hamcrest-core:$HAMCREST_VERSION"
6963
testCompile "org.hamcrest:hamcrest-library:$HAMCREST_VERSION"
@@ -73,6 +67,6 @@ dependencies {
7367
exclude group: 'commons-logging', module: 'commons-logging'
7468
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
7569
}
76-
7770
androidTestApt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
71+
compile 'com.google.dexmaker:dexmaker:1.2'
7872
}

app/src/main/java/com/hitherejoe/androidboilerplate/data/DataManager.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,22 @@ public Scheduler getScheduler() {
6969
}
7070

7171
public Observable<Character> syncCharacters(int[] ids) {
72-
return getAvengers(ids).toList().concatMap(new Func1<List<Character>, Observable<? extends Character>>() {
72+
return getCharacters(ids).toList().concatMap(new Func1<List<Character>, Observable<? extends Character>>() {
7373
@Override
7474
public Observable<? extends Character> call(List<Character> characters) {
7575
return mDatabaseHelper.setCharacters(characters);
7676
}
7777
});
7878
}
7979

80-
public Observable<Character> getAvengers(int[] ids) {
80+
public Observable<Character> getCharacters(int[] ids) {
8181
List<Integer> characterIds = new ArrayList<>(ids.length);
8282
for (int id : ids) characterIds.add(id);
83-
return Observable.from(characterIds).concatMap(new Func1<Integer, Observable<AndroidBoilerplateService.CharacterResponse>>() {
83+
return Observable.from(characterIds).concatMap(new Func1<Integer, Observable<Character>>() {
8484
@Override
85-
public Observable<AndroidBoilerplateService.CharacterResponse> call(Integer integer) {
85+
public Observable<Character> call(Integer integer) {
8686
return mAndroidBoilerplateService.getCharacter(integer);
8787
}
88-
}).concatMap(new Func1<AndroidBoilerplateService.CharacterResponse, Observable<Character>>() {
89-
@Override
90-
public Observable<Character> call(AndroidBoilerplateService.CharacterResponse characterResponse) {
91-
if (characterResponse.data.results != null && !characterResponse.data.results.isEmpty()) {
92-
return Observable.just(characterResponse.data.results.get(0));
93-
}
94-
return Observable.empty();
95-
}
9688
});
9789
}
9890

@@ -107,7 +99,7 @@ public Observable<? extends Character> call(List<Character> characters) {
10799
public Observable<? extends Character> call(Character character) {
108100
return Observable.just(character);
109101
}
110-
});
102+
}).distinct();
111103
}
112104

113105
}

app/src/main/java/com/hitherejoe/androidboilerplate/data/SyncService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
public class SyncService extends Service {
2626

27-
public static final String TAG = "SyncService";
28-
2927
@Inject DataManager mDataManager;
3028
private Subscription mSubscription;
3129

@@ -55,7 +53,7 @@ public int onStartCommand(Intent intent, int flags, final int startId) {
5553
}
5654

5755
if (mSubscription != null && !mSubscription.isUnsubscribed()) mSubscription.unsubscribe();
58-
int[] characterIds = getResources().getIntArray(R.array.avengers);
56+
int[] characterIds = getResources().getIntArray(R.array.characters);
5957

6058
mSubscription = mDataManager.syncCharacters(characterIds)
6159
.observeOn(AndroidSchedulers.mainThread())

app/src/main/java/com/hitherejoe/androidboilerplate/data/local/Db.java

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import com.google.gson.Gson;
77
import com.google.gson.reflect.TypeToken;
88
import com.hitherejoe.androidboilerplate.data.model.Character;
9-
import com.hitherejoe.androidboilerplate.data.model.Collection;
109
import com.hitherejoe.androidboilerplate.util.DataUtils;
1110

11+
import java.util.List;
12+
1213
public class Db {
1314

1415
public Db() { }
@@ -17,50 +18,71 @@ public static abstract class CharacterTable {
1718
public static final String TABLE_NAME = "characters";
1819
public static final String COLUMN_ID = "id";
1920
public static final String COLUMN_NAME = "name";
20-
public static final String COLUMN_DESCRIPTION = "description";
21-
public static final String COLUMN_THUMBNAIL = "thumbnail";
22-
public static final String COLUMN_COMICS = "comics";
23-
public static final String COLUMN_SERIES = "series";
24-
public static final String COLUMN_STORIES = "stories";
25-
public static final String COLUMN_EVENTS = "events";
21+
public static final String COLUMN_HEIGHT = "height";
22+
public static final String COLUMN_MASS = "mass";
23+
public static final String COLUMN_HAIR_COLOR = "hair_color";
24+
public static final String COLUMN_SKIN_COLOR = "skin_color";
25+
public static final String COLUMN_EYE_COLOR = "eye_color";
26+
public static final String COLUMN_BIRTH_YEAR = "birth_year";
27+
public static final String COLUMN_GENDER = "gender";
28+
public static final String COLUMN_HOME_WORLD = "home_world";
29+
public static final String COLUMN_FILMS = "films";
30+
public static final String COLUMN_SPECIES = "species";
31+
public static final String COLUMN_VEHICLES = "vehicles";
32+
public static final String COLUMN_STARSHIPS = "starships";
2633

2734
public static final String CREATE =
2835
"CREATE TABLE " + TABLE_NAME + " (" +
29-
COLUMN_ID + " INTEGER PRIMARY KEY," +
36+
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
3037
COLUMN_NAME + " TEXT NOT NULL," +
31-
COLUMN_DESCRIPTION + " TEXT," +
32-
COLUMN_THUMBNAIL + " TEXT," +
33-
COLUMN_COMICS + " TEXT," +
34-
COLUMN_SERIES + " TEXT," +
35-
COLUMN_STORIES + " TEXT," +
36-
COLUMN_EVENTS + " TEXT" +
38+
COLUMN_HEIGHT + " TEXT," +
39+
COLUMN_MASS + " TEXT," +
40+
COLUMN_HAIR_COLOR + " TEXT," +
41+
COLUMN_SKIN_COLOR + " TEXT," +
42+
COLUMN_EYE_COLOR + " TEXT," +
43+
COLUMN_BIRTH_YEAR + " TEXT," +
44+
COLUMN_GENDER + " TEXT," +
45+
COLUMN_HOME_WORLD + " TEXT," +
46+
COLUMN_FILMS + " TEXT," +
47+
COLUMN_SPECIES + " TEXT," +
48+
COLUMN_VEHICLES + " TEXT," +
49+
COLUMN_STARSHIPS + " TEXT" +
3750
" ); ";
3851

3952
public static ContentValues toContentValues(Character character) {
4053
Gson gson = DataUtils.getGsonInstance();
4154
ContentValues values = new ContentValues();
42-
values.put(COLUMN_ID, character.id);
4355
values.put(COLUMN_NAME, character.name);
44-
values.put(COLUMN_DESCRIPTION, character.description);
45-
values.put(COLUMN_THUMBNAIL, gson.toJson(character.thumbnail, new TypeToken<Character.Thumbnail>(){}.getType()));
46-
values.put(COLUMN_COMICS, gson.toJson(character.comics, new TypeToken<Collection>(){}.getType()));
47-
values.put(COLUMN_SERIES, gson.toJson(character.series, new TypeToken<Collection>(){}.getType()));
48-
values.put(COLUMN_STORIES, gson.toJson(character.stories, new TypeToken<Collection>(){}.getType()));
49-
values.put(COLUMN_EVENTS, gson.toJson(character.events, new TypeToken<Collection>(){}.getType()));
56+
values.put(COLUMN_HEIGHT, character.height);
57+
values.put(COLUMN_MASS, character.mass);
58+
values.put(COLUMN_HAIR_COLOR, character.hairColor);
59+
values.put(COLUMN_SKIN_COLOR, character.skinColor);
60+
values.put(COLUMN_EYE_COLOR, character.eyeColor);
61+
values.put(COLUMN_BIRTH_YEAR, character.birthYear);
62+
values.put(COLUMN_GENDER, character.gender);
63+
values.put(COLUMN_HOME_WORLD, character.homeworld);
64+
values.put(COLUMN_FILMS, gson.toJson(character.films, new TypeToken<List<String>>(){}.getType()));
65+
values.put(COLUMN_SPECIES, gson.toJson(character.species, new TypeToken<List<String>>(){}.getType()));
66+
values.put(COLUMN_VEHICLES, gson.toJson(character.vehicles, new TypeToken<List<String>>(){}.getType()));
67+
values.put(COLUMN_STARSHIPS, gson.toJson(character.starships, new TypeToken<List<String>>(){}.getType()));
5068
return values;
5169
}
5270

5371
public static Character parseCursor(Cursor cursor) {
5472
Gson gson = DataUtils.getGsonInstance();
5573
Character character = new Character();
56-
character.id = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID));
5774
character.name = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NAME));
58-
character.description = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DESCRIPTION));
59-
character.thumbnail = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_THUMBNAIL)), new TypeToken<Character.Thumbnail>() {}.getType());
60-
character.comics = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_COMICS)), new TypeToken<Collection>() {}.getType());
61-
character.series = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_SERIES)), new TypeToken<Collection>() {}.getType());
62-
character.stories = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_STORIES)), new TypeToken<Collection>() {}.getType());
63-
character.events = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_EVENTS)), new TypeToken<Collection>() {}.getType());
75+
character.height = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_HEIGHT));
76+
character.mass = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MASS));
77+
character.hairColor = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_SKIN_COLOR));
78+
character.eyeColor = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_EYE_COLOR));
79+
character.birthYear = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_BIRTH_YEAR));
80+
character.gender = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_GENDER));
81+
character.homeworld = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_HOME_WORLD));
82+
character.films = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_FILMS)), new TypeToken<List<String>>() {}.getType());
83+
character.species = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_SPECIES)), new TypeToken<List<String>>() {}.getType());
84+
character.vehicles = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_VEHICLES)), new TypeToken<List<String>>() {}.getType());
85+
character.starships = gson.fromJson(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_STARSHIPS)), new TypeToken<List<String>>() {}.getType());
6486
return character;
6587
}
6688
}

0 commit comments

Comments
 (0)