From 7ad712cb413824b1caae4327e8cc2f691e29d888 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 24 Nov 2011 16:04:58 +0300 Subject: [PATCH] handle login/logout and session errors in child fragments --- res/layout/main.xml | 14 ++--- res/menu/main_menu.xml | 14 +++++ src/org/fox/ttrss/FeedsFragment.java | 20 +----- src/org/fox/ttrss/HeadlinesFragment.java | 37 ++++------- src/org/fox/ttrss/MainActivity.java | 79 ++++++++++++++++++------ 5 files changed, 94 insertions(+), 70 deletions(-) diff --git a/res/layout/main.xml b/res/layout/main.xml index 97a9e9c8..9b327d8a 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -1,12 +1,10 @@ - - - - - - - + + + + @@ -14,4 +12,4 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/res/menu/main_menu.xml b/res/menu/main_menu.xml index f7bec3fb..a4dcb9f3 100644 --- a/res/menu/main_menu.xml +++ b/res/menu/main_menu.xml @@ -1,11 +1,25 @@ + + + + + diff --git a/src/org/fox/ttrss/FeedsFragment.java b/src/org/fox/ttrss/FeedsFragment.java index 3b5fefbc..35c5a490 100644 --- a/src/org/fox/ttrss/FeedsFragment.java +++ b/src/org/fox/ttrss/FeedsFragment.java @@ -183,23 +183,9 @@ public class FeedsFragment extends Fragment implements OnItemClickListener { }); } } else { - JsonObject content = rv.get("content").getAsJsonObject(); - - if (content != null) { - String error = content.get("error").getAsString(); - - /* m_sessionId = null; - - if (error.equals("LOGIN_ERROR")) { - setLoadingStatus(R.string.login_wrong_password, false); - } else if (error.equals("API_DISABLED")) { - setLoadingStatus(R.string.login_api_disabled, false); - } else { - setLoadingStatus(R.string.login_failed, false); - } */ - - // TODO report error back to MainActivity - } + MainActivity activity = (MainActivity)getActivity(); + activity.login(); + showLoading(false); } } catch (Exception e) { e.printStackTrace(); diff --git a/src/org/fox/ttrss/HeadlinesFragment.java b/src/org/fox/ttrss/HeadlinesFragment.java index d2c019ad..e1928d52 100644 --- a/src/org/fox/ttrss/HeadlinesFragment.java +++ b/src/org/fox/ttrss/HeadlinesFragment.java @@ -39,7 +39,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { private final String TAG = this.getClass().getSimpleName(); protected SharedPreferences m_prefs; - private String m_sessionId; + //private String m_sessionId; private Feed m_feed; //private int m_activeArticleId; private int m_selectedArticleId; @@ -55,7 +55,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (savedInstanceState != null) { - m_sessionId = savedInstanceState.getString("sessionId"); + //m_sessionId = savedInstanceState.getString("sessionId"); //m_feedId = savedInstanceState.getInt("feedId"); //m_activeArticleId = savedInstanceState.getInt("activeArticleId"); } @@ -90,10 +90,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { public void onAttach(Activity activity) { super.onAttach(activity); m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); - - m_sessionId = ((MainActivity)activity).getSessionId(); m_feed = ((MainActivity)activity).getActiveFeed(); - m_articleSelectedListener = (OnArticleSelectedListener) activity; } @@ -117,11 +114,13 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { HeadlinesRequest req = new HeadlinesRequest(); req.setApi(m_prefs.getString("ttrss_url", null)); + + final String sessionId = ((MainActivity)getActivity()).getSessionId(); HashMap map = new HashMap() { { put("op", "getHeadlines"); - put("sid", m_sessionId); + put("sid", sessionId); put("feed_id", String.valueOf(m_feed.id)); put("show_content", "true"); put("limit", String.valueOf(30)); @@ -138,7 +137,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { public void onSaveInstanceState (Bundle out) { super.onSaveInstanceState(out); - out.putString("sessionId", m_sessionId); + //out.putString("sessionId", m_sessionId); //out.putInt("feedId", m_feedId); //out.putInt("activeArticleId", m_activeArticleId); } @@ -174,23 +173,9 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { }); } } else { - JsonObject content = rv.get("content").getAsJsonObject(); - - if (content != null) { - String error = content.get("error").getAsString(); - - /* m_sessionId = null; - - if (error.equals("LOGIN_ERROR")) { - setLoadingStatus(R.string.login_wrong_password, false); - } else if (error.equals("API_DISABLED")) { - setLoadingStatus(R.string.login_api_disabled, false); - } else { - setLoadingStatus(R.string.login_failed, false); - } */ - - // TODO report error back to MainActivity - } + MainActivity activity = (MainActivity)getActivity(); + activity.login(); + showLoading(false); } } catch (Exception e) { e.printStackTrace(); @@ -212,9 +197,11 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { ApiRequest ar = new ApiRequest(); ar.setApi(m_prefs.getString("ttrss_url", null)); + final String sessionId = ((MainActivity)getActivity()).getSessionId(); + HashMap map = new HashMap() { { - put("sid", m_sessionId); + put("sid", sessionId); put("op", "updateArticle"); put("article_ids", String.valueOf(article.id)); put("mode", "0"); diff --git a/src/org/fox/ttrss/MainActivity.java b/src/org/fox/ttrss/MainActivity.java index fdfaadb4..9c0e789b 100644 --- a/src/org/fox/ttrss/MainActivity.java +++ b/src/org/fox/ttrss/MainActivity.java @@ -17,7 +17,6 @@ import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; -import android.widget.ViewFlipper; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -106,21 +105,8 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect ft.commit(); findViewById(R.id.article_fragment).setVisibility(View.GONE); - - LoginRequest ar = new LoginRequest(); - ar.setApi(m_prefs.getString("ttrss_url", null)); - HashMap map = new HashMap() { - { - put("op", "login"); - put("user", m_prefs.getString("login", null)); - put("password", m_prefs.getString("password", null)); - } - }; - - ar.execute(map); - - setLoadingStatus(R.string.login_in_progress, true); + login(); } @@ -199,6 +185,12 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect case R.id.update: refreshFeeds(); return true; + case R.id.logout: + logout(); + return true; + case R.id.login: + login(); + return true; case R.id.show_feeds: if (getUnreadOnly()) { item.setTitle(R.string.menu_unread_feeds); @@ -229,11 +221,8 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect setLoadingStatus(R.string.loading_message, true); - ViewFlipper vf = (ViewFlipper) findViewById(R.id.main_flipper); - - if (vf != null) { - vf.showNext(); - } + findViewById(R.id.loading_container).setVisibility(View.INVISIBLE); + findViewById(R.id.main).setVisibility(View.VISIBLE); FeedsFragment frag = new FeedsFragment(); @@ -242,6 +231,11 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect ft.show(frag); ft.commit(); + m_menu.findItem(R.id.login).setVisible(false); + m_menu.findItem(R.id.logout).setVisible(true); + m_menu.findItem(R.id.update).setVisible(true); + m_menu.findItem(R.id.show_feeds).setVisible(true); + if (m_refreshTask != null) { m_refreshTask.cancel(); m_refreshTask = null; @@ -272,6 +266,8 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect } else { setLoadingStatus(R.string.login_failed, false); } + + m_menu.findItem(R.id.login).setVisible(true); } } } catch (Exception e) { @@ -325,4 +321,47 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect public Feed getActiveFeed() { return m_activeFeed; } + + public void logout() { + findViewById(R.id.loading_container).setVisibility(View.VISIBLE); + findViewById(R.id.main).setVisibility(View.INVISIBLE); + + if (m_menu != null) { + m_menu.findItem(R.id.logout).setVisible(false); + m_menu.findItem(R.id.update).setVisible(false); + m_menu.findItem(R.id.show_feeds).setVisible(false); + } + + if (m_refreshTask != null) { + m_refreshTask.cancel(); + m_refreshTask = null; + } + + if (m_refreshTimer != null) { + m_refreshTimer.cancel(); + m_refreshTimer = null; + } + + m_sessionId = null; + } + + public void login() { + + logout(); + + LoginRequest ar = new LoginRequest(); + ar.setApi(m_prefs.getString("ttrss_url", null)); + + HashMap map = new HashMap() { + { + put("op", "login"); + put("user", m_prefs.getString("login", null)); + put("password", m_prefs.getString("password", null)); + } + }; + + ar.execute(map); + + setLoadingStatus(R.string.login_in_progress, true); + } } \ No newline at end of file