Skip to content

Commit 8023916

Browse files
committed
logout support
1 parent d17595f commit 8023916

File tree

7 files changed

+371
-0
lines changed

7 files changed

+371
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package site.hanschen.runwithyou;
2+
3+
/**
4+
* @author HansChen
5+
*/
6+
public class Const {
7+
8+
public static final String KEY_USERNAME = "KEY_USERNAME";
9+
public static final String KEY_PASSWORD = "KEY_PASSWORD";
10+
}

runner/src/main/java/site/hanschen/runwithyou/application/AuthManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public static AuthManager getInstance() {
2828
private AuthManager() {
2929
}
3030

31+
public void logout() {
32+
email = null;
33+
token = null;
34+
}
35+
3136
public void setAuth(String email, String token) {
3237
this.email = PreconditionUtils.checkNotNull(email);
3338
this.token = PreconditionUtils.checkNotNull(token);

runner/src/main/java/site/hanschen/runwithyou/application/RunnerApplication.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package site.hanschen.runwithyou.application;
22

33

4+
import android.app.Activity;
45
import android.content.ComponentName;
56
import android.content.Context;
67
import android.content.ServiceConnection;
@@ -119,4 +120,11 @@ RunnerManager getRunnerManager() {
119120
}
120121
return mRunnerManager;
121122
}
123+
124+
public void exit() {
125+
for (int i = mActivities.size() - 1; i >= 0; i--) {
126+
Activity activity = mActivities.get(i);
127+
activity.finish();
128+
}
129+
}
122130
}

runner/src/main/java/site/hanschen/runwithyou/ui/home/userinfo/UserInfoActivity.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package site.hanschen.runwithyou.ui.home.userinfo;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
5+
import android.support.annotation.NonNull;
46
import android.support.annotation.Nullable;
57
import android.support.v7.widget.Toolbar;
68
import android.text.TextUtils;
@@ -11,6 +13,8 @@
1113
import android.widget.RadioButton;
1214
import android.widget.RadioGroup;
1315

16+
import com.afollestad.materialdialogs.DialogAction;
17+
import com.afollestad.materialdialogs.MaterialDialog;
1418
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
1519

1620
import java.util.Locale;
@@ -29,10 +33,13 @@
2933
import site.hanschen.api.user.UserCenterApiWrapper;
3034
import site.hanschen.api.user.UserInfo;
3135
import site.hanschen.api.user.UserInfoReply;
36+
import site.hanschen.runwithyou.Const;
3237
import site.hanschen.runwithyou.R;
3338
import site.hanschen.runwithyou.application.AuthManager;
3439
import site.hanschen.runwithyou.application.RunnerApplication;
3540
import site.hanschen.runwithyou.base.RunnerBaseActivity;
41+
import site.hanschen.runwithyou.ui.login.LoginActivity;
42+
import site.hanschen.runwithyou.utils.PreferencesUtils;
3643

3744
/**
3845
* @author HansChen
@@ -175,6 +182,37 @@ public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayO
175182
dpd.show(getFragmentManager(), "DatePickerDialog");
176183
}
177184

185+
private void logout() {
186+
PreferencesUtils.remove(this, Const.KEY_PASSWORD);
187+
AuthManager.getInstance().logout();
188+
RunnerApplication.getInstance().exit();
189+
startActivity(new Intent(UserInfoActivity.this, LoginActivity.class));
190+
}
191+
192+
@OnClick(R.id.user_info_logout)
193+
void onLogoutClick() {
194+
new MaterialDialog.Builder(mContext).title("退出登录")
195+
.content("是否退出登录 ?")
196+
.positiveText("退出")
197+
.onPositive(new MaterialDialog.SingleButtonCallback() {
198+
@Override
199+
public void onClick(@NonNull MaterialDialog dialog,
200+
@NonNull DialogAction which) {
201+
logout();
202+
}
203+
})
204+
.negativeText("取消")
205+
.onNegative(new MaterialDialog.SingleButtonCallback() {
206+
@Override
207+
public void onClick(@NonNull MaterialDialog dialog,
208+
@NonNull DialogAction which) {
209+
dialog.dismiss();
210+
}
211+
})
212+
.build()
213+
.show();
214+
}
215+
178216
private void refreshSaveBtn() {
179217

180218
if (mMenu == null) {

runner/src/main/java/site/hanschen/runwithyou/ui/login/LoginActivity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import site.hanschen.api.user.UserCenterApiWrapper;
2121
import site.hanschen.common.statusbar.StatusBarCompat;
2222
import site.hanschen.common.utils.ResourceUtils;
23+
import site.hanschen.runwithyou.Const;
2324
import site.hanschen.runwithyou.R;
2425
import site.hanschen.runwithyou.application.AuthManager;
2526
import site.hanschen.runwithyou.application.RunnerApplication;
2627
import site.hanschen.runwithyou.base.RunnerBaseActivity;
2728
import site.hanschen.runwithyou.ui.home.HomeActivity;
29+
import site.hanschen.runwithyou.utils.PreferencesUtils;
2830

2931
/**
3032
* @author HansChen
@@ -53,6 +55,15 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5355
.build()
5456
.inject(LoginActivity.this);
5557
setLoginBtnState();
58+
59+
String username;
60+
String password;
61+
if ((username = PreferencesUtils.getString(this, Const.KEY_USERNAME)) != null
62+
&& (password = PreferencesUtils.getString(this, Const.KEY_PASSWORD)) != null) {
63+
doLogin(username, password);
64+
} else if (username != null) {
65+
mUsername.setText(username);
66+
}
5667
}
5768

5869
private void setLoginBtnState() {
@@ -104,6 +115,8 @@ public void onSubscribe(Disposable d) {
104115
public void onNext(LoginReply loginReply) {
105116
dismissWaitingDialog();
106117
if (loginReply.getSucceed()) {
118+
PreferencesUtils.putString(LoginActivity.this, Const.KEY_USERNAME, username);
119+
PreferencesUtils.putString(LoginActivity.this, Const.KEY_PASSWORD, password);
107120
AuthManager.getInstance().setAuth(username, loginReply.getToken());
108121
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
109122
finish();

0 commit comments

Comments
 (0)