e vor 6 Jahren
Ursprung
Commit
b7a7b4ace8

+ 51 - 0
app/src/main/java/com/kuxuan/moneynote/api/ApiHelper.java

@@ -0,0 +1,51 @@
1
+package com.kuxuan.moneynote.api;
2
+
3
+import java.util.concurrent.TimeUnit;
4
+
5
+import okhttp3.OkHttpClient;
6
+import retrofit2.Retrofit;
7
+
8
+/**
9
+ * ApiHelper
10
+ */
11
+public class ApiHelper {
12
+
13
+    private static final String TAG = "ApiHelper";
14
+
15
+    private static ApiHelper mInstance;
16
+    private Retrofit mRetrofit;
17
+    private OkHttpClient mHttpClient;
18
+
19
+    private ApiHelper() {
20
+        this( 30, 30, 30);
21
+    }
22
+
23
+    public ApiHelper( int connTimeout, int readTimeout, int writeTimeout) {
24
+        OkHttpClient.Builder builder = new OkHttpClient.Builder()
25
+                .connectTimeout(connTimeout, TimeUnit.SECONDS)
26
+                .readTimeout(readTimeout, TimeUnit.SECONDS)
27
+                .writeTimeout(writeTimeout, TimeUnit.SECONDS);
28
+
29
+        mHttpClient = builder.build();
30
+    }
31
+
32
+    public static ApiHelper getInstance() {
33
+        if (mInstance == null) {
34
+            mInstance = new ApiHelper();
35
+        }
36
+
37
+        return mInstance;
38
+    }
39
+
40
+    public ApiHelper buildRetrofit() {
41
+        mRetrofit = new Retrofit.Builder()
42
+                .client(mHttpClient)
43
+                .build();
44
+        return this;
45
+    }
46
+
47
+    public <T> T createService(Class<T> serviceClass) {
48
+        return mRetrofit.create(serviceClass);
49
+    }
50
+
51
+}

+ 6 - 0
app/src/main/java/com/kuxuan/moneynote/api/ApiService.java

@@ -36,12 +36,15 @@ import java.util.ArrayList;
36 36
 
37 37
 import io.reactivex.Observable;
38 38
 import okhttp3.MultipartBody;
39
+import okhttp3.ResponseBody;
40
+import retrofit2.Call;
39 41
 import retrofit2.http.Body;
40 42
 import retrofit2.http.GET;
41 43
 import retrofit2.http.Multipart;
42 44
 import retrofit2.http.POST;
43 45
 import retrofit2.http.Part;
44 46
 import retrofit2.http.Query;
47
+import retrofit2.http.Url;
45 48
 
46 49
 /**
47 50
  * Created by Android Studio,网络访问类
@@ -209,6 +212,9 @@ public interface ApiService {
209 212
     Observable<SkinBean> getSkinData();
210 213
 
211 214
 
215
+    @GET
216
+    Call<ResponseBody> downloadPic(@Url String fileUrl);
217
+
212 218
 
213 219
     /**
214 220
      * 移除记账类别

+ 10 - 0
app/src/main/java/com/kuxuan/moneynote/common/Constant.java

@@ -92,6 +92,16 @@ public class Constant {
92 92
 
93 93
         public static final String LOADED = "loaded";
94 94
 
95
+        public static final String JSON = "json";
96
+
97
+        public static final String POSITION="position";
98
+
99
+        public static final String HOMEPICNAME = "HOME";
100
+
101
+        public static final String MINE = "mine";
102
+
103
+        public static final String SKINICON = "skinicon";
104
+
95 105
 
96 106
     }
97 107
 

+ 11 - 0
app/src/main/java/com/kuxuan/moneynote/json/netbody/RES.java

@@ -17,6 +17,17 @@ public class RES {
17 17
     String mine_banner;
18 18
     String preview_img;
19 19
 
20
+    boolean isDownload;
21
+
22
+
23
+    public boolean isDownload() {
24
+        return isDownload;
25
+    }
26
+
27
+    public void setDownload(boolean download) {
28
+        isDownload = download;
29
+    }
30
+
20 31
     public String getBanner() {
21 32
         return banner;
22 33
     }

+ 2 - 1
app/src/main/java/com/kuxuan/moneynote/ui/adapter/SkinAdapter.java

@@ -38,8 +38,9 @@ public class SkinAdapter extends BaseQuickAdapter<RES,BaseViewHolder> {
38 38
         helper.getView(R.id.view).setVisibility(View.VISIBLE);
39 39
         helper.getView(R.id.iv_check).setVisibility(View.GONE);
40 40
 
41
-        int checked = (int) SPUtil.get(mContext, Constant.Skin.CHECKED,-1);
42 41
 
42
+
43
+        int checked = (int) SPUtil.get(mContext, Constant.Skin.CHECKED,-1);
43 44
         if(helper.getPosition() == checked){
44 45
         helper.getView(R.id.iv_check).setVisibility(View.VISIBLE);
45 46
         }

+ 17 - 1
app/src/main/java/com/kuxuan/moneynote/ui/fragments/details/DetialContract.java

@@ -64,6 +64,17 @@ public interface DetialContract {
64 64
     }
65 65
 
66 66
 
67
+    public interface DownloadListener {
68
+        void onStart();
69
+
70
+        void onProgress(int currentLength);
71
+
72
+        void onFinish(String localPath);
73
+
74
+        void onFailure();
75
+    }
76
+
77
+
67 78
     interface DetialModel extends BaseModel {
68 79
 
69 80
         void getDataLists(String year, String month, MVPListener<UserAllBillJson> listMVPListener);
@@ -71,7 +82,9 @@ public interface DetialContract {
71 82
 
72 83
         void getPopWindowData(MVPListener<SkinBean> listMvpListener);
73 84
 
74
-        void getDataListsForDB(String year, String month, HashMap<String,Integer> maps,MVPListener<UserAllBillJson> listMVPListener);
85
+        void getDataListsForDB(String year,String month, HashMap<String,Integer> maps,MVPListener<UserAllBillJson> listMVPListener);
86
+
87
+        void downLoadPic(String url,int size,int position,DownloadListener downloadListener);
75 88
 
76 89
     }
77 90
 
@@ -93,6 +106,9 @@ public interface DetialContract {
93 106
 
94 107
         abstract void InitPopWindow();
95 108
 
109
+
110
+        abstract void downLoadPic(String url,int size,int position);
111
+
96 112
     }
97 113
 
98 114
 }

+ 41 - 29
app/src/main/java/com/kuxuan/moneynote/ui/fragments/details/DetialFragment.java

@@ -4,6 +4,7 @@ import android.content.Intent;
4 4
 import android.graphics.Color;
5 5
 import android.graphics.drawable.ColorDrawable;
6 6
 import android.os.Bundle;
7
+import android.os.Handler;
7 8
 import android.provider.Settings;
8 9
 import android.support.v7.widget.LinearLayoutManager;
9 10
 import android.support.v7.widget.RecyclerView;
@@ -52,9 +53,6 @@ import org.greenrobot.eventbus.EventBus;
52 53
 import org.greenrobot.eventbus.Subscribe;
53 54
 import org.greenrobot.eventbus.ThreadMode;
54 55
 
55
-import java.util.ArrayList;
56
-import java.util.List;
57
-
58 56
 import butterknife.Bind;
59 57
 import butterknife.OnClick;
60 58
 
@@ -363,7 +361,7 @@ public class DetialFragment extends MVPFragment<DetialPresent, DetialModel> impl
363 361
 
364 362
     }
365 363
     ProgressBar progressBar;
366
-
364
+    int j=0;
367 365
     @OnClick(R.id.iv_search)
368 366
     public void searchClick() {
369 367
         Intent intent = new Intent(getActivity(), SearchActivity.class);
@@ -371,7 +369,7 @@ public class DetialFragment extends MVPFragment<DetialPresent, DetialModel> impl
371 369
     }
372 370
 
373 371
     PopupWindow popupWindow;
374
-
372
+    Handler handler;
375 373
     private void showPopWindow() {
376 374
 
377 375
 
@@ -397,7 +395,7 @@ public class DetialFragment extends MVPFragment<DetialPresent, DetialModel> impl
397 395
         recyclerView.setLayoutManager(linearLayoutManager);
398 396
         adapter = new SkinAdapter(R.layout.skinadapter_item);
399 397
         recyclerView.setAdapter(adapter);
400
-
398
+        handler = new Handler();
401 399
         adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
402 400
             @Override
403 401
             public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
@@ -407,32 +405,57 @@ public class DetialFragment extends MVPFragment<DetialPresent, DetialModel> impl
407 405
                     return;
408 406
                 }
409 407
 
410
-                View background = view.findViewById(R.id.view);
408
+                final View background = view.findViewById(R.id.view);
411 409
 
412 410
                 if(background.getVisibility()==View.VISIBLE){
413 411
 
414 412
                     progressBar  = view.findViewById(R.id.progressbar);
415
-                    progressBar.setMax(1000);
413
+                    progressBar.setMax(100);
416 414
                     if(progressBar.getVisibility()==View.GONE){
417 415
                         progressBar.setVisibility(View.VISIBLE);
418 416
                     }
419 417
 
420
-                    for (int i=0;i<1000;i++){
421
-                        progressBar.setProgress(i);
422
-                    }
423
-//                    progressBar.setVisibility(View.GONE);
424
-                    background.setVisibility(View.GONE);
418
+//                    new Thread(new Runnable() {
419
+//                        @Override
420
+//                        public void run() {
421
+//
422
+//                            for (j=0;j<100;j++){
423
+//
424
+//                                handler.post(new Runnable() {
425
+//                                    @Override
426
+//                                    public void run() {
427
+//                                        progressBar.setProgress(j);
428
+//                                    }
429
+//                                });
430
+//
431
+//                                try {
432
+//                                    Thread.sleep(100);
433
+//                                } catch (InterruptedException e) {
434
+//                                    e.printStackTrace();
435
+//                                }
436
+//                            }
437
+//
438
+//                            handler.post(new Runnable() {
439
+//                                @Override
440
+//                                public void run() {
441
+//                                    background.setVisibility(View.GONE);
442
+//                                    progressBar.setVisibility(View.GONE);
443
+//                                }
444
+//                            });
445
+//                        }
446
+//                    }).start();
447
+
448
+                     RES res = (RES) adapter.getData().get(position);
449
+                     mPresenter.downLoadPic(res.getPreview_img(),0,position);
450
+
425 451
                 }else {
426 452
                     SPUtil.putAndApply(getContext(), Constant.Skin.CHECKED,position);
453
+                    SPUtil.putAndApply(getContext(),Constant.Skin.POSITION,position);
427 454
                     adapter.notifyDataSetChanged();
428 455
                     SkinBean  mskinBean = new SkinBean();
429 456
                     mskinBean.setCode(position);
430 457
                     EventBus.getDefault().post(mskinBean);
431 458
                 }
432
-
433
-
434
-
435
-
436 459
             }
437 460
         });
438 461
 
@@ -455,21 +478,10 @@ public class DetialFragment extends MVPFragment<DetialPresent, DetialModel> impl
455 478
         });
456 479
         mPresenter.InitPopWindow();
457 480
 
458
-
459 481
     }
460
-    List<RES> resList;
461 482
     private void setSkinData() {
462 483
 
463
-        resList = new ArrayList<>();
464
-        resList.add(new RES());
465
-        resList.add(new RES());
466
-        resList.add(new RES());
467
-        resList.add(new RES());
468
-        resList.add(new RES());
469
-        resList.add(new RES());
470
-        adapter.setNewData(resList);
471
-
472
-//        adapter.setNewData(skinBean.getRes());
484
+        adapter.setNewData(skinBean.getRes());
473 485
     }
474 486
 
475 487
 

+ 134 - 2
app/src/main/java/com/kuxuan/moneynote/ui/fragments/details/DetialModel.java

@@ -1,5 +1,11 @@
1 1
 package com.kuxuan.moneynote.ui.fragments.details;
2 2
 
3
+import android.text.TextUtils;
4
+import android.util.Log;
5
+
6
+import com.kuxuan.moneynote.MyApplication;
7
+import com.kuxuan.moneynote.api.ApiHelper;
8
+import com.kuxuan.moneynote.api.ApiService;
3 9
 import com.kuxuan.moneynote.api.ExceptionHandle;
4 10
 import com.kuxuan.moneynote.api.MyObsever;
5 11
 import com.kuxuan.moneynote.api.RetrofitClient;
@@ -10,6 +16,12 @@ import com.kuxuan.moneynote.json.netbody.SkinBean;
10 16
 import com.kuxuan.moneynote.json.netbody.UserAllBillBody;
11 17
 import com.kuxuan.moneynote.listener.MVPListener;
12 18
 
19
+import java.io.File;
20
+import java.io.FileNotFoundException;
21
+import java.io.FileOutputStream;
22
+import java.io.IOException;
23
+import java.io.InputStream;
24
+import java.io.OutputStream;
13 25
 import java.util.HashMap;
14 26
 
15 27
 import io.reactivex.ObservableEmitter;
@@ -18,6 +30,12 @@ import io.reactivex.Observer;
18 30
 import io.reactivex.android.schedulers.AndroidSchedulers;
19 31
 import io.reactivex.disposables.Disposable;
20 32
 import io.reactivex.schedulers.Schedulers;
33
+import okhttp3.ResponseBody;
34
+import retrofit2.Call;
35
+import retrofit2.Callback;
36
+import retrofit2.Response;
37
+
38
+import static android.content.ContentValues.TAG;
21 39
 
22 40
 /**
23 41
  * Created by xieshengqi on 2017/10/19.
@@ -53,7 +71,6 @@ public class DetialModel implements DetialContract.DetialModel {
53 71
             public void onError(ExceptionHandle.ResponeThrowable e) {
54 72
 
55 73
 
56
-
57 74
             }
58 75
 
59 76
             @Override
@@ -68,13 +85,15 @@ public class DetialModel implements DetialContract.DetialModel {
68 85
                 }
69 86
 
70 87
 
71
-
72 88
             }
73 89
         });
74 90
 
75 91
 
76 92
     }
77 93
 
94
+
95
+
96
+    Thread mThread;
78 97
     @Override
79 98
     public void getDataListsForDB(final String year, final String month, final HashMap<String, Integer> maps, final MVPListener<UserAllBillJson> listMVPListener) {
80 99
         io.reactivex.Observable.create(new ObservableOnSubscribe<UserAllBillJson>() {
@@ -108,4 +127,117 @@ public class DetialModel implements DetialContract.DetialModel {
108 127
 
109 128
 
110 129
     }
130
+
131
+    String  mVideoPath;
132
+    File mFile;
133
+    @Override
134
+    public void downLoadPic(String url, int size, int position, final DetialContract.DownloadListener downloadListener) {
135
+
136
+        String fileheader = MyApplication.getInstance().getApplicationContext().getFilesDir().getPath()+"/";
137
+
138
+        switch (size){
139
+
140
+            case 0:
141
+
142
+                mVideoPath = fileheader + Constant.Skin.SKINICON+position+".png";
143
+
144
+                break;
145
+
146
+            case 1:
147
+
148
+                mVideoPath = fileheader + Constant.Skin.MINE+position+".png";
149
+
150
+                break;
151
+
152
+            case 2:
153
+
154
+                mVideoPath = fileheader + Constant.Skin.HOMEPICNAME+position+".png";
155
+
156
+                break;
157
+        }
158
+
159
+        if (TextUtils.isEmpty(mVideoPath)) {
160
+            Log.e(TAG, "downloadVideo: 存储路径为空");
161
+            return;
162
+        }
163
+
164
+        //建立一个文件
165
+        mFile = new File(mVideoPath);
166
+
167
+        Call<ResponseBody> mCall= ApiHelper.getInstance().buildRetrofit().createService(ApiService.class).downloadPic(url);
168
+
169
+        mCall.enqueue(new Callback<ResponseBody>() {
170
+            @Override
171
+            public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
172
+
173
+
174
+                //下载文件放在子线程
175
+                mThread = new Thread() {
176
+                    @Override
177
+                    public void run() {
178
+                        super.run();
179
+                        //保存到本地
180
+                        writeFile2Disk(response, mFile, downloadListener);
181
+                    }
182
+                };
183
+                mThread.start();
184
+
185
+            }
186
+
187
+            @Override
188
+            public void onFailure(Call<ResponseBody> call, Throwable t) {
189
+
190
+                downloadListener.onFailure();
191
+
192
+            }
193
+        });
194
+
195
+    }
196
+
197
+
198
+    private void writeFile2Disk(Response<ResponseBody> response, File file, DetialContract.DownloadListener downloadListener) {
199
+        downloadListener.onStart();
200
+        long currentLength = 0;
201
+        OutputStream os = null;
202
+
203
+        InputStream is = response.body().byteStream(); //获取下载输入流
204
+        long totalLength = response.body().contentLength();
205
+
206
+        try {
207
+            os = new FileOutputStream(file); //输出流
208
+            int len;
209
+            byte[] buff = new byte[1024];
210
+            while ((len = is.read(buff)) != -1) {
211
+                os.write(buff, 0, len);
212
+                currentLength += len;
213
+                Log.e(TAG, "当前进度: " + currentLength);
214
+                //计算当前下载百分比,并经由回调传出
215
+                downloadListener.onProgress((int) (100 * currentLength / totalLength));
216
+                //当百分比为100时下载结束,调用结束回调,并传出下载后的本地路径
217
+                if ((int) (100 * currentLength / totalLength) == 100) {
218
+                    downloadListener.onFinish(mVideoPath); //下载完成
219
+                }
220
+            }
221
+        } catch (FileNotFoundException e) {
222
+            e.printStackTrace();
223
+        } catch (IOException e) {
224
+            e.printStackTrace();
225
+        } finally {
226
+            if (os != null) {
227
+                try {
228
+                    os.close(); //关闭输出流
229
+                } catch (IOException e) {
230
+                    e.printStackTrace();
231
+                }
232
+            }
233
+            if (is != null) {
234
+                try {
235
+                    is.close(); //关闭输入流
236
+                } catch (IOException e) {
237
+                    e.printStackTrace();
238
+                }
239
+            }
240
+        }
241
+    }
242
+
111 243
 }

+ 53 - 1
app/src/main/java/com/kuxuan/moneynote/ui/fragments/details/DetialPresent.java

@@ -4,10 +4,13 @@ import android.app.Activity;
4 4
 import android.content.Context;
5 5
 import android.support.v7.widget.LinearLayoutManager;
6 6
 import android.support.v7.widget.RecyclerView;
7
+import android.util.Log;
7 8
 import android.view.View;
8 9
 
9 10
 import com.chad.library.adapter.base.BaseQuickAdapter;
11
+import com.google.gson.Gson;
10 12
 import com.kuxuan.moneynote.R;
13
+import com.kuxuan.moneynote.common.Constant;
11 14
 import com.kuxuan.moneynote.db.CategoryDaoOperator;
12 15
 import com.kuxuan.moneynote.json.BillData;
13 16
 import com.kuxuan.moneynote.json.DetialJson;
@@ -23,6 +26,7 @@ import com.kuxuan.moneynote.ui.weight.LoadBottomView;
23 26
 import com.kuxuan.moneynote.ui.weight.LoaddingHeadView;
24 27
 import com.kuxuan.moneynote.utils.LoginStatusUtil;
25 28
 import com.kuxuan.moneynote.utils.PickerUtil;
29
+import com.kuxuan.moneynote.utils.SPUtil;
26 30
 import com.kuxuan.moneynote.utils.TimeUtlis;
27 31
 import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
28 32
 import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
@@ -336,8 +340,13 @@ public class DetialPresent extends DetialContract.DetialPresent implements View.
336 340
             @Override
337 341
             public void onSuccess(SkinBean content) {
338 342
 
339
-                view.showSkinData(content);
343
+                Gson gson = new Gson();
344
+
345
+                String json = gson.toJson(content.getRes());
346
+
347
+                SPUtil.putAndApply(mContext, Constant.Skin.JSON,json);
340 348
 
349
+                view.showSkinData(content);
341 350
             }
342 351
 
343 352
             @Override
@@ -350,6 +359,49 @@ public class DetialPresent extends DetialContract.DetialPresent implements View.
350 359
 
351 360
     }
352 361
 
362
+    @Override
363
+    void downLoadPic(String url,int size,int position) {
364
+
365
+
366
+
367
+        mModel.downLoadPic(url, size, position, new DetialContract.DownloadListener() {
368
+            @Override
369
+            public void onStart() {
370
+
371
+
372
+                Log.e("allence","开始了");
373
+
374
+            }
375
+
376
+            @Override
377
+            public void onProgress(int currentLength) {
378
+
379
+                Log.e("allence","进度"+currentLength);
380
+
381
+            }
382
+
383
+            @Override
384
+            public void onFinish(String localPath) {
385
+
386
+                Log.e("allence","结束了");
387
+
388
+            }
389
+
390
+            @Override
391
+            public void onFailure() {
392
+
393
+                Log.e("allence","失败了");
394
+
395
+            }
396
+        });
397
+
398
+
399
+
400
+
401
+
402
+
403
+    }
404
+
353 405
 
354 406
     @Override
355 407
     public void onClick(View view) {

+ 43 - 0
app/src/main/java/com/kuxuan/moneynote/utils/SkinUtil.java

@@ -0,0 +1,43 @@
1
+package com.kuxuan.moneynote.utils;
2
+
3
+import com.google.gson.Gson;
4
+import com.google.gson.reflect.TypeToken;
5
+import com.kuxuan.moneynote.MyApplication;
6
+import com.kuxuan.moneynote.common.Constant;
7
+import com.kuxuan.moneynote.json.netbody.RES;
8
+
9
+import java.lang.reflect.Type;
10
+import java.util.List;
11
+
12
+/**
13
+ * Created by Allence on 2018/4/26 0026.
14
+ */
15
+
16
+public class SkinUtil {
17
+
18
+
19
+    /**
20
+     * 记录哪个图片已经下载了
21
+     * @param position
22
+     */
23
+
24
+    public static void markDownload(int position){
25
+
26
+        String json = (String) SPUtil.get(MyApplication.getInstance(), Constant.Skin.JSON,"");
27
+        Gson gson = new Gson();
28
+
29
+        Type type = new TypeToken<List<RES>>(){}.getType();
30
+        List<RES> res = new Gson().fromJson(json, type);
31
+        res.get(position).setDownload(true);
32
+        String json1 = gson.toJson(res);
33
+        SPUtil.putAndApply(MyApplication.getInstance(), Constant.Skin.JSON,json1);
34
+
35
+    }
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+}