Skip to content

Commit bd9231b

Browse files
author
Tneciv
committed
enable files cached
1 parent bad49b3 commit bd9231b

File tree

6 files changed

+37
-44
lines changed

6 files changed

+37
-44
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@
4949
android:label="Welcome"
5050
android:parentActivityName=".home.view.MainActivity"
5151
android:theme="@style/AppTheme.NoActionBar">
52-
<meta-data
53-
android:name="android.support.PARENT_ACTIVITY"
54-
android:value="com.tneciv.zhihudaily.home.view.MainActivity" />
5552
</activity>
5653
<activity
5754
android:name=".github.GithubActivity"

app/src/main/java/com/tneciv/zhihudaily/detail/presenter/DetailPresenterCompl.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.tneciv.zhihudaily.detail.presenter;
22

3+
import android.content.Context;
4+
35
import com.google.gson.Gson;
46
import com.tneciv.zhihudaily.api.ZhihuApi;
57
import com.tneciv.zhihudaily.detail.model.ContentEntity;
6-
import com.tneciv.zhihudaily.detail.view.IDeatilView;
8+
import com.tneciv.zhihudaily.utils.CacheUtil;
79
import com.tneciv.zhihudaily.utils.OkhttpUtil;
810

911
import java.io.IOException;
@@ -18,15 +20,15 @@
1820
* Created by Tneciv on 1-16-0016.
1921
*/
2022
public class DetailPresenterCompl implements IDetailPresenter {
21-
private IDeatilView iDeatilView;
23+
private Context mContext;
2224

23-
public DetailPresenterCompl(IDeatilView iDeatilView) {
24-
this.iDeatilView = iDeatilView;
25+
public DetailPresenterCompl(Context context) {
26+
this.mContext = context;
2527
}
2628

2729
@Override
28-
public void requestNewsContent(int id) {
29-
String newsContentUrl = ZhihuApi.getNewsContentUrl(id);
30+
public void requestNewsContent(final int id) {
31+
final String newsContentUrl = ZhihuApi.getNewsContentUrl(id);
3032
Request request = new Request.Builder().get().url(newsContentUrl).build();
3133
OkhttpUtil.getInstance().newCall(request).enqueue(new Callback() {
3234
@Override
@@ -37,6 +39,7 @@ public void onFailure(Call call, IOException e) {
3739
@Override
3840
public void onResponse(Call call, Response response) throws IOException {
3941
String string = response.body().string();
42+
new CacheUtil(mContext).cacheFiles(String.valueOf(id), string);
4043
Gson gson = new Gson();
4144
ContentEntity entity = gson.fromJson(string, ContentEntity.class);
4245
EventBus.getDefault().post(entity);

app/src/main/java/com/tneciv/zhihudaily/detail/view/DetailActivity.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010
import android.support.design.widget.FloatingActionButton;
1111
import android.support.v7.app.AppCompatActivity;
1212
import android.support.v7.widget.Toolbar;
13-
import android.util.Log;
1413
import android.view.Menu;
1514
import android.view.MenuItem;
1615
import android.view.View;
17-
import android.webkit.WebChromeClient;
1816
import android.webkit.WebSettings;
1917
import android.webkit.WebView;
2018
import android.widget.ImageView;
2119
import android.widget.TextView;
2220

2321
import com.daimajia.androidanimations.library.Techniques;
2422
import com.daimajia.androidanimations.library.YoYo;
23+
import com.google.gson.Gson;
2524
import com.squareup.leakcanary.RefWatcher;
2625
import com.squareup.picasso.Picasso;
2726
import com.tneciv.zhihudaily.MyApplication;
2827
import com.tneciv.zhihudaily.R;
2928
import com.tneciv.zhihudaily.detail.model.ContentEntity;
3029
import com.tneciv.zhihudaily.detail.presenter.DetailPresenterCompl;
3130
import com.tneciv.zhihudaily.detail.presenter.IDetailPresenter;
31+
import com.tneciv.zhihudaily.utils.CacheUtil;
3232

3333
import butterknife.Bind;
3434
import butterknife.ButterKnife;
@@ -39,10 +39,10 @@
3939
public class DetailActivity extends AppCompatActivity implements IDeatilView {
4040

4141
private String title;
42-
4342
private int id;
44-
4543
private IDetailPresenter iDetailPresenter;
44+
private boolean noImagesMode;
45+
private boolean nightMode;
4646

4747
@Bind(R.id.toolbar)
4848
Toolbar toolbar;
@@ -59,14 +59,6 @@ public class DetailActivity extends AppCompatActivity implements IDeatilView {
5959
@Bind(R.id.appBarLayout)
6060
AppBarLayout appBarLayout;
6161

62-
63-
private boolean noImagesMode;
64-
65-
private boolean nightMode;
66-
67-
// @Bind(R.id.progress)
68-
// ProgressBar progress;
69-
7062
@Override
7163
protected void onCreate(Bundle savedInstanceState) {
7264
super.onCreate(savedInstanceState);
@@ -78,7 +70,14 @@ protected void onCreate(Bundle savedInstanceState) {
7870
nightMode = preferences.getBoolean("dayNightMode", false);
7971
initView();
8072
iDetailPresenter = new DetailPresenterCompl(this);
81-
iDetailPresenter.requestNewsContent(id);
73+
String cache = new CacheUtil(this).loadCache(String.valueOf(id));
74+
if (cache == null || "".equals(cache)) {
75+
iDetailPresenter.requestNewsContent(id);
76+
} else {
77+
Gson gson = new Gson();
78+
ContentEntity entity = gson.fromJson(cache, ContentEntity.class);
79+
showContent(entity);
80+
}
8281
}
8382

8483
@Override
@@ -135,15 +134,6 @@ public void showContent(ContentEntity entity) {
135134
StringBuffer stringBuffer = handleHtml(body);
136135
webView.setDrawingCacheEnabled(true);
137136
webView.loadDataWithBaseURL("file:///android_asset/", stringBuffer.toString(), "text/html", "utf-8", null);
138-
webView.setWebChromeClient(new WebChromeClient() {
139-
140-
@Override
141-
public void onProgressChanged(WebView view, int newProgress) {
142-
super.onProgressChanged(view, newProgress);
143-
Log.d("DetailActivity", "newProgress:" + newProgress);
144-
// progress.setProgress(newProgress);
145-
}
146-
});
147137
}
148138

149139
@NonNull
@@ -157,7 +147,6 @@ private StringBuffer handleHtml(String body) {
157147
}
158148

159149
private void webviewSettings(WebSettings settings) {
160-
settings.setJavaScriptEnabled(true);
161150
settings.setDatabaseEnabled(true);
162151

163152
if (noImagesMode) {
@@ -176,18 +165,18 @@ private void webviewSettings(WebSettings settings) {
176165

177166
@Override
178167
public boolean onCreateOptionsMenu(Menu menu) {
179-
// getMenuInflater().inflate(R.menu.main, menu);
168+
// getMenuInflater().inflate(R.menu.main, menu);
180169
return true;
181170
}
182171

183172
@Override
184173
public boolean onOptionsItemSelected(MenuItem item) {
185174
int itemId = item.getItemId();
186175

187-
// if (itemId == R.id.action_share) {
188-
// share();
189-
// return true;
190-
// }
176+
// if (itemId == R.id.action_share) {
177+
// share();
178+
// return true;
179+
// }
191180

192181
return super.onOptionsItemSelected(item);
193182
}

app/src/main/java/com/tneciv/zhihudaily/theme/presenter/ThemePresenterCompl.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.tneciv.zhihudaily.theme.presenter;
22

3+
import android.content.Context;
4+
35
import com.google.gson.Gson;
46
import com.google.gson.JsonElement;
57
import com.google.gson.JsonParser;
@@ -9,7 +11,7 @@
911
import com.tneciv.zhihudaily.costants.ErrorEntity;
1012
import com.tneciv.zhihudaily.theme.model.ThemeEntity;
1113
import com.tneciv.zhihudaily.theme.model.ThemeResultEntity;
12-
import com.tneciv.zhihudaily.theme.view.IThemeView;
14+
import com.tneciv.zhihudaily.utils.CacheUtil;
1315
import com.tneciv.zhihudaily.utils.OkhttpUtil;
1416

1517
import java.io.IOException;
@@ -27,10 +29,11 @@
2729
* Created by Tneciv on 1-31-0031.
2830
*/
2931
public class ThemePresenterCompl implements IThemePresenter {
30-
private IThemeView iThemeView;
3132

32-
public ThemePresenterCompl(IThemeView iThemeView) {
33-
this.iThemeView = iThemeView;
33+
private Context mContext;
34+
35+
public ThemePresenterCompl(Context context) {
36+
this.mContext = context;
3437
}
3538

3639
@Override
@@ -46,14 +49,15 @@ public void onFailure(Call call, IOException e) {
4649
@Override
4750
public void onResponse(Call call, Response response) throws IOException {
4851
String string = response.body().string();
52+
new CacheUtil(mContext).cacheFiles(url, string);
4953
handleResponse(string, url);
5054
}
5155
});
5256
}
5357

5458
private void handleResponse(String response, String url) {
5559
Gson gson = new Gson();
56-
if (url == ZhihuApi.THEME_LIST) {
60+
if (url.equals(ZhihuApi.THEME_LIST)) {
5761
Type type = new TypeToken<List<ThemeEntity>>() {
5862
}.getType();
5963
List<ThemeEntity> themeEntities = new ArrayList<>();

app/src/main/java/com/tneciv/zhihudaily/theme/view/ThemeFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public ThemeFragment() {
3838
@Override
3939
public void init() {
4040
boolean isNightMode = config.getBoolean("dayNightMode", false);
41-
iThemePresenter = new ThemePresenterCompl(this);
41+
iThemePresenter = new ThemePresenterCompl(getContext());
4242
adapter = new ThemeRecyclerAdapter(getContext(), entities, isNightMode);
4343
recyclerView.setAdapter(adapter);
4444
}

app/src/main/java/com/tneciv/zhihudaily/utils/CacheUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class CacheUtil {
1919
public static final int APP_VERSION = 1;
2020
public static final int VALUE_COUNT = 1;
21-
public static final long MAX_SIZE = 10 * 1024 * 1024;
21+
public static final long MAX_SIZE = 30 * 1024 * 1024;
2222

2323
private Context mContext;
2424

0 commit comments

Comments
 (0)