From 60f441d45388b5f8eebcbec75d7ba8e030273b34 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Wed, 30 Nov 2016 17:25:14 +0200 Subject: [PATCH] v0.2 Replaced menu with NavigationDrawer Replaced TopicActivity's Actionbar with Toolbar --- app/build.gradle | 5 +- app/src/main/AndroidManifest.xml | 5 +- .../thmmy/mthmmy/activities/BaseActivity.java | 130 ++++++++++++++++++ .../thmmy/mthmmy/activities/MainActivity.java | 99 ++----------- .../mthmmy/activities/TopicActivity.java | 17 ++- .../main/java/gr/thmmy/mthmmy/data/Post.java | 2 - app/src/main/res/layout/activity_topic.xml | 18 +++ app/src/main/res/menu/menu_main.xml | 22 --- 8 files changed, 172 insertions(+), 126 deletions(-) delete mode 100644 app/src/main/res/menu/menu_main.xml diff --git a/app/build.gradle b/app/build.gradle index 4ee4c703..a00df7c5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "gr.thmmy.mthmmy" minSdkVersion 19 targetSdkVersion 25 - versionCode 3 - versionName "0.17" + versionCode 4 + versionName "0.2" } buildTypes { release { @@ -35,4 +35,5 @@ dependencies { compile('com.mikepenz:materialdrawer:5.7.0@aar') { transitive = true } + compile 'com.mikepenz:fontawesome-typeface:4.7.0.0@aar' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ec83d38a..a4a2cfa0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="gr.thmmy.mthmmy"> @@ -40,7 +40,8 @@ + android:parentActivityName=".activities.MainActivity" + android:theme="@style/AppTheme.NoActionBar"> diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java index 287bc00c..856890ea 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java @@ -1,16 +1,32 @@ package gr.thmmy.mthmmy.activities; +import android.app.ProgressDialog; +import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Color; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; +import android.view.View; +import android.widget.Toast; import com.franmontiel.persistentcookiejar.PersistentCookieJar; import com.franmontiel.persistentcookiejar.cache.SetCookieCache; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; +import com.mikepenz.fontawesome_typeface_library.FontAwesome; +import com.mikepenz.iconics.IconicsDrawable; +import com.mikepenz.materialdrawer.Drawer; +import com.mikepenz.materialdrawer.DrawerBuilder; +import com.mikepenz.materialdrawer.model.PrimaryDrawerItem; +import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; +import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.session.SessionManager; import okhttp3.OkHttpClient; +import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_IN; + public class BaseActivity extends AppCompatActivity { // Client & Cookies @@ -28,6 +44,9 @@ public class BaseActivity extends AppCompatActivity //Other variables private static boolean init = false; //To initialize stuff only once per app start + //Common UI elements + protected Toolbar toolbar; + protected Drawer drawer; @Override protected void onCreate(Bundle savedInstanceState) { @@ -51,4 +70,115 @@ public class BaseActivity extends AppCompatActivity return client; } + //TODO: move stuff below + //------------------------------------------DRAWER STUFF---------------------------------------- + + private static final int LOGINLOGOUT_ID=0; + private static final int ABOUT_ID=1; + + protected PrimaryDrawerItem loginLogout, about; + protected IconicsDrawable loginIcon,logoutIcon, aboutIcon; + /** + * Call only after initializing Toolbar + */ + protected void createDrawer()//TODO + { + loginIcon =new IconicsDrawable(this) + .icon(FontAwesome.Icon.faw_sign_in) + .color(Color.BLACK) + .sizeDp(24); + logoutIcon =new IconicsDrawable(this) + .icon(FontAwesome.Icon.faw_sign_out) + .color(Color.BLACK) + .sizeDp(24); + loginLogout = new PrimaryDrawerItem().withIdentifier(LOGINLOGOUT_ID).withName(R.string.logout).withIcon(logoutIcon); + aboutIcon =new IconicsDrawable(this) + .icon(FontAwesome.Icon.faw_info_circle) + .color(Color.BLACK) + .sizeDp(24); + about = new PrimaryDrawerItem().withIdentifier(ABOUT_ID).withName(R.string.about).withIcon(aboutIcon); + drawer = new DrawerBuilder().withActivity(BaseActivity.this) + .withToolbar(toolbar) + .addDrawerItems(loginLogout,about) + .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { + @Override + public boolean onItemClick(View view, int position, IDrawerItem drawerItem) { + if(drawerItem.equals(LOGINLOGOUT_ID)) + { + if (sessionManager.getLogStatus()!= LOGGED_IN) //When logged out or if user is guest + { + Intent intent = new Intent(BaseActivity.this, LoginActivity.class); + startActivity(intent); + finish(); + overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); + + } + else + new LogoutTask().execute(); + } + else if(drawerItem.equals(ABOUT_ID)) + { + Intent i = new Intent(BaseActivity.this, AboutActivity.class); + startActivity(i); + } + drawer.closeDrawer(); + return true; + } + }) + .build(); + drawer.setSelection(-1); + } + + protected void updateDrawer() + { + if(drawer!=null) + { + if (sessionManager.getLogStatus()!= LOGGED_IN) //When logged out or if user is guest + { + //Swap logout with login + loginLogout.withName(R.string.login).withIcon(loginIcon); + drawer.updateItem(loginLogout); + } + else + { + //Swap login with logout + loginLogout.withName(R.string.logout).withIcon(logoutIcon); + drawer.updateItem(loginLogout); + } + } + } + + + //-------------------------------------------LOGOUT------------------------------------------------- + /** + * Result toast will always display a success, because when user chooses logout all data are + * cleared regardless of the actual outcome + */ + protected class LogoutTask extends AsyncTask { //Attempt logout + ProgressDialog progressDialog; + + protected Integer doInBackground(Void... voids) { + return sessionManager.logout(); + } + + protected void onPreExecute() + { //Show a progress dialog until done + progressDialog = new ProgressDialog(BaseActivity.this, + R.style.AppTheme_Dark_Dialog); + progressDialog.setCancelable(false); + progressDialog.setIndeterminate(true); + progressDialog.setMessage("Logging out..."); + progressDialog.show(); + } + + protected void onPostExecute(Integer result) + { + Toast.makeText(getBaseContext(), "Logged out successfully!", Toast.LENGTH_LONG).show(); + updateDrawer(); + progressDialog.dismiss(); + } + } +//-----------------------------------------LOGOUT END----------------------------------------------- + + } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java index f791d871..25b4dd70 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/MainActivity.java @@ -1,8 +1,6 @@ package gr.thmmy.mthmmy.activities; -import android.app.ProgressDialog; import android.content.Intent; -import android.os.AsyncTask; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; @@ -10,23 +8,18 @@ import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.widget.Toolbar; -import android.view.Menu; -import android.view.MenuItem; -import android.widget.Toast; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.data.TopicSummary; import gr.thmmy.mthmmy.sections.forum.ForumFragment; import gr.thmmy.mthmmy.sections.recent.RecentFragment; -import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_IN; import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_OUT; public class MainActivity extends BaseActivity implements RecentFragment.OnListFragmentInteractionListener, ForumFragment.OnListFragmentInteractionListener { -//----------------------------------------CLASS VARIABLES----------------------------------------- + //----------------------------------------CLASS VARIABLES----------------------------------------- private static final String TAG = "MainActivity"; - private Menu menu; @Override protected void onCreate(Bundle savedInstanceState) { @@ -42,9 +35,12 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF } //Initialize toolbar - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); + //Initialize drawer + createDrawer(); + //Create the adapter that will return a fragment for each section of the activity SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); @@ -54,66 +50,17 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(mViewPager); - - //TODO: Drawer -// new DrawerBuilder().withActivity(this) -// .withToolbar(toolbar) -// .build(); - } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - //Inflate the menu; this adds items to the action bar if it is present. - this.menu = menu; - getMenuInflater().inflate(R.menu.menu_main, menu); - - if (sessionManager.getLogStatus()!= LOGGED_IN) //When logged out or if user is guest - hideLogout(); - else - hideLogin(); - - - return true; + protected void onResume() { + super.onResume(); + updateDrawer(); } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - int id = item.getItemId(); - - if (id == R.id.action_about) { - //Go to about - Intent i = new Intent(MainActivity.this, AboutActivity.class); - startActivity(i); - return true; - } else if (id == R.id.action_logout) - //Attempt logout - new LogoutTask().execute(); - else { - //Go to login - Intent intent = new Intent(MainActivity.this, LoginActivity.class); - startActivity(intent); - finish(); - overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); - } - return super.onOptionsItemSelected(item); - } - private void hideLogin() { //Hide login AND show logout - MenuItem login = menu.findItem(R.id.action_login); - MenuItem logout = menu.findItem(R.id.action_logout); - login.setVisible(false); - logout.setVisible(true); - } - private void hideLogout() { //Hide logout AND show login - MenuItem login = menu.findItem(R.id.action_login); - MenuItem logout = menu.findItem(R.id.action_logout); - login.setVisible(true); - logout.setVisible(false); - } @Override public void onFragmentInteraction(TopicSummary topicSummary) { @@ -166,34 +113,4 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF } //-------------------------------FragmentPagerAdapter END------------------------------------------- -//-------------------------------------------LOGOUT------------------------------------------------- -/** - * Result toast will always display a success, because when user chooses logout all data are - * cleared regardless of the actual outcome - */ - private class LogoutTask extends AsyncTask { //Attempt logout - ProgressDialog progressDialog; - - protected Integer doInBackground(Void... voids) { - return sessionManager.logout(); - } - - protected void onPreExecute() - { //Show a progress dialog until done - progressDialog = new ProgressDialog(MainActivity.this, - R.style.AppTheme_Dark_Dialog); - progressDialog.setCancelable(false); - progressDialog.setIndeterminate(true); - progressDialog.setMessage("Logging out..."); - progressDialog.show(); - } - - protected void onPostExecute(Integer result) - { - Toast.makeText(getBaseContext(), "Logged out successfully!", Toast.LENGTH_LONG).show(); - hideLogout(); - progressDialog.dismiss(); - } - } -//-----------------------------------------LOGOUT END----------------------------------------------- } \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java index e7385f4e..cfe2bbde 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/TopicActivity.java @@ -13,6 +13,7 @@ import android.os.Handler; import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.support.v7.widget.CardView; +import android.support.v7.widget.Toolbar; import android.text.TextUtils; import android.util.Log; import android.util.SparseArray; @@ -92,7 +93,6 @@ public class TopicActivity extends BaseActivity { private static final String TAG = "TopicActivity"; private String topicTitle; private String parsedTitle; - private ActionBar actionbar; @Override protected void onCreate(Bundle savedInstanceState) { @@ -102,6 +102,13 @@ public class TopicActivity extends BaseActivity { Bundle extras = getIntent().getExtras(); topicTitle = getIntent().getExtras().getString("TOPIC_TITLE"); + //Initialize toolbar + toolbar = (Toolbar) findViewById(R.id.toolbar); + toolbar.setTitle(topicTitle); + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setDisplayShowHomeEnabled(true); + //Variables initialization postsLinearLayout = (LinearLayout) findViewById(R.id.posts_list); @@ -112,10 +119,6 @@ public class TopicActivity extends BaseActivity { postsList = new ArrayList<>(); - actionbar = getSupportActionBar(); - if (actionbar != null) - if(!Objects.equals(topicTitle, "")) - actionbar.setTitle(topicTitle); firstPage = (ImageButton) findViewById(R.id.page_first_button); previousPage = (ImageButton) findViewById(R.id.page_previous_button); @@ -504,8 +507,8 @@ public class TopicActivity extends BaseActivity { //Set topic title if not already present if (topicTitle == null || Objects.equals(topicTitle, "")) { topicTitle = parsedTitle; - if (actionbar != null){ - actionbar.setTitle(topicTitle); + if (toolbar != null){ + toolbar.setTitle(topicTitle); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/data/Post.java b/app/src/main/java/gr/thmmy/mthmmy/data/Post.java index f424eb65..2352d711 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/data/Post.java +++ b/app/src/main/java/gr/thmmy/mthmmy/data/Post.java @@ -1,7 +1,5 @@ package gr.thmmy.mthmmy.data; -import android.util.Log; - public class Post { //Standard info private final String thumbnailUrl; diff --git a/app/src/main/res/layout/activity_topic.xml b/app/src/main/res/layout/activity_topic.xml index 98da0249..eb9c6ee7 100644 --- a/app/src/main/res/layout/activity_topic.xml +++ b/app/src/main/res/layout/activity_topic.xml @@ -9,10 +9,28 @@ android:fitsSystemWindows="true" tools:context=".activities.MainActivity"> + + + + + + + - - - -