Browse Source

Make unread invisible for guests

pull/24/head
Ezerous 8 years ago
parent
commit
8eaaf1c054
  1. 75
      app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java
  2. 10
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  3. 2
      app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java
  4. 29
      app/src/main/java/gr/thmmy/mthmmy/base/BaseFragment.java

75
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.support.v4.view.ViewPager;
import android.widget.Toast; import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.LoginActivity; import gr.thmmy.mthmmy.activities.LoginActivity;
import gr.thmmy.mthmmy.activities.board.BoardActivity; import gr.thmmy.mthmmy.activities.board.BoardActivity;
@ -39,6 +42,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
//-----------------------------------------CLASS VARIABLES------------------------------------------ //-----------------------------------------CLASS VARIABLES------------------------------------------
private static final int TIME_INTERVAL = 2000; private static final int TIME_INTERVAL = 2000;
private long mBackPressed; private long mBackPressed;
private SectionsPagerAdapter sectionsPagerAdapter;
private ViewPager viewPager;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -48,7 +53,6 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
if (sessionManager.isLoginScreenDefault()) if (sessionManager.isLoginScreenDefault())
{ {
//Go to login //Go to login
Intent intent = new Intent(MainActivity.this, LoginActivity.class); Intent intent = new Intent(MainActivity.this, LoginActivity.class);
@ -61,14 +65,21 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
createDrawer(); createDrawer();
//Create the adapter that will return a fragment for each section of the activity //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. //Set up the ViewPager with the sections adapter.
ViewPager mViewPager = (ViewPager) findViewById(R.id.container); viewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter); viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager); tabLayout.setupWithViewPager(viewPager);
setMainActivity(this);
} }
@Override @Override
@ -81,6 +92,7 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
@Override @Override
protected void onResume() { protected void onResume() {
drawer.setSelection(HOME_ID); drawer.setSelection(HOME_ID);
updateTabs();
super.onResume(); super.onResume();
} }
@ -135,45 +147,56 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
* {@link android.support.v4.app.FragmentStatePagerAdapter}. * {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/ */
private class SectionsPagerAdapter extends FragmentPagerAdapter { private class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
SectionsPagerAdapter(FragmentManager fm) { SectionsPagerAdapter(FragmentManager fm) {
super(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 @Override
public Fragment getItem(int position) { public Fragment getItem(int position) {
switch (position) { return fragmentList.get(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 (?)
}
} }
@Override @Override
public int getCount() { public int getCount() {
// Show 3 total pages. return fragmentList.size();
return 3;
} }
@Override @Override
public CharSequence getPageTitle(int position) { public CharSequence getPageTitle(int position) {
switch (position) { return fragmentTitleList.get(position);
case 0: }
return "RECENT";
case 1:
return "FORUM";
case 2:
return "UNREAD";
}
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()&&sectionsPagerAdapter.getCount()==3)
sectionsPagerAdapter.removeFragment(2);
else if(sessionManager.isLoggedIn()&&sectionsPagerAdapter.getCount()==2)
sectionsPagerAdapter.addFragment(UnreadFragment.newInstance(3), "UNREAD");
}
//-------------------------------FragmentPagerAdapter END------------------------------------------- //-------------------------------FragmentPagerAdapter END-------------------------------------------
private void redirectToActivityFromIntent(Intent intent) { private void redirectToActivityFromIntent(Intent intent) {

10
app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java

@ -75,6 +75,8 @@ public abstract class BaseActivity extends AppCompatActivity {
protected Toolbar toolbar; protected Toolbar toolbar;
protected Drawer drawer; protected Drawer drawer;
private MainActivity mainActivity;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -394,6 +396,8 @@ public abstract class BaseActivity extends AppCompatActivity {
protected void onPostExecute(Integer result) { protected void onPostExecute(Integer result) {
Toast.makeText(getBaseContext(), "Logged out successfully!", Toast.LENGTH_LONG).show(); Toast.makeText(getBaseContext(), "Logged out successfully!", Toast.LENGTH_LONG).show();
updateDrawer(); updateDrawer();
if(mainActivity!=null)
mainActivity.updateTabs();
progressDialog.dismiss(); progressDialog.dismiss();
} }
} }
@ -563,4 +567,10 @@ public abstract class BaseActivity extends AppCompatActivity {
} }
//----------------------------------MISC----------------------
protected void setMainActivity(MainActivity mainActivity)
{
this.mainActivity = mainActivity;
}
} }

2
app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java

@ -35,8 +35,6 @@ import okhttp3.Response;
import timber.log.Timber; import timber.log.Timber;
public class BaseApplication extends Application { public class BaseApplication extends Application {
private static BaseApplication baseApplication; //BaseApplication singleton private static BaseApplication baseApplication; //BaseApplication singleton
// Client & SessionManager // Client & SessionManager

29
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 android.support.v4.app.Fragment;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import timber.log.Timber;
public abstract class BaseFragment extends Fragment { public abstract class BaseFragment extends Fragment {
protected static final String ARG_SECTION_NUMBER = "SectionNumber"; protected static final String ARG_SECTION_NUMBER = "SectionNumber";
@ -26,31 +25,6 @@ public abstract class BaseFragment extends Fragment {
if (client == null) if (client == null)
client = BaseApplication.getInstance().getClient(); //must check every time - e.g. client = BaseApplication.getInstance().getClient(); //must check every time - e.g.
// becomes null when app restarts after crash // 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 @Override
@ -76,6 +50,5 @@ public abstract class BaseFragment extends Fragment {
* the activity that contains it, to allow communication upon interaction, * the activity that contains it, to allow communication upon interaction,
* between the fragment and the activity/ other fragments * between the fragment and the activity/ other fragments
*/ */
public interface FragmentInteractionListener { public interface FragmentInteractionListener {}
}
} }

Loading…
Cancel
Save