|
|
@ -1,6 +1,7 @@ |
|
|
|
package gr.thmmy.mthmmy.activities; |
|
|
|
|
|
|
|
import android.content.Intent; |
|
|
|
import android.content.SharedPreferences; |
|
|
|
import android.os.AsyncTask; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.support.design.widget.TabLayout; |
|
|
@ -11,18 +12,17 @@ import android.support.v4.view.ViewPager; |
|
|
|
import android.support.v7.widget.Toolbar; |
|
|
|
import android.view.Menu; |
|
|
|
import android.view.MenuItem; |
|
|
|
|
|
|
|
import org.jsoup.nodes.Document; |
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
import gr.thmmy.mthmmy.R; |
|
|
|
import gr.thmmy.mthmmy.data.TopicSummary; |
|
|
|
import gr.thmmy.mthmmy.sections.recent.RecentFragment; |
|
|
|
import gr.thmmy.mthmmy.utils.Thmmy; |
|
|
|
|
|
|
|
public class MainActivity extends BaseActivity implements RecentFragment.OnListFragmentInteractionListener |
|
|
|
{ |
|
|
|
private SectionsPagerAdapter mSectionsPagerAdapter; |
|
|
|
private ViewPager mViewPager; /** The {@link ViewPager} that will host the section contents.*/ |
|
|
|
public class MainActivity extends BaseActivity implements RecentFragment.OnListFragmentInteractionListener { |
|
|
|
/** |
|
|
|
* The {@link ViewPager} that will host the section contents. |
|
|
|
*/ |
|
|
|
|
|
|
|
private Menu menu; |
|
|
|
|
|
|
@ -31,14 +31,22 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF |
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
setContentView(R.layout.activity_main); |
|
|
|
|
|
|
|
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE); |
|
|
|
if (!prefs.getBoolean(IS_LOGGED_IN, false)) { |
|
|
|
Intent intent = new Intent(MainActivity.this, LoginActivity.class); |
|
|
|
startActivity(intent); |
|
|
|
finish(); |
|
|
|
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); |
|
|
|
} |
|
|
|
|
|
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
|
|
|
setSupportActionBar(toolbar); |
|
|
|
|
|
|
|
// Create the adapter that will return a fragment for each section of the activity
|
|
|
|
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); |
|
|
|
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); |
|
|
|
|
|
|
|
// Set up the ViewPager with the sections adapter.
|
|
|
|
mViewPager = (ViewPager) findViewById(R.id.container); |
|
|
|
ViewPager mViewPager = (ViewPager) findViewById(R.id.container); |
|
|
|
mViewPager.setAdapter(mSectionsPagerAdapter); |
|
|
|
|
|
|
|
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); |
|
|
@ -57,8 +65,9 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF |
|
|
|
// 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(loginData.getStatus()== Thmmy.LOGGED_IN) |
|
|
|
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE); |
|
|
|
if (prefs.getBoolean(IS_LOGGED_IN, false) |
|
|
|
&& prefs.getString(USER_NAME, null) != GUEST_PREF_USERNAME) |
|
|
|
hideLogin(); |
|
|
|
else |
|
|
|
hideLogout(); |
|
|
@ -67,17 +76,14 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean onOptionsItemSelected(MenuItem item) |
|
|
|
{ |
|
|
|
public boolean onOptionsItemSelected(MenuItem item) { |
|
|
|
int id = item.getItemId(); |
|
|
|
|
|
|
|
if (id == R.id.action_about) |
|
|
|
{ |
|
|
|
if (id == R.id.action_about) { |
|
|
|
Intent i = new Intent(MainActivity.this, AboutActivity.class); |
|
|
|
startActivity(i); |
|
|
|
return true; |
|
|
|
} |
|
|
|
else if (id == R.id.action_logout) |
|
|
|
} else if (id == R.id.action_logout) |
|
|
|
new LogoutTask().execute(); |
|
|
|
else { |
|
|
|
Intent intent = new Intent(MainActivity.this, LoginActivity.class); |
|
|
@ -89,26 +95,22 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF |
|
|
|
return super.onOptionsItemSelected(item); |
|
|
|
} |
|
|
|
|
|
|
|
private void hideLogin() |
|
|
|
{ |
|
|
|
private void hideLogin() { |
|
|
|
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() |
|
|
|
{ |
|
|
|
private void hideLogout() { |
|
|
|
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) |
|
|
|
{ |
|
|
|
public void onFragmentInteraction(TopicSummary topicSummary) { |
|
|
|
Intent i = new Intent(MainActivity.this, TopicActivity.class); |
|
|
|
i.putExtra("TOPIC_URL", topicSummary.getTopicUrl()); |
|
|
|
i.putExtra("TOPIC_TITLE", topicSummary.getTitle()); |
|
|
@ -116,6 +118,7 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF |
|
|
|
} |
|
|
|
|
|
|
|
//---------------------------------FragmentPagerAdapter---------------------------------------------
|
|
|
|
|
|
|
|
/** |
|
|
|
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to |
|
|
|
* one of the sections/tabs/pages. If it becomes too memory intensive, |
|
|
@ -154,29 +157,26 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private class LogoutTask extends AsyncTask<Void, Void, Integer> |
|
|
|
{ |
|
|
|
protected Integer doInBackground(Void... voids) |
|
|
|
{ |
|
|
|
private class LogoutTask extends AsyncTask<Void, Void, Integer> { |
|
|
|
protected Integer doInBackground(Void... voids) { |
|
|
|
return Thmmy.logout(loginData); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void onPreExecute() { |
|
|
|
//TODO: a progressbar maybe?
|
|
|
|
} |
|
|
|
|
|
|
|
protected void onPostExecute(Integer result) |
|
|
|
{ |
|
|
|
if(result==Thmmy.LOGGED_OUT) |
|
|
|
protected void onPostExecute(Integer result) { |
|
|
|
if (result == Thmmy.LOGGED_OUT) { |
|
|
|
SharedPreferences.Editor editor = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE).edit(); |
|
|
|
editor.putString(USER_NAME, null); |
|
|
|
editor.putBoolean(IS_LOGGED_IN, false); |
|
|
|
editor.apply(); |
|
|
|
Toast.makeText(getBaseContext(), "Logged out successfully!", Toast.LENGTH_LONG).show(); |
|
|
|
|
|
|
|
hideLogout(); |
|
|
|
else |
|
|
|
} else |
|
|
|
hideLogin(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|