diff --git a/app/build.gradle b/app/build.gradle index 762e622d..161bee86 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,4 +37,7 @@ dependencies { compile 'com.mikhaellopez:circularfillableloaders:1.2.0' compile 'com.squareup.retrofit2:retrofit:2.2.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' + compile('com.mikepenz:materialdrawer:5.8.2@aar') { + transitive = true + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index 37f8e7bc..72d4c2bd 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -21,6 +21,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.os.Build; import android.os.SystemClock; import android.preference.PreferenceManager; @@ -35,6 +36,7 @@ import android.transition.TransitionInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.ImageView; import com.android.volley.AuthFailureError; import com.android.volley.Request; @@ -42,6 +44,18 @@ import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; 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.JSONObject; @@ -63,6 +77,8 @@ public class MainActivity extends BaseActivity { private String loggedInAccountUsername; Stack pageHistory = new Stack(); private ViewPager viewPager; + private AccountHeader headerResult; + private Drawer drawer; @Override 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. TimelinePagerAdapter adapter = new TimelinePagerAdapter(getSupportFragmentManager()); String[] pageTitles = { @@ -152,16 +250,34 @@ public class MainActivity extends BaseActivity { private void fetchUserInfo() { SharedPreferences preferences = getSharedPreferences( getString(R.string.preferences_file_key), Context.MODE_PRIVATE); + final String domain = preferences.getString("domain", null); String id = preferences.getString("loggedInAccountId", null); String username = preferences.getString("loggedInAccountUsername", null); - if (id != null && username != null) { - loggedInAccountId = id; - loggedInAccountUsername = username; - } else { + //if (id != null && username != null) { + // loggedInAccountId = id; + // loggedInAccountUsername = username; + //} else { mastodonAPI.accountVerifyCredentials().enqueue(new Callback() { @Override public void onResponse(Call call, retrofit2.Response 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 @@ -169,7 +285,7 @@ public class MainActivity extends BaseActivity { onFetchUserInfoFailure((Exception) t); } }); - } + //} } 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()); } - @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 public void onBackPressed() { - if(pageHistory.empty()) { + if(drawer != null && drawer.isDrawerOpen()) { + drawer.closeDrawer(); + } else if(pageHistory.empty()) { super.onBackPressed(); } else { pageHistory.pop(); diff --git a/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java b/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java index 53b29cea..ffc2811a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java @@ -22,7 +22,7 @@ import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; -public class PreferencesActivity extends AppCompatActivity +public class PreferencesActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener { private boolean themeSwitched; diff --git a/app/src/main/res/drawable/ic_block_24dp.xml b/app/src/main/res/drawable/ic_block_24dp.xml new file mode 100644 index 00000000..7a184d9d --- /dev/null +++ b/app/src/main/res/drawable/ic_block_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_exit_to_app_24dp.xml b/app/src/main/res/drawable/ic_exit_to_app_24dp.xml new file mode 100644 index 00000000..93378231 --- /dev/null +++ b/app/src/main/res/drawable/ic_exit_to_app_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_settings_24dp.xml b/app/src/main/res/drawable/ic_settings_24dp.xml new file mode 100644 index 00000000..4c81fb46 --- /dev/null +++ b/app/src/main/res/drawable/ic_settings_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/menu/main_toolbar.xml b/app/src/main/res/menu/main_toolbar.xml deleted file mode 100644 index 2517ee5d..00000000 --- a/app/src/main/res/menu/main_toolbar.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cfc9ffd3..c48f4a5e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -91,7 +91,7 @@ %s followed you Reporting @%s - Additional Comments? + Additional comments? Compose Login with Mastodon @@ -113,7 +113,7 @@ Profile Preferences Favourites - Blocked Users + Blocked users Open in browser Set diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index df0430db..8546465d 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -55,6 +55,16 @@ @color/notification_icon_tint_dark @color/report_status_background_dark @drawable/report_status_divider_dark + + @color/color_primary_dark + @color/text_color_primary_dark + @color/toolbar_icon_dark + @color/text_color_secondary_dark + @color/text_color_tertiary_dark + @color/color_primary_dark_dark + @color/window_background_dark + @color/text_color_primary_dark + @color/text_color_primary_dark