Skip to content

Commit

Permalink
Support log out
Browse files Browse the repository at this point in the history
  • Loading branch information
floating-cat committed Nov 23, 2014
1 parent 9e4da45 commit 101d0e6
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 17 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/cl/monsoon/s1next/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public static void setAuthenticityToken(String authenticityToken) {
INSTANCE.authenticityToken = authenticityToken;
}

public static String clearUserInfo() {
return INSTANCE.username = INSTANCE.uid = INSTANCE.authenticityToken = null;
}

public static int getTheme() {
return INSTANCE.theme;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package cl.monsoon.s1next.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Point;
Expand All @@ -24,6 +27,7 @@
import cl.monsoon.s1next.Api;
import cl.monsoon.s1next.Config;
import cl.monsoon.s1next.R;
import cl.monsoon.s1next.singleton.MyOkHttpClient;

/**
* An abstract Activity to create a navigation drawer.
Expand All @@ -37,10 +41,12 @@ public abstract class AbsNavigationDrawerActivity extends AbsThemeActivity {
ActionBarDrawerToggle mDrawerToggle;

private View mDrawer;
private TextView mDrawerUsernameView;

private ActionMenuView mActionMenuView;
private CharSequence mTitle;

private boolean showLogin = false;
private LoginStatus mLoginStatus = LoginStatus.NONE;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -52,18 +58,10 @@ protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(mToolbar);

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

mDrawer = findViewById(R.id.drawer);
Glide.with(this)
.load(R.drawable.ic_avatar_placeholder)
.transform(new CenterCrop(Glide.get(this).getBitmapPool()))
.into((ImageView) mDrawer.findViewById(R.id.drawer_avatar));
mDrawer.findViewById(R.id.drawer_header).setOnClickListener(v -> {
mDrawerLayout.closeDrawer(mDrawer);
mDrawerUsernameView = (TextView) mDrawer.findViewById(R.id.drawer_username);

Intent intent = new Intent(AbsNavigationDrawerActivity.this, LoginActivity.class);
startActivity(intent);
});
showLoginPrompt();

TextView textView = (TextView) mDrawer.findViewById(R.id.settings);
textView.setText(getText(R.string.settings));
Expand Down Expand Up @@ -126,6 +124,7 @@ public void onDrawerClosed(View drawerView) {
protected void onResume() {
super.onResume();

showLoginPrompt();
showUserInfo();
}

Expand Down Expand Up @@ -201,21 +200,88 @@ void restoreToolbar() {
}
}

/**
* Show default avatar and login prompt.
*/
private void showLoginPrompt() {
if (mDrawerLayout == null || mDrawer == null || mDrawerUsernameView == null) {
throw new IllegalStateException("Some views must not be null.");
}

if (TextUtils.isEmpty(Config.getUsername()) && mLoginStatus != LoginStatus.NOT) {
mLoginStatus = LoginStatus.NOT;

Glide.with(this)
.load(R.drawable.ic_avatar_placeholder)
.transform(new CenterCrop(Glide.get(this).getBitmapPool()))
.into((ImageView) mDrawer.findViewById(R.id.drawer_avatar));
mDrawer.findViewById(R.id.drawer_header).setOnClickListener(v -> {
mDrawerLayout.closeDrawer(mDrawer);

Intent intent = new Intent(AbsNavigationDrawerActivity.this, LoginActivity.class);
startActivity(intent);
});
mDrawerUsernameView.setText(R.string.action_login);
}
}

/**
* Show username and its avatar when user logged in.
*/
public void showUserInfo() {
if (!TextUtils.isEmpty(Config.getUsername()) && !showLogin) {
showLogin = true;
if (mDrawer == null || mDrawerUsernameView == null) {
throw new IllegalStateException("Some views must not be null.");
}

((TextView) mDrawer.findViewById(R.id.drawer_username)).setText(Config.getUsername());
mDrawer.findViewById(R.id.drawer_header).setOnClickListener(null);
if (!TextUtils.isEmpty(Config.getUsername()) && mLoginStatus != LoginStatus.LOGIN) {
mLoginStatus = LoginStatus.LOGIN;

Glide.with(this)
.load(Api.getUrlAvatarMedium(Config.getUid()))
.error(R.drawable.ic_avatar_placeholder)
.transform(new CenterCrop(Glide.get(this).getBitmapPool()))
.into((ImageView) mDrawer.findViewById(R.id.drawer_avatar));
mDrawerUsernameView.setText(Config.getUsername());

mDrawer.findViewById(R.id.drawer_header).setOnClickListener(v ->
new LogOutDialog().show(getFragmentManager(), LogOutDialog.TAG));
}
}

public void logout() {
MyOkHttpClient.clearCookie();
Config.clearUserInfo();

showLoginPrompt();
}

private enum LoginStatus {
NONE, NOT, LOGIN
}

public static class LogOutDialog extends DialogFragment {

private static final String TAG = "log_out_dialog";

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

return
new AlertDialog.Builder(getActivity())
.setMessage(R.string.dialog_progress_log_out)
.setPositiveButton(android.R.string.ok,
(dialog, which) -> {
try {
((AbsNavigationDrawerActivity) getActivity()).logout();
} catch (ClassCastException e) {
throw new ClassCastException(
getActivity()
+ " must extend AbsNavigationDrawerActivity.");
}
})
.setNegativeButton(
android.R.string.cancel, null)
.create();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum MyOkHttpClient {
INSTANCE;

private final OkHttpClient okHttpClient;
private final CookieManager cookieManager;

private MyOkHttpClient() {
okHttpClient = new OkHttpClient();
Expand All @@ -24,7 +25,7 @@ private MyOkHttpClient() {
okHttpClient.setWriteTimeout(10, TimeUnit.SECONDS);
okHttpClient.setReadTimeout(30, TimeUnit.SECONDS);

CookieManager cookieManager = new CookieManager(
cookieManager = new CookieManager(
new PersistentHttpCookieStore(
MyApplication.getContext()), CookiePolicy.ACCEPT_ALL);

Expand All @@ -34,4 +35,8 @@ private MyOkHttpClient() {
public static OkHttpClient get() {
return INSTANCE.okHttpClient;
}

public static boolean clearCookie() {
return INSTANCE.cookieManager.getCookieStore().removeAll();
}
}
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_navigation_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
android:gravity="center_vertical"
android:includeFontPadding="false"
android:singleLine="true"
android:text="@string/action_login"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>

</LinearLayout>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<resources>
<string name="action_login">登录</string>
<string name="avatar_content_desc">头像</string>
<string name="dialog_progress_log_out">退出登录?</string>
<string name="dialog_progress_message_back">要舍弃这条信息吗?</string>
<string name="dialog_progress_title_login">登录中…</string>
<string name="dialog_progress_title_reply">回复中…</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<string name="send_content_desc">Send</string>

<!-- drawer -->
<string name="dialog_progress_log_out">Sign out?</string>
<string name="settings">Settings</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
Expand Down

0 comments on commit 101d0e6

Please sign in to comment.