add interactor to MVP struct.

chengzhenyu лет %!s(int64=8): %!d(string=назад)
Родитель
Сommit
fecd754e3e

+ 16 - 0
app/src/main/java/ai/pai/lensman/base/BaseInteractor.java

@@ -0,0 +1,16 @@
1
+package ai.pai.lensman.base;
2
+
3
+/**
4
+ * Created by chengzhenyu on 2016/8/12.
5
+ */
6
+public interface BaseInteractor {
7
+
8
+    void startJob();
9
+    void cancelJob();
10
+
11
+    interface InteractorListener<T>{
12
+        void onInteractSuccess(T result);
13
+        void onInteractFail(String errorMsg);
14
+    }
15
+
16
+}

+ 6 - 1
app/src/main/java/ai/pai/lensman/login/LoginActivity.java

@@ -2,8 +2,10 @@ package ai.pai.lensman.login;
2 2
 
3 3
 import android.content.Intent;
4 4
 import android.os.Bundle;
5
+import android.text.TextUtils;
5 6
 import android.view.View;
6 7
 import android.widget.EditText;
8
+import android.widget.Toast;
7 9
 
8 10
 import com.android.views.progressbar.ProgressWheel;
9 11
 
@@ -38,7 +40,10 @@ public class LoginActivity extends BaseActivity implements LoginContract.View{
38 40
 
39 41
     @Override
40 42
     public void showLoginHint(String hint) {
41
-
43
+        if(TextUtils.isEmpty(hint)){
44
+            return;
45
+        }
46
+        Toast.makeText(this,hint,Toast.LENGTH_SHORT).show();
42 47
     }
43 48
 
44 49
     @Override

+ 69 - 0
app/src/main/java/ai/pai/lensman/login/LoginInteractor.java

@@ -0,0 +1,69 @@
1
+package ai.pai.lensman.login;
2
+
3
+import android.os.AsyncTask;
4
+
5
+import com.android.common.executors.ThreadExecutor;
6
+
7
+import java.util.HashMap;
8
+
9
+import ai.pai.lensman.base.BaseInteractor;
10
+import ai.pai.lensman.utils.HttpPostTask;
11
+import ai.pai.lensman.utils.UrlContainer;
12
+
13
+/**
14
+ * Created by chengzhenyu on 2016/8/12.
15
+ */
16
+public class LoginInteractor implements BaseInteractor{
17
+
18
+    private HttpPostTask loginTask;
19
+    private String userName;
20
+    private String password;
21
+    private InteractorListener<String> listener;
22
+
23
+    public LoginInteractor(String userName,String password,InteractorListener<String> listener){
24
+        this.userName = userName;
25
+        this.password = password;
26
+        this.listener = listener;
27
+    }
28
+
29
+    @Override
30
+    public void startJob() {
31
+        cancelJob();
32
+        HashMap<String,String> params = new HashMap<>();
33
+        params.put("account",userName);
34
+        params.put("password",password);
35
+
36
+        loginTask = new HttpPostTask(params){
37
+
38
+            @Override
39
+            protected boolean parseResponse(String response) {
40
+                return super.parseResponse(response);
41
+            }
42
+
43
+            @Override
44
+            protected void onPostFail() {
45
+                super.onPostFail();
46
+                listener.onInteractFail("");
47
+            }
48
+
49
+            @Override
50
+            protected void onPostSuccess() {
51
+                super.onPostSuccess();
52
+                listener.onInteractSuccess("");
53
+            }
54
+        };
55
+        loginTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.LOGIN_URL);
56
+    }
57
+
58
+    @Override
59
+    public void cancelJob() {
60
+        if(loginTask == null){
61
+            return;
62
+        }
63
+        if(loginTask.getStatus()== AsyncTask.Status.RUNNING){
64
+            loginTask.cancel(true);
65
+        }
66
+        loginTask = null;
67
+    }
68
+
69
+}

+ 19 - 2
app/src/main/java/ai/pai/lensman/login/LoginPresenter.java

@@ -1,11 +1,14 @@
1 1
 package ai.pai.lensman.login;
2 2
 
3
+import ai.pai.lensman.base.BaseInteractor;
4
+
3 5
 /**
4 6
  * Created by chengzhenyu on 2016/8/11.
5 7
  */
6
-public class LoginPresenter implements LoginContract.Presenter {
8
+public class LoginPresenter implements LoginContract.Presenter,BaseInteractor.InteractorListener<String> {
7 9
 
8 10
     private LoginContract.View view;
11
+    private LoginInteractor interactor;
9 12
 
10 13
     public LoginPresenter(LoginContract.View view){
11 14
         this.view = view;
@@ -14,7 +17,8 @@ public class LoginPresenter implements LoginContract.Presenter {
14 17
     @Override
15 18
     public void login(String userName, String pwd) {
16 19
         view.showProgressView();
17
-
20
+        interactor = new LoginInteractor(userName,pwd,this);
21
+        interactor.startJob();
18 22
     }
19 23
 
20 24
     @Override
@@ -24,7 +28,20 @@ public class LoginPresenter implements LoginContract.Presenter {
24 28
 
25 29
     @Override
26 30
     public void stop() {
31
+        if(interactor!=null){
32
+            interactor.cancelJob();
33
+        }
34
+    }
35
+
27 36
 
37
+    @Override
38
+    public void onInteractSuccess(String result) {
39
+        view.hideProgressView();
28 40
     }
29 41
 
42
+    @Override
43
+    public void onInteractFail(String errorMsg) {
44
+        view.hideProgressView();
45
+        view.showLoginHint(errorMsg);
46
+    }
30 47
 }

+ 53 - 0
app/src/main/java/ai/pai/lensman/utils/HttpPostTask.java

@@ -0,0 +1,53 @@
1
+package ai.pai.lensman.utils;
2
+
3
+import android.os.AsyncTask;
4
+
5
+import com.android.common.http.HttpUtils;
6
+
7
+import java.util.HashMap;
8
+
9
+import ai.pai.lensman.BuildConfig;
10
+
11
+
12
+public  class HttpPostTask extends AsyncTask<String,Integer,Boolean> {
13
+
14
+
15
+    private HashMap<String,String> httpParams;
16
+
17
+    public HttpPostTask(HashMap<String,String> httpParams){
18
+        this.httpParams = httpParams;
19
+    }
20
+
21
+    @Override
22
+    protected Boolean doInBackground(String... params) {
23
+        String rootUrl = params[0];
24
+        if(rootUrl.contains("?")){
25
+            rootUrl+="&platform=android";
26
+        }else{
27
+            rootUrl+="?platform=android";
28
+        }
29
+        rootUrl+="&version="+ BuildConfig.VERSION_NAME;
30
+        rootUrl+="&channel="+"guanwang";
31
+        String response= HttpUtils.doHttpPost(rootUrl,httpParams);
32
+        return parseResponse(response);
33
+    }
34
+
35
+    @Override
36
+    protected void onPostExecute(Boolean result) {
37
+        super.onPostExecute(result);
38
+        if(result){
39
+            onPostSuccess();
40
+        }else{
41
+            onPostFail();
42
+        }
43
+    }
44
+
45
+    protected  boolean parseResponse(String response){return true;}
46
+
47
+    protected void onPostFail(){ }
48
+
49
+    protected  void onPostSuccess(){ }
50
+
51
+
52
+
53
+}