26
26
* # #
27
27
*/
28
28
29
- package com .jiang .android .rxjavaapp ;
30
-
31
- import android .app .IntentService ;
32
- import android .content .Intent ;
33
- import android .widget .Toast ;
29
+ package com .jiang .android .rxjavaapp .utils ;
34
30
35
31
import com .jiang .android .rxjavaapp .common .CommonString ;
36
32
import com .jiang .android .rxjavaapp .common .OperatorsUrl ;
37
- import com .jiang .android .rxjavaapp .common .SPKey ;
38
33
import com .jiang .android .rxjavaapp .database .alloperators ;
39
34
import com .jiang .android .rxjavaapp .database .helper .DbUtil ;
40
35
import com .jiang .android .rxjavaapp .database .operators ;
41
- import com .jiang .android .rxjavaapp .utils .L ;
42
- import com .jiang .android .rxjavaapp .utils .SharePrefUtil ;
43
36
44
37
import java .util .ArrayList ;
45
38
import java .util .List ;
46
39
47
- import de .greenrobot .event .EventBus ;
48
- import rx .Observable ;
49
- import rx .Subscriber ;
40
+ import rx .Single ;
41
+ import rx .SingleSubscriber ;
50
42
import rx .android .schedulers .AndroidSchedulers ;
51
43
import rx .schedulers .Schedulers ;
52
44
53
45
/**
54
46
* Created by jiang on 16/3/13.
55
47
*/
56
- public class InitDataService extends IntentService {
57
-
58
- public InitDataService () {
59
- super ("InitDataService" );
60
- }
61
-
62
- long i = 1l ;
63
- long parentId = 1l ;
64
-
65
- @ Override
66
- protected void onHandleIntent (Intent intent ) {
48
+ public class DataUtils {
49
+ static long i = 1l ;
50
+ static long parentId = 1l ;
67
51
68
- Observable .create (new Observable .OnSubscribe <Boolean >() {
52
+ public static void fillData (final callBack call ) {
53
+ Single .create (new Single .OnSubscribe <Boolean >() {
69
54
@ Override
70
- public void call (Subscriber <? super Boolean > subscriber ) {
71
-
55
+ public void call (SingleSubscriber <? super Boolean > singleSubscriber ) {
72
56
try {
73
57
List <operators > lists = getOperatorsData ();
74
58
List <alloperators > alloperatorses = getAllOperators ();
75
59
DbUtil .getOperatorsService ().save (lists );
76
60
DbUtil .getAllOperatorsService ().save (alloperatorses );
77
- subscriber .onNext (true );
78
- subscriber .onCompleted ();
61
+ singleSubscriber .onSuccess (true );
79
62
} catch (Exception e ) {
80
- subscriber .onError (e );
63
+ singleSubscriber .onError (e );
81
64
}
82
65
}
83
- }).subscribeOn (Schedulers .io ()).observeOn (AndroidSchedulers .mainThread ()).subscribe (new Subscriber <Boolean >() {
84
- @ Override
85
- public void onCompleted () {
86
- Toast .makeText (InitDataService .this , "数据库初始化成功" , Toast .LENGTH_SHORT ).show ();
87
- SharePrefUtil .saveBoolean (InitDataService .this , SPKey .FIRST_ENTER , false );
88
- EventBus .getDefault ().post (1 );
89
-
90
- }
91
-
92
- @ Override
93
- public void onError (Throwable e ) {
94
- Toast .makeText (InitDataService .this , "保存数据失败,请退出重试,错误信息:" + e .getMessage (), Toast .LENGTH_SHORT ).show ();
95
- L .e (e .getMessage ());
96
- }
66
+ }).subscribeOn (Schedulers .io ())
67
+ .observeOn (AndroidSchedulers .mainThread ())
68
+ .subscribe (new SingleSubscriber <Boolean >() {
69
+ @ Override
70
+ public void onSuccess (Boolean value ) {
71
+ call .onSuccess ();
72
+ }
73
+
74
+ @ Override
75
+ public void onError (Throwable error ) {
76
+ call .onFail (error );
77
+ }
78
+ });
97
79
98
- @ Override
99
- public void onNext (Boolean operatorss ) {
100
-
101
- }
102
- });
103
80
}
104
81
105
82
106
- public List <operators > getOperatorsData () {
83
+ public static List <operators > getOperatorsData () {
107
84
List <operators > lists = new ArrayList <>();
108
85
lists .add (new operators (1l , "RxJava介绍" , 1l ));
109
86
lists .add (new operators (2l , "Creating" , 2l ));
@@ -122,7 +99,7 @@ public List<operators> getOperatorsData() {
122
99
return lists ;
123
100
}
124
101
125
- public List <alloperators > getAllOperators () {
102
+ public static List <alloperators > getAllOperators () {
126
103
127
104
List <alloperators > alloperatorses = new ArrayList <>();
128
105
getIntroduceList (alloperatorses );
@@ -137,7 +114,7 @@ public List<alloperators> getAllOperators() {
137
114
}
138
115
139
116
140
- private void getStringList (List <alloperators > alloperatorses ) {
117
+ private static void getStringList (List <alloperators > alloperatorses ) {
141
118
alloperatorses .add (new alloperators (i ++, "byLine()" , "将一个字符串的Observable转换为一个行序列的Observable,这个Observable将原来的序列当做流处理,然后按换行符分割" , CommonString .byLine , OperatorsUrl .byLine , parentId ));
142
119
alloperatorses .add (new alloperators (i ++, "decode()" , "将一个多字节的字符流转换为一个Observable,它按字符边界发射字节数组" , CommonString .decode , OperatorsUrl .decode , parentId ));
143
120
alloperatorses .add (new alloperators (i ++, "encode()" , "对一个发射字符串的Observable执行变换操作,变换后的Observable发射一个在原始字符串中表示多字节字符边界的字节数组" , CommonString .encode , OperatorsUrl .encode , parentId ));
@@ -149,7 +126,7 @@ private void getStringList(List<alloperators> alloperatorses) {
149
126
}
150
127
151
128
152
- private void getUtilityList (List <alloperators > alloperatorses ) {
129
+ private static void getUtilityList (List <alloperators > alloperatorses ) {
153
130
alloperatorses .add (new alloperators (i ++, "Materialize()" , "将Observable转换成一个通知列表convert an Observable into a list of Notifications" , CommonString .Materialize , OperatorsUrl .Materialize , parentId ));
154
131
alloperatorses .add (new alloperators (i ++, "Dematerialize()" , "将上面的结果逆转回一个Observable" , CommonString .Dematerialize , OperatorsUrl .Dematerialize , parentId ));
155
132
alloperatorses .add (new alloperators (i ++, "Timestamp()" , "给Observable发射的每个数据项添加一个时间戳" , CommonString .Timestamp , OperatorsUrl .Timestamp , parentId ));
@@ -173,7 +150,7 @@ private void getUtilityList(List<alloperators> alloperatorses) {
173
150
}
174
151
175
152
176
- private void getErrorList (List <alloperators > alloperatorses ) {
153
+ private static void getErrorList (List <alloperators > alloperatorses ) {
177
154
alloperatorses .add (new alloperators (i ++, "onErrorResumeNext()" , "指示Observable在遇到错误时发射一个数据序列" , CommonString .EMPTY , OperatorsUrl .ERROR , parentId ));
178
155
alloperatorses .add (new alloperators (i ++, "onErrorReturn()" , "指示Observable在遇到错误时发射一个特定的数据" , CommonString .EMPTY , OperatorsUrl .ERROR , parentId ));
179
156
alloperatorses .add (new alloperators (i ++, "onExceptionResumeNext()" , "instructs an Observable to continue emitting items after it encounters an exception (but not another variety of throwable)指示Observable遇到错误时继续发射数据" , CommonString .EMPTY , OperatorsUrl .ERROR , parentId ));
@@ -183,7 +160,7 @@ private void getErrorList(List<alloperators> alloperatorses) {
183
160
}
184
161
185
162
186
- private void getCombinList (List <alloperators > alloperatorses ) {
163
+ private static void getCombinList (List <alloperators > alloperatorses ) {
187
164
alloperatorses .add (new alloperators (i ++, "startWith()" , "在数据序列的开头增加一项数据" , CommonString .STARTWITH , OperatorsUrl .STARTWITH , parentId ));
188
165
alloperatorses .add (new alloperators (i ++, "merge()" , "将多个Observable合并为一个" , CommonString .MERGE , OperatorsUrl .MERGE , parentId ));
189
166
alloperatorses .add (new alloperators (i ++, "mergeDelayError()" , "合并多个Observables,让没有错误的Observable都完成后再发射错误通知" , CommonString .MERGEDELAY , OperatorsUrl .MERGEDELAY , parentId ));
@@ -195,7 +172,7 @@ private void getCombinList(List<alloperators> alloperatorses) {
195
172
parentId ++;
196
173
}
197
174
198
- private void getIntroduceList (List <alloperators > alloperatorses ) {
175
+ private static void getIntroduceList (List <alloperators > alloperatorses ) {
199
176
alloperatorses .add (new alloperators (i ++, "ReactiveX" , "什么是Rx,Rx的理念和优势" , CommonString .SPLASH_INDEX_URL , OperatorsUrl .INTRODUCE , parentId ));
200
177
alloperatorses .add (new alloperators (i ++, "Observables" , "简要介绍Observable的观察者模型" , CommonString .OBSERVABLES , OperatorsUrl .OBSERVABLES , parentId ));
201
178
alloperatorses .add (new alloperators (i ++, "Single" , "一种特殊的只发射单个值的Observable" , CommonString .SPLASH_INDEX_URL , OperatorsUrl .SINGLE , parentId ));
@@ -204,7 +181,7 @@ private void getIntroduceList(List<alloperators> alloperatorses) {
204
181
parentId ++;
205
182
}
206
183
207
- private void getCreatingList (List <alloperators > alloperatorses ) {
184
+ private static void getCreatingList (List <alloperators > alloperatorses ) {
208
185
209
186
alloperatorses .add (new alloperators (i ++, "just()" , "将一个或多个对象转换成发射这个或这些对象的一个Observable" , CommonString .JUST , OperatorsUrl .JUST , parentId ));
210
187
alloperatorses .add (new alloperators (i ++, "from()" , "将一个Iterable, 一个Future, 或者一个数组转换成一个Observable" , CommonString .FROM , OperatorsUrl .FROM , parentId ));
@@ -221,7 +198,7 @@ private void getCreatingList(List<alloperators> alloperatorses) {
221
198
parentId ++;
222
199
}
223
200
224
- private void getTransformList (List <alloperators > alloperatorses ) {
201
+ private static void getTransformList (List <alloperators > alloperatorses ) {
225
202
alloperatorses .add (new alloperators (i ++, "map()" , "对序列的每一项都应用一个函数来变换Observable发射的数据序列" , CommonString .MAP , OperatorsUrl .MAP , parentId ));
226
203
alloperatorses .add (new alloperators (i ++, "flatMap()" , "将Observable发射的数据集合变换为Observables集合,然后将这些Observable发射的数据平坦化的放进一个单独的Observable" , CommonString .FLATMAP , OperatorsUrl .FLATMAP , parentId ));
227
204
alloperatorses .add (new alloperators (i ++, "concatMap()" , "将Observable发射的数据集合变换为Observables集合,然后将这些Observable发射的数据平坦化的放进一个单独的Observable" , CommonString .CONTACTMAP , OperatorsUrl .CONTACTMAP , parentId ));
@@ -234,7 +211,7 @@ private void getTransformList(List<alloperators> alloperatorses) {
234
211
parentId ++;
235
212
}
236
213
237
- private void getFilterList (List <alloperators > alloperatorses ) {
214
+ private static void getFilterList (List <alloperators > alloperatorses ) {
238
215
alloperatorses .add (new alloperators (i ++, "filter()" , "过滤数据" , CommonString .FILTER , OperatorsUrl .FILTER , parentId ));
239
216
alloperatorses .add (new alloperators (i ++, "takeLast()" , "只发射最后的N项数据" , CommonString .TAKE_LAST , OperatorsUrl .TAKE_LAST , parentId ));
240
217
alloperatorses .add (new alloperators (i ++, "last()" , "只发射最后的一项数据" , CommonString .LAST , OperatorsUrl .LAST , parentId ));
@@ -257,4 +234,12 @@ private void getFilterList(List<alloperators> alloperatorses) {
257
234
alloperatorses .add (new alloperators (i ++, "ignoreElements()" , "丢弃所有的正常数据,只发射错误或完成通知" , CommonString .IGNORE_ELEMENT , OperatorsUrl .IGNORE_ELEMENT , parentId ));
258
235
parentId ++;
259
236
}
237
+
238
+
239
+ public interface callBack {
240
+ void onSuccess ();
241
+
242
+ void onFail (Throwable e );
243
+
244
+ }
260
245
}
0 commit comments