From 8eaaf1c054e0072d62d6fc054d7a5b93b1b4bced Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sun, 16 Jul 2017 12:57:03 +0300 Subject: [PATCH] Make unread invisible for guests --- .../mthmmy/activities/main/MainActivity.java | 75 ++++++++++++------- .../gr/thmmy/mthmmy/base/BaseActivity.java | 10 +++ .../gr/thmmy/mthmmy/base/BaseApplication.java | 2 - .../gr/thmmy/mthmmy/base/BaseFragment.java | 29 +------ 4 files changed, 60 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index bc6603a8..9e665182 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java @@ -10,6 +10,9 @@ import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.widget.Toast; +import java.util.ArrayList; +import java.util.List; + import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.LoginActivity; import gr.thmmy.mthmmy.activities.board.BoardActivity; @@ -39,6 +42,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF //-----------------------------------------CLASS VARIABLES------------------------------------------ private static final int TIME_INTERVAL = 2000; private long mBackPressed; + private SectionsPagerAdapter sectionsPagerAdapter; + private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { @@ -48,7 +53,6 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF setContentView(R.layout.activity_main); if (sessionManager.isLoginScreenDefault()) - { //Go to login Intent intent = new Intent(MainActivity.this, LoginActivity.class); @@ -61,14 +65,21 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF createDrawer(); //Create the adapter that will return a fragment for each section of the activity - SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + sectionsPagerAdapter.addFragment(RecentFragment.newInstance(1), "RECENT"); + sectionsPagerAdapter.addFragment(ForumFragment.newInstance(2), "FORUM"); + if(sessionManager.isLoggedIn()) + sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(3), "UNREAD"); + //Set up the ViewPager with the sections adapter. - ViewPager mViewPager = (ViewPager) findViewById(R.id.container); - mViewPager.setAdapter(mSectionsPagerAdapter); + viewPager = (ViewPager) findViewById(R.id.container); + viewPager.setAdapter(sectionsPagerAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); - tabLayout.setupWithViewPager(mViewPager); + tabLayout.setupWithViewPager(viewPager); + + setMainActivity(this); } @Override @@ -81,6 +92,7 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF @Override protected void onResume() { drawer.setSelection(HOME_ID); + updateTabs(); super.onResume(); } @@ -135,45 +147,56 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ private class SectionsPagerAdapter extends FragmentPagerAdapter { + private final List fragmentList = new ArrayList<>(); + private final List fragmentTitleList = new ArrayList<>(); SectionsPagerAdapter(FragmentManager fm) { super(fm); } + void addFragment(Fragment fragment, String title) { + fragmentList.add(fragment); + fragmentTitleList.add(title); + notifyDataSetChanged(); + } + + void removeFragment(int position) { + fragmentList.remove(position); + fragmentTitleList.remove(position); + notifyDataSetChanged(); + if(viewPager.getCurrentItem()==position) + viewPager.setCurrentItem(position-1); + } + @Override public Fragment getItem(int position) { - switch (position) { - case 0: - return RecentFragment.newInstance(position + 1); - case 1: - return ForumFragment.newInstance(position + 1); - case 2: - return UnreadFragment.newInstance(position + 1); - default: - return RecentFragment.newInstance(position + 1); //temp (?) - } + return fragmentList.get(position); } @Override public int getCount() { - // Show 3 total pages. - return 3; + return fragmentList.size(); } @Override public CharSequence getPageTitle(int position) { - switch (position) { - case 0: - return "RECENT"; - case 1: - return "FORUM"; - case 2: - return "UNREAD"; - } + return fragmentTitleList.get(position); + } - return null; + @Override + public int getItemPosition(Object object) { + int position = fragmentList.indexOf(object); + return position == -1 ? POSITION_NONE : position; } } + + public void updateTabs() + { + if(!sessionManager.isLoggedIn()&§ionsPagerAdapter.getCount()==3) + sectionsPagerAdapter.removeFragment(2); + else if(sessionManager.isLoggedIn()&§ionsPagerAdapter.getCount()==2) + sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(3), "UNREAD"); + } //-------------------------------FragmentPagerAdapter END------------------------------------------- private void redirectToActivityFromIntent(Intent intent) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index a2c8877e..f43d603f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -75,6 +75,8 @@ public abstract class BaseActivity extends AppCompatActivity { protected Toolbar toolbar; protected Drawer drawer; + private MainActivity mainActivity; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -394,6 +396,8 @@ public abstract class BaseActivity extends AppCompatActivity { protected void onPostExecute(Integer result) { Toast.makeText(getBaseContext(), "Logged out successfully!", Toast.LENGTH_LONG).show(); updateDrawer(); + if(mainActivity!=null) + mainActivity.updateTabs(); progressDialog.dismiss(); } } @@ -563,4 +567,10 @@ public abstract class BaseActivity extends AppCompatActivity { } + //----------------------------------MISC---------------------- + protected void setMainActivity(MainActivity mainActivity) + { + this.mainActivity = mainActivity; + } + } diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java index 3030cce0..24c84558 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java @@ -35,8 +35,6 @@ import okhttp3.Response; import timber.log.Timber; public class BaseApplication extends Application { - - private static BaseApplication baseApplication; //BaseApplication singleton // Client & SessionManager diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseFragment.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseFragment.java index 20bd8da2..0f3487d9 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseFragment.java @@ -6,7 +6,6 @@ import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import okhttp3.OkHttpClient; -import timber.log.Timber; public abstract class BaseFragment extends Fragment { protected static final String ARG_SECTION_NUMBER = "SectionNumber"; @@ -26,31 +25,6 @@ public abstract class BaseFragment extends Fragment { if (client == null) client = BaseApplication.getInstance().getClient(); //must check every time - e.g. // becomes null when app restarts after crash - Timber.d("onCreate"); - } - - @Override - public void onStart() { - super.onStart(); - Timber.d("onStart"); - } - - @Override - public void onResume() { - super.onResume(); - Timber.d("onResume"); - } - - @Override - public void onPause() { - super.onPause(); - Timber.d("onPause"); - } - - @Override - public void onStop() { - super.onStop(); - Timber.d("onStop"); } @Override @@ -76,6 +50,5 @@ public abstract class BaseFragment extends Fragment { * the activity that contains it, to allow communication upon interaction, * between the fragment and the activity/ other fragments */ - public interface FragmentInteractionListener { - } + public interface FragmentInteractionListener {} }