@@ -57,55 +57,137 @@ public ContentDetailPresenterImpl(ContentDetailContract.View view) {
57
57
58
58
@ Override
59
59
protected void onStart () {
60
- final ContentEntity contentEntity = getView ().getContentEntity ();
60
+ this .loadBlogLocalStatus ();
61
+ final ContentEntity contentEntity = getView ().getContentEntity (); // 内容实体
62
+ final BlogBean blog = ContentEntityConverter .convertToBlog (contentEntity ); // 博客实体
63
+ Observable <String > observable ; // 内容
64
+
65
+ if (isWIFI ()) {
66
+ // 有网情况下从网络加载
67
+ observable = createNetworkContentObservable ();
68
+ } else {
69
+ // 无网情况情况加载本地
70
+ observable = createLocalContentObservable ();
71
+ }
72
+
73
+ // 无图模式处理
74
+ observable = withNotImageMode (observable );
75
+
76
+ // 内容转换成实体
77
+ Observable <BlogBean > blogObservable = observable .map (new Function <String , BlogBean >() {
78
+ @ Override
79
+ public BlogBean apply (String content ) {
80
+ blog .setContent (content );
81
+ return blog ;
82
+ }
83
+ });
84
+
61
85
// 请求内容
62
- AndroidObservable .create (fetchContentSource ())
63
- .with (this )
64
- // 离线模式下处理图片
65
- .map (new Function <String , String >() {
66
- @ Override
67
- public String apply (String content ) throws Exception {
68
- // 网络不可用、WIFI情况、 关闭智能无图模式
69
- if (!networkIsOk () || isWIFI () || !mAppConfig .disableBlogImage ())
70
- return content ;
71
-
72
- Document document = Jsoup .parse (content );
73
- Elements elements = document .select ("img" );
74
- for (Element element : elements ) {
75
- element .attr ("src" , "file:///android_asset/images/placeholder.png" );
76
- }
86
+ AndroidObservable .create (blogObservable ).with (this ).subscribe (new ApiDefaultObserver <BlogBean >() {
87
+ @ Override
88
+ protected void onError (String message ) {
89
+ getView ().onLoadDataFailed (message );
90
+ }
91
+
92
+ @ Override
93
+ protected void accept (BlogBean content ) {
94
+ updateLocalBlogStatus (contentEntity );
95
+ getView ().onLoadDataSuccess (content , AppGson .toJson (content ));
96
+ }
97
+ });
98
+ }
77
99
78
- return document .html ();
79
- }
80
- })
81
- .map (new Function <String , BlogBean >() {
82
- @ Override
83
- public BlogBean apply (String content ) {
84
- // 从数据库查询,转换为博客对象
85
- BlogBean data = DbFactory .getInstance ().getBlog ().getBlog (contentEntity .getId ());
86
- if (data == null ) {
87
- // 没有查询到博客,可能没保存成功,或者缓存已经删除
88
- data = ContentEntityConverter .convertToBlog (contentEntity );
89
- }
90
- data .setContent (content ); // 设置博客内容
91
- return data ;
92
- }
93
- })
94
- // 没有找到博客的时候返回
95
- .subscribe (new ApiDefaultObserver <BlogBean >() {
96
- @ Override
97
- protected void onError (String message ) {
98
- getView ().onLoadDataFailed (message );
99
- }
100
100
101
+ /**
102
+ * 自动判断无图模式
103
+ */
104
+ private Observable <String > withNotImageMode (Observable <String > observable ) {
105
+ return observable .map (new Function <String , String >() {
106
+ @ Override
107
+ public String apply (String content ) throws Exception {
108
+ // 网络不可用、WIFI情况、 关闭智能无图模式
109
+ if (!networkIsOk () || isWIFI () || !mAppConfig .disableBlogImage ())
110
+ return content ;
111
+
112
+ Document document = Jsoup .parse (content );
113
+ Elements elements = document .select ("img" );
114
+ for (Element element : elements ) {
115
+ element .attr ("src" , "file:///android_asset/images/placeholder.png" );
116
+ }
117
+
118
+ return document .html ();
119
+ }
120
+ });
121
+ }
122
+
123
+
124
+ /**
125
+ * 读取本地的博客内容
126
+ *
127
+ * @return
128
+ */
129
+ private Observable <String > createLocalContentObservable () {
130
+ return Observable
131
+ .just (getView ().getContentEntity ())
132
+ .subscribeOn (Schedulers .newThread ())
133
+ .map (new Function <ContentEntity , String >() {
101
134
@ Override
102
- protected void accept (BlogBean content ) {
103
- updateLocalBlogStatus (contentEntity );
104
- getView ().onLoadDataSuccess (content , AppGson .toJson (content ));
135
+ public String apply (ContentEntity contentEntity ) throws Exception {
136
+ Log .i ("rae" , "读取本地内容!" );
137
+ // 根据ID和类型获取博文内容
138
+ String content = DbFactory .getInstance ().getBlog ().getBlogContent (contentEntity .getType (), contentEntity .getId ());
139
+ if (TextUtils .isEmpty (content )) return null ;
140
+ return content ;
105
141
}
106
142
});
143
+ }
107
144
108
- this .loadBlogLocalStatus ();
145
+ /**
146
+ * 读取网络的博客内容
147
+ *
148
+ * @return
149
+ */
150
+ private Observable <String > createNetworkContentObservable () {
151
+
152
+ /*
153
+ * 内容加载顺序
154
+ * 1. 接口获取
155
+ * 2. 网页获取
156
+ * 3. 本地获取
157
+ */
158
+ ContentEntity contentEntity = getView ().getContentEntity ();
159
+
160
+ // 接口获取
161
+ Observable <String > apiObservable = onCreateContentObservable (contentEntity .getId ()).onErrorResumeNext (Observable .<String >empty ());
162
+ // 网页获取
163
+ Observable <String > webObservable = onCreateWebContentObservable (contentEntity .getId ()).onErrorResumeNext (Observable .<String >empty ());
164
+ // 本地获取
165
+ Observable <String > localObservable = createLocalContentObservable ().onErrorResumeNext (Observable .<String >empty ());
166
+
167
+ // 保存到本地数据库
168
+ apiObservable = withSaveLocalContentObservable (apiObservable );
169
+ webObservable = withSaveLocalContentObservable (webObservable );
170
+
171
+ // 关联顺序
172
+ return Observable .concat (apiObservable , webObservable , localObservable ).take (1 );
173
+ }
174
+
175
+
176
+ /**
177
+ * 内容保存到本地
178
+ */
179
+ private Observable <String > withSaveLocalContentObservable (Observable <String > observable ) {
180
+ return observable .map (new Function <String , String >() {
181
+ @ Override
182
+ public String apply (String content ) {
183
+ Log .i ("rae" , "保存内容: " + content .length ());
184
+ // 博文内容接口也没有返回内容
185
+ if (TextUtils .isEmpty (content ))
186
+ return null ;
187
+ updateContent (content ); // 内容保存到本地数据库
188
+ return content ;
189
+ }
190
+ });
109
191
}
110
192
111
193
/**
@@ -324,58 +406,6 @@ protected void accept(Empty empty) {
324
406
325
407
}
326
408
327
-
328
- /**
329
- * 请求数据源
330
- */
331
- private Observable <String > fetchContentSource () {
332
-
333
- String id = getView ().getContentEntity ().getId ();
334
-
335
- // 数据源1: 本地数据库
336
- Observable <String > local = Observable
337
- .just (id )
338
- .subscribeOn (Schedulers .io ())
339
- .map (new Function <String , String >() {
340
- @ Override
341
- public String apply (String id ) {
342
- // 根据ID和类型获取博文内容
343
- String type = getView ().getContentEntity ().getType ();
344
- String content = DbFactory .getInstance ().getBlog ().getBlogContent (type , id );
345
- if (TextUtils .isEmpty (content )) return null ;
346
- Log .i ("rae" ,"读取本地内容!" );
347
- return content ;
348
- }
349
- })
350
- // 返回为空默认返回空的观察者,执行下个观察者
351
- .onErrorResumeNext (Observable .<String >empty ());
352
-
353
- // 数据源2: 网络数据
354
- // 根据不同的博客类型:博客、新闻、知识库来确定网络数据提供者
355
- Observable <String > networkObservable = onCreateContentObservable (id );
356
-
357
- Observable <String > network = networkObservable .map (new Function <String , String >() {
358
- @ Override
359
- public String apply (String content ) {
360
- // 博文内容接口也没有返回内容
361
- if (TextUtils .isEmpty (content ))
362
- return null ;
363
- updateContent (content ); // 博客内容写入本地数据库
364
- Log .i ("rae" ,"读取网络内容!" );
365
- return content ;
366
- }
367
-
368
- }).onErrorResumeNext (Observable .<String >empty ());
369
-
370
-
371
- return onFetchContentSource (local , network );
372
- }
373
-
374
-
375
- protected Observable <String > onFetchContentSource (Observable <String > local , Observable <String > network ) {
376
- return Observable .concat (local , network ).take (1 );
377
- }
378
-
379
409
/**
380
410
* 异步更新博客内容
381
411
*
@@ -422,6 +452,14 @@ public void onComplete() {
422
452
*/
423
453
protected abstract Observable <String > onCreateContentObservable (String id );
424
454
455
+ /**
456
+ * 从原文地址获取内容
457
+ *
458
+ * @param id 内容id
459
+ */
460
+ protected Observable <String > onCreateWebContentObservable (String id ) {
461
+ return Observable .empty ();
462
+ }
425
463
426
464
/**
427
465
* 创建点赞的网络请求
0 commit comments