Skip to content

Commit 1e05410

Browse files
committed
add about page and fix bugs
fix cache from persistence ClassCastException
1 parent 5749be5 commit 1e05410

24 files changed

+304
-72
lines changed

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ android {
1010
minSdkVersion rootProject.ext.minSdkVersion
1111
targetSdkVersion rootProject.ext.targetSdkVersion
1212
versionCode 1
13-
versionName "1.0"
13+
versionName "0.1.1 beta"
1414
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1515

1616
jackOptions{
@@ -102,5 +102,6 @@ dependencies {
102102

103103
compile 'com.jakewharton.timber:timber:4.5.1'
104104

105+
compile 'com.github.medyo:android-about-page:1.2'
105106

106107
}

app/src/androidTest/AndroidManifest.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<category android:name="android.intent.category.LAUNCHER"/>
2323
</intent-filter>
2424
</activity>
25+
<activity android:name=".ui.AboutActivity">
26+
</activity>
2527
</application>
2628

2729
</manifest>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2016 Victor Albertos
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.junnanhao.gank.data.gson;
18+
19+
import com.google.gson.Gson;
20+
import java.io.BufferedReader;
21+
import java.io.File;
22+
import java.io.FileReader;
23+
import java.io.IOException;
24+
import java.lang.reflect.GenericArrayType;
25+
import java.lang.reflect.ParameterizedType;
26+
import java.lang.reflect.Type;
27+
28+
import io.victoralbertos.jolyglot.JolyglotGenerics;
29+
import io.victoralbertos.jolyglot.Types;
30+
31+
/**
32+
* Gson implementation of Jolyglot
33+
*/
34+
public class GsonSpeaker implements JolyglotGenerics {
35+
private final Gson gson;
36+
37+
public GsonSpeaker(Gson gson) {
38+
this.gson = gson;
39+
}
40+
41+
public GsonSpeaker() {
42+
this.gson = new Gson();
43+
}
44+
45+
/**
46+
* {@inheritDoc}
47+
*/
48+
@Override public String toJson(Object src) {
49+
return gson.toJson(src);
50+
}
51+
52+
/**
53+
* {@inheritDoc}
54+
*/
55+
@Override public String toJson(Object src, Type typeOfSrc) {
56+
return gson.toJson(src, typeOfSrc);
57+
}
58+
59+
/**
60+
* {@inheritDoc}
61+
*/
62+
@Override public <T> T fromJson(String json, Class<T> classOfT) throws RuntimeException {
63+
return gson.fromJson(json, classOfT);
64+
}
65+
66+
/**
67+
* {@inheritDoc}
68+
*/
69+
@Override public <T> T fromJson(String json, Type typeOfT) throws RuntimeException {
70+
return gson.fromJson(json, typeOfT);
71+
}
72+
73+
/**
74+
* {@inheritDoc}
75+
*/
76+
@Override public <T> T fromJson(File file, Class<T> classOfT) throws RuntimeException {
77+
BufferedReader reader = null;
78+
79+
try {
80+
reader = new BufferedReader(new FileReader(file.getAbsoluteFile()));
81+
T object = gson.fromJson(reader, classOfT);
82+
reader.close();
83+
return object;
84+
} catch (IOException e) {
85+
throw new RuntimeException(e);
86+
} finally {
87+
if (reader != null) {
88+
try {
89+
reader.close();
90+
} catch (IOException i) {}
91+
}
92+
}
93+
}
94+
95+
/**
96+
* {@inheritDoc}
97+
*/
98+
@Override public <T> T fromJson(File file, Type typeOfT) throws RuntimeException {
99+
BufferedReader reader = null;
100+
101+
try {
102+
reader = new BufferedReader(new FileReader(file.getAbsoluteFile()));
103+
T object = gson.fromJson(reader, typeOfT);
104+
reader.close();
105+
return object;
106+
} catch (IOException e) {
107+
throw new RuntimeException(e);
108+
} finally {
109+
if (reader != null) {
110+
try {
111+
reader.close();
112+
} catch (IOException i) {}
113+
}
114+
}
115+
}
116+
117+
@Override public GenericArrayType arrayOf(Type componentType) {
118+
return Types.arrayOf(componentType);
119+
}
120+
121+
/**
122+
* {@inheritDoc}
123+
*/
124+
@Override public ParameterizedType newParameterizedType(Type rawType, Type... typeArguments) {
125+
return Types.newParameterizedType(rawType, typeArguments);
126+
}
127+
128+
}

app/src/main/java/com/junnanhao/gank/data/source/GankRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
88
import com.junnanhao.gank.BuildConfig;
99
import com.junnanhao.gank.data.gson.AutoValueGsonFactory;
10+
import com.junnanhao.gank.data.gson.GsonSpeaker;
1011
import com.junnanhao.gank.data.models.Gank;
1112
import com.junnanhao.gank.data.models.Response;
1213
import com.junnanhao.gank.ganks.GankFilterType;
@@ -21,7 +22,6 @@
2122
import io.rx_cache2.EvictProvider;
2223
import io.rx_cache2.Reply;
2324
import io.rx_cache2.internal.RxCache;
24-
import io.victoralbertos.jolyglot.GsonSpeaker;
2525
import okhttp3.OkHttpClient;
2626
import okhttp3.logging.HttpLoggingInterceptor;
2727
import retrofit2.Retrofit;

app/src/main/java/com/junnanhao/gank/ganks/GanksActivity.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
package com.junnanhao.gank.ganks;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45
import android.support.design.widget.Snackbar;
56
import android.support.v7.app.AppCompatActivity;
67
import android.support.v7.widget.Toolbar;
7-
import android.util.Log;
88
import android.view.View;
99
import android.view.Menu;
1010
import android.view.MenuItem;
1111

1212
import com.junnanhao.gank.BuildConfig;
1313
import com.junnanhao.gank.R;
1414
import com.junnanhao.gank.data.source.GankRepository;
15+
import com.junnanhao.gank.ui.AboutActivity;
1516

1617

1718
import butterknife.BindView;
1819
import butterknife.ButterKnife;
1920
import butterknife.OnClick;
20-
import io.reactivex.schedulers.Schedulers;
2121
import timber.log.Timber;
2222

2323
public class GanksActivity extends AppCompatActivity {
@@ -52,15 +52,6 @@ protected void onResume() {
5252
public void submit(View view) {
5353
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
5454
.setAction("Action", null).show();
55-
56-
GankRepository.getInstance(this).getGanks(2017, 2, 22, false)
57-
.filter(predicate -> {
58-
Log.d("SOURCE", predicate.getSource().toString());
59-
return true;
60-
})
61-
.map(responseReply -> responseReply.getData().results())
62-
.subscribeOn(Schedulers.io())
63-
.subscribe(stringListMap -> Log.d("test", stringListMap.toString()), Throwable::printStackTrace);
6455
}
6556

6657
@Override
@@ -77,8 +68,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
7768
// as you specify a parent activity in AndroidManifest.xml.
7869
int id = item.getItemId();
7970

80-
//noinspection SimplifiableIfStatement
81-
if (id == R.id.action_settings) {
71+
if (id == R.id.action_about) {
72+
Intent intent = new Intent(this, AboutActivity.class);
73+
startActivity(intent);
8274
return true;
8375
}
8476

app/src/main/java/com/junnanhao/gank/ganks/GanksAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public void setData(Map<String, List<Gank>> ganks) {
4444
Timber.d("gank: %s", gank);
4545
models.add(new GankEpoxyModel(gank));
4646
});
47+
4748
});
4849
notifyModelsChanged();
4950
}

app/src/main/java/com/junnanhao/gank/ganks/GanksContract.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ interface View extends BaseView<Presenter> {
2727

2828
void showSuccessfullySubmitMessage();
2929

30+
void showNetworkError();
31+
3032
void showLoadingGanksError();
3133

3234
void showNoGank();

app/src/main/java/com/junnanhao/gank/ganks/GanksPresenter.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
package com.junnanhao.gank.ganks;
22

33

4+
import com.google.gson.Gson;
5+
import com.google.gson.GsonBuilder;
6+
import com.google.gson.JsonObject;
7+
import com.google.gson.internal.LinkedTreeMap;
8+
import com.google.gson.reflect.TypeToken;
9+
import com.junnanhao.gank.data.gson.AutoValueGson_AutoValueGsonFactory;
10+
import com.junnanhao.gank.data.models.Gank;
11+
import com.junnanhao.gank.data.models.Response;
412
import com.junnanhao.gank.data.source.GankRepository;
513

614
import java.io.IOException;
15+
import java.lang.reflect.Type;
16+
import java.util.List;
17+
import java.util.Map;
718

819
import io.reactivex.android.schedulers.AndroidSchedulers;
20+
import io.reactivex.annotations.NonNull;
21+
import io.reactivex.functions.Consumer;
922
import io.reactivex.schedulers.Schedulers;
23+
import io.rx_cache2.Reply;
24+
import io.rx_cache2.Source;
1025
import timber.log.Timber;
1126

1227
/**
@@ -47,22 +62,34 @@ public void loadGanks(boolean forceUpdate) {
4762

4863
view.setLoadingIndicator(true);
4964

65+
Gson gson = new GsonBuilder().registerTypeAdapterFactory(new AutoValueGson_AutoValueGsonFactory()).create();
66+
5067
gankRepository.getGanks(2017, 2, 17, forceUpdate)
5168
.subscribeOn(Schedulers.io())
52-
.map(responseReply -> responseReply.getData().results())
69+
.map(responseReply -> {
70+
Timber.d("source from: %s", responseReply.getSource());
71+
if (responseReply.getData().results() instanceof LinkedTreeMap) {
72+
LinkedTreeMap<?, ?> yourMap = (LinkedTreeMap<?, ?>) responseReply.getData().results();
73+
JsonObject jsonObject = gson.toJsonTree(yourMap).getAsJsonObject();
74+
Type type = new TypeToken<Map<String, List<Gank>>>() {
75+
}.getType();
76+
return gson.fromJson(jsonObject, type);
77+
}
78+
return responseReply.getData().results();
79+
})
5380
.observeOn(AndroidSchedulers.mainThread())
5481
.subscribe(data -> view.showGanks(data), throwable -> {
5582
Timber.e(throwable);
56-
if(throwable instanceof IOException){
57-
// todo: prompt network fail
58-
view.showLoadingGanksError();
83+
if (throwable instanceof IOException) {
84+
view.showNetworkError();
5985
}
60-
// todo: permission
86+
view.setLoadingIndicator(false);
6187
view.showLoadingGanksError();
6288
}, () -> {
6389
firstLoad = false;
6490
view.setLoadingIndicator(false);
6591
});
92+
6693
}
6794

6895

app/src/main/java/com/junnanhao/gank/ganks/GanksView.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import butterknife.BindView;
2222
import butterknife.ButterKnife;
23+
import butterknife.OnClick;
2324
import timber.log.Timber;
2425

2526
/**
@@ -29,7 +30,7 @@
2930

3031
public class GanksView extends ScrollChildSwipeRefreshLayout implements GanksContract.View {
3132
@BindView(R.id.ganks) RecyclerView gankListView;
32-
@BindView(R.id.noGank) LinearLayout noGank;
33+
@BindView(R.id.networkError) LinearLayout noGank;
3334

3435
private GanksAdapter mAdapter;
3536
private GanksContract.Presenter mPresenter;
@@ -58,6 +59,11 @@ private void init() {
5859
setOnRefreshListener(() -> mPresenter.loadGanks(false));
5960
}
6061

62+
@OnClick(R.id.networkError)
63+
void refreshGanks() {
64+
mPresenter.loadGanks(true);
65+
}
66+
6167
@Override
6268
public void setLoadingIndicator(boolean active) {
6369
final SwipeRefreshLayout srl = this;
@@ -71,7 +77,6 @@ public void showGanks(Map<String, List<Gank>> ganks) {
7177
showNoGank();
7278
return;
7379
}
74-
7580
mAdapter.setData(ganks);
7681

7782
gankListView.setVisibility(VISIBLE);
@@ -89,6 +94,13 @@ public void showSuccessfullySubmitMessage() {
8994
showMessage(R.string.submit_success);
9095
}
9196

97+
@Override
98+
public void showNetworkError() {
99+
noGank.setVisibility(VISIBLE);
100+
gankListView.setVisibility(GONE);
101+
Timber.e("网络异常");
102+
}
103+
92104

93105
@Override
94106
public void showLoadingGanksError() {

0 commit comments

Comments
 (0)