Add drawer to main activity

This commit is contained in:
Eugen Rochko 2017-03-09 19:31:15 +01:00
parent 9172636127
commit 7fa01371d9
9 changed files with 168 additions and 91 deletions

View file

@ -37,4 +37,7 @@ dependencies {
compile 'com.mikhaellopez:circularfillableloaders:1.2.0' compile 'com.mikhaellopez:circularfillableloaders:1.2.0'
compile 'com.squareup.retrofit2:retrofit:2.2.0' compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile('com.mikepenz:materialdrawer:5.8.2@aar') {
transitive = true
}
} }

View file

@ -21,6 +21,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -35,6 +36,7 @@ import android.transition.TransitionInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.ImageView;
import com.android.volley.AuthFailureError; import com.android.volley.AuthFailureError;
import com.android.volley.Request; import com.android.volley.Request;
@ -42,6 +44,18 @@ import com.android.volley.Response;
import com.android.volley.VolleyError; import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.JsonObjectRequest;
import com.keylesspalace.tusky.entity.Account; import com.keylesspalace.tusky.entity.Account;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;
import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader;
import com.mikepenz.materialdrawer.util.DrawerImageLoader;
import com.squareup.picasso.Picasso;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -63,6 +77,8 @@ public class MainActivity extends BaseActivity {
private String loggedInAccountUsername; private String loggedInAccountUsername;
Stack<Integer> pageHistory = new Stack<Integer>(); Stack<Integer> pageHistory = new Stack<Integer>();
private ViewPager viewPager; private ViewPager viewPager;
private AccountHeader headerResult;
private Drawer drawer;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -84,6 +100,88 @@ public class MainActivity extends BaseActivity {
} }
}); });
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withSelectionListEnabledForSingleProfile(false)
.withTranslucentStatusBar(true)
.withCompactStyle(true)
.withOnAccountHeaderProfileImageListener(new AccountHeader.OnAccountHeaderProfileImageListener() {
@Override
public boolean onProfileImageClick(View view, IProfile profile, boolean current) {
Intent intent = new Intent(MainActivity.this, AccountActivity.class);
intent.putExtra("id", loggedInAccountId);
startActivity(intent);
return false;
}
@Override
public boolean onProfileImageLongClick(View view, IProfile profile, boolean current) {
return false;
}
})
.build();
DrawerImageLoader.init(new AbstractDrawerImageLoader() {
@Override
public void set(ImageView imageView, Uri uri, Drawable placeholder) {
Picasso.with(imageView.getContext()).load(uri).placeholder(placeholder).into(imageView);
}
@Override
public void cancel(ImageView imageView) {
Picasso.with(imageView.getContext()).cancelRequest(imageView);
}
});
drawer = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withTranslucentStatusBar(true)
.withAccountHeader(headerResult)
.withHasStableIds(true)
.withSelectedItem(-1)
.addDrawerItems(
new PrimaryDrawerItem().withIdentifier(1).withName(getString(R.string.action_view_favourites)).withIcon(R.drawable.ic_star_24dp).withSelectable(false),
new PrimaryDrawerItem().withIdentifier(2).withName(getString(R.string.action_view_blocks)).withIcon(R.drawable.ic_block_24dp).withSelectable(false),
new PrimaryDrawerItem().withIdentifier(3).withName(getString(R.string.action_view_preferences)).withIcon(R.drawable.ic_settings_24dp).withSelectable(false),
new PrimaryDrawerItem().withIdentifier(4).withName(getString(R.string.action_logout)).withIcon(R.drawable.ic_exit_to_app_24dp).withSelectable(false)
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if (drawerItem != null) {
long drawerItemIdentifier = drawerItem.getIdentifier();
if (drawerItemIdentifier == 1) {
Intent intent = new Intent(MainActivity.this, FavouritesActivity.class);
startActivity(intent);
} else if (drawerItemIdentifier == 2) {
Intent intent = new Intent(MainActivity.this, BlocksActivity.class);
startActivity(intent);
} else if (drawerItemIdentifier == 3) {
Intent intent = new Intent(MainActivity.this, PreferencesActivity.class);
startActivity(intent);
} else if (drawerItemIdentifier == 4) {
if (notificationServiceEnabled) {
alarmManager.cancel(serviceAlarmIntent);
}
SharedPreferences preferences = getSharedPreferences(
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.remove("domain");
editor.remove("accessToken");
editor.apply();
Intent intent = new Intent(MainActivity.this, SplashActivity.class);
startActivity(intent);
finish();
}
}
return false;
}
})
.build();
// Setup the tabs and timeline pager. // Setup the tabs and timeline pager.
TimelinePagerAdapter adapter = new TimelinePagerAdapter(getSupportFragmentManager()); TimelinePagerAdapter adapter = new TimelinePagerAdapter(getSupportFragmentManager());
String[] pageTitles = { String[] pageTitles = {
@ -152,16 +250,34 @@ public class MainActivity extends BaseActivity {
private void fetchUserInfo() { private void fetchUserInfo() {
SharedPreferences preferences = getSharedPreferences( SharedPreferences preferences = getSharedPreferences(
getString(R.string.preferences_file_key), Context.MODE_PRIVATE); getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
final String domain = preferences.getString("domain", null);
String id = preferences.getString("loggedInAccountId", null); String id = preferences.getString("loggedInAccountId", null);
String username = preferences.getString("loggedInAccountUsername", null); String username = preferences.getString("loggedInAccountUsername", null);
if (id != null && username != null) { //if (id != null && username != null) {
loggedInAccountId = id; // loggedInAccountId = id;
loggedInAccountUsername = username; // loggedInAccountUsername = username;
} else { //} else {
mastodonAPI.accountVerifyCredentials().enqueue(new Callback<Account>() { mastodonAPI.accountVerifyCredentials().enqueue(new Callback<Account>() {
@Override @Override
public void onResponse(Call<Account> call, retrofit2.Response<Account> response) { public void onResponse(Call<Account> call, retrofit2.Response<Account> response) {
onFetchUserInfoSuccess(response.body().id, response.body().username); Account me = response.body();
ImageView background = headerResult.getHeaderBackgroundView();
Picasso.with(MainActivity.this)
.load(me.header)
.placeholder(R.drawable.account_header_missing)
.resize(background.getWidth(), background.getHeight())
.centerCrop()
.into(background);
headerResult.addProfiles(
new ProfileDrawerItem()
.withName(me.displayName)
.withEmail(String.format("%s@%s", me.username, domain))
.withIcon(me.avatar)
);
//onFetchUserInfoSuccess(response.body().id, response.body().username);
} }
@Override @Override
@ -169,7 +285,7 @@ public class MainActivity extends BaseActivity {
onFetchUserInfoFailure((Exception) t); onFetchUserInfoFailure((Exception) t);
} }
}); });
} //}
} }
private void onFetchUserInfoSuccess(String id, String username) { private void onFetchUserInfoSuccess(String id, String username) {
@ -187,59 +303,11 @@ public class MainActivity extends BaseActivity {
Log.e(TAG, "Failed to fetch user info. " + exception.getMessage()); Log.e(TAG, "Failed to fetch user info. " + exception.getMessage());
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_toolbar, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_view_profile: {
Intent intent = new Intent(this, AccountActivity.class);
intent.putExtra("id", loggedInAccountId);
startActivity(intent);
return true;
}
case R.id.action_view_preferences: {
Intent intent = new Intent(this, PreferencesActivity.class);
startActivity(intent);
return true;
}
case R.id.action_view_favourites: {
Intent intent = new Intent(this, FavouritesActivity.class);
startActivity(intent);
return true;
}
case R.id.action_view_blocks: {
Intent intent = new Intent(this, BlocksActivity.class);
startActivity(intent);
return true;
}
case R.id.action_logout: {
if (notificationServiceEnabled) {
alarmManager.cancel(serviceAlarmIntent);
}
SharedPreferences preferences = getSharedPreferences(
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.remove("domain");
editor.remove("accessToken");
editor.apply();
Intent intent = new Intent(this, SplashActivity.class);
startActivity(intent);
finish();
return true;
}
}
return super.onOptionsItemSelected(item);
}
@Override @Override
public void onBackPressed() { public void onBackPressed() {
if(pageHistory.empty()) { if(drawer != null && drawer.isDrawerOpen()) {
drawer.closeDrawer();
} else if(pageHistory.empty()) {
super.onBackPressed(); super.onBackPressed();
} else { } else {
pageHistory.pop(); pageHistory.pop();

View file

@ -22,7 +22,7 @@ import android.preference.PreferenceManager;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
public class PreferencesActivity extends AppCompatActivity public class PreferencesActivity extends BaseActivity
implements SharedPreferences.OnSharedPreferenceChangeListener { implements SharedPreferences.OnSharedPreferenceChangeListener {
private boolean themeSwitched; private boolean themeSwitched;

View file

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@color/toolbar_icon_dark"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM4,12c0,-4.42 3.58,-8 8,-8 1.85,0 3.55,0.63 4.9,1.69L5.69,16.9C4.63,15.55 4,13.85 4,12zM12,20c-1.85,0 -3.55,-0.63 -4.9,-1.69L18.31,7.1C19.37,8.45 20,10.15 20,12c0,4.42 -3.58,8 -8,8z"/>
</vector>

View file

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@color/toolbar_icon_dark"
android:pathData="M10.09,15.59L11.5,17l5,-5 -5,-5 -1.41,1.41L12.67,11H3v2h9.67l-2.58,2.59zM19,3H5c-1.11,0 -2,0.9 -2,2v4h2V5h14v14H5v-4H3v4c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z"/>
</vector>

View file

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@color/toolbar_icon_dark"
android:pathData="M19.43,12.98c0.04,-0.32 0.07,-0.64 0.07,-0.98s-0.03,-0.66 -0.07,-0.98l2.11,-1.65c0.19,-0.15 0.24,-0.42 0.12,-0.64l-2,-3.46c-0.12,-0.22 -0.39,-0.3 -0.61,-0.22l-2.49,1c-0.52,-0.4 -1.08,-0.73 -1.69,-0.98l-0.38,-2.65C14.46,2.18 14.25,2 14,2h-4c-0.25,0 -0.46,0.18 -0.49,0.42l-0.38,2.65c-0.61,0.25 -1.17,0.59 -1.69,0.98l-2.49,-1c-0.23,-0.09 -0.49,0 -0.61,0.22l-2,3.46c-0.13,0.22 -0.07,0.49 0.12,0.64l2.11,1.65c-0.04,0.32 -0.07,0.65 -0.07,0.98s0.03,0.66 0.07,0.98l-2.11,1.65c-0.19,0.15 -0.24,0.42 -0.12,0.64l2,3.46c0.12,0.22 0.39,0.3 0.61,0.22l2.49,-1c0.52,0.4 1.08,0.73 1.69,0.98l0.38,2.65c0.03,0.24 0.24,0.42 0.49,0.42h4c0.25,0 0.46,-0.18 0.49,-0.42l0.38,-2.65c0.61,-0.25 1.17,-0.59 1.69,-0.98l2.49,1c0.23,0.09 0.49,0 0.61,-0.22l2,-3.46c0.12,-0.22 0.07,-0.49 -0.12,-0.64l-2.11,-1.65zM12,15.5c-1.93,0 -3.5,-1.57 -3.5,-3.5s1.57,-3.5 3.5,-3.5 3.5,1.57 3.5,3.5 -1.57,3.5 -3.5,3.5z"/>
</vector>

View file

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_view_profile"
android:title="@string/action_view_profile"
app:showAsAction="never" />
<item
android:id="@+id/action_view_preferences"
android:title="@string/action_view_preferences"
app:showAsAction="never" />
<item
android:id="@+id/action_view_favourites"
android:title="@string/action_view_favourites"
app:showAsAction="never" />
<item
android:id="@+id/action_view_blocks"
android:title="@string/action_view_blocks"
app:showAsAction="never" />
<item
android:id="@+id/action_logout"
android:title="@string/action_logout"
app:showAsAction="never" />
</menu>

View file

@ -91,7 +91,7 @@
<string name="notification_follow_format">%s followed you</string> <string name="notification_follow_format">%s followed you</string>
<string name="report_username_format">Reporting @%s</string> <string name="report_username_format">Reporting @%s</string>
<string name="report_comment_hint">Additional Comments?</string> <string name="report_comment_hint">Additional comments?</string>
<string name="action_compose">Compose</string> <string name="action_compose">Compose</string>
<string name="action_login">Login with Mastodon</string> <string name="action_login">Login with Mastodon</string>
@ -113,7 +113,7 @@
<string name="action_view_profile">Profile</string> <string name="action_view_profile">Profile</string>
<string name="action_view_preferences">Preferences</string> <string name="action_view_preferences">Preferences</string>
<string name="action_view_favourites">Favourites</string> <string name="action_view_favourites">Favourites</string>
<string name="action_view_blocks">Blocked Users</string> <string name="action_view_blocks">Blocked users</string>
<string name="action_open_in_web">Open in browser</string> <string name="action_open_in_web">Open in browser</string>
<string name="action_set_time">Set</string> <string name="action_set_time">Set</string>

View file

@ -55,6 +55,16 @@
<item name="notification_icon_tint">@color/notification_icon_tint_dark</item> <item name="notification_icon_tint">@color/notification_icon_tint_dark</item>
<item name="report_status_background_color">@color/report_status_background_dark</item> <item name="report_status_background_color">@color/report_status_background_dark</item>
<item name="report_status_divider_drawable">@drawable/report_status_divider_dark</item> <item name="report_status_divider_drawable">@drawable/report_status_divider_dark</item>
<item name="material_drawer_background">@color/color_primary_dark</item>
<item name="material_drawer_primary_text">@color/text_color_primary_dark</item>
<item name="material_drawer_primary_icon">@color/toolbar_icon_dark</item>
<item name="material_drawer_secondary_text">@color/text_color_secondary_dark</item>
<item name="material_drawer_hint_text">@color/text_color_tertiary_dark</item>
<item name="material_drawer_divider">@color/color_primary_dark_dark</item>
<item name="material_drawer_selected">@color/window_background_dark</item>
<item name="material_drawer_selected_text">@color/text_color_primary_dark</item>
<item name="material_drawer_header_selection_text">@color/text_color_primary_dark</item>
</style> </style>
<style name="AppTheme.ImageButton.Dark" parent="@style/Widget.AppCompat.Button.Borderless.Colored"> <style name="AppTheme.ImageButton.Dark" parent="@style/Widget.AppCompat.Button.Borderless.Colored">