handle login/logout and session errors in child fragments

This commit is contained in:
Andrew Dolgov 2011-11-24 16:04:58 +03:00
parent fb86971ca8
commit 7ad712cb41
5 changed files with 94 additions and 70 deletions

View File

@ -1,12 +1,10 @@
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/main_flipper"> android:layout_height="fill_parent" android:id="@+id/main_flipper">
<FrameLayout android:layout_gravity="center_vertical" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/sync_splash"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:id="@+id/loading_container">
<LinearLayout android:layout_width="match_parent" android:gravity="center" android:layout_height="match_parent" android:id="@+id/linearLayout1"> <ProgressBar android:id="@+id/loading_progress" android:layout_height="wrap_content" android:layout_width="wrap_content" style="?android:attr/progressBarStyleLarge"></ProgressBar>
<ProgressBar android:layout_height="wrap_content" android:layout_width="wrap_content" style="?android:attr/progressBarStyleLarge" android:id="@+id/loading_progress"></ProgressBar> <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/loading_message" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/loading_message"></TextView>
<TextView android:id="@+id/loading_message" android:text="@string/loading_message" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
</LinearLayout> </LinearLayout>
</FrameLayout>
<LinearLayout android:layout_height="fill_parent" android:orientation="horizontal" android:id="@+id/main" android:layout_width="fill_parent"> <LinearLayout android:layout_height="fill_parent" android:orientation="horizontal" android:id="@+id/main" android:layout_width="fill_parent">
<FrameLayout android:layout_height="match_parent" android:layout_width="300dp" android:layout_weight="0" android:id="@+id/feeds_fragment"></FrameLayout> <FrameLayout android:layout_height="match_parent" android:layout_width="300dp" android:layout_weight="0" android:id="@+id/feeds_fragment"></FrameLayout>
<FrameLayout android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="0.5" android:id="@+id/headlines_fragment"></FrameLayout> <FrameLayout android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="0.5" android:id="@+id/headlines_fragment"></FrameLayout>
@ -14,4 +12,4 @@
</LinearLayout> </LinearLayout>
</ViewFlipper> </FrameLayout>

View File

@ -1,11 +1,25 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/login"
android:visible="false"
android:icon="@android:drawable/ic_menu_rotate"
android:title="@string/login"
android:showAsAction="ifRoom|withText"/>
<item android:id="@+id/logout"
android:visible="false"
android:title="@string/logout"
android:showAsAction=""/>
<item android:id="@+id/show_feeds" <item android:id="@+id/show_feeds"
android:visible="false"
android:icon="@android:drawable/ic_menu_rotate" android:icon="@android:drawable/ic_menu_rotate"
android:title="@string/menu_all_feeds" android:title="@string/menu_all_feeds"
android:showAsAction=""/> android:showAsAction=""/>
<item android:id="@+id/update" <item android:id="@+id/update"
android:visible="false"
android:icon="@android:drawable/ic_menu_rotate" android:icon="@android:drawable/ic_menu_rotate"
android:title="@string/update" android:title="@string/update"
android:showAsAction="ifRoom|withText"/> android:showAsAction="ifRoom|withText"/>

View File

@ -183,23 +183,9 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
}); });
} }
} else { } else {
JsonObject content = rv.get("content").getAsJsonObject(); MainActivity activity = (MainActivity)getActivity();
activity.login();
if (content != null) { showLoading(false);
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
}
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -39,7 +39,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
private final String TAG = this.getClass().getSimpleName(); private final String TAG = this.getClass().getSimpleName();
protected SharedPreferences m_prefs; protected SharedPreferences m_prefs;
private String m_sessionId; //private String m_sessionId;
private Feed m_feed; private Feed m_feed;
//private int m_activeArticleId; //private int m_activeArticleId;
private int m_selectedArticleId; private int m_selectedArticleId;
@ -55,7 +55,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) { if (savedInstanceState != null) {
m_sessionId = savedInstanceState.getString("sessionId"); //m_sessionId = savedInstanceState.getString("sessionId");
//m_feedId = savedInstanceState.getInt("feedId"); //m_feedId = savedInstanceState.getInt("feedId");
//m_activeArticleId = savedInstanceState.getInt("activeArticleId"); //m_activeArticleId = savedInstanceState.getInt("activeArticleId");
} }
@ -90,10 +90,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
super.onAttach(activity); super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_sessionId = ((MainActivity)activity).getSessionId();
m_feed = ((MainActivity)activity).getActiveFeed(); m_feed = ((MainActivity)activity).getActiveFeed();
m_articleSelectedListener = (OnArticleSelectedListener) activity; m_articleSelectedListener = (OnArticleSelectedListener) activity;
} }
@ -118,10 +115,12 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
req.setApi(m_prefs.getString("ttrss_url", null)); req.setApi(m_prefs.getString("ttrss_url", null));
final String sessionId = ((MainActivity)getActivity()).getSessionId();
HashMap<String,String> map = new HashMap<String,String>() { HashMap<String,String> map = new HashMap<String,String>() {
{ {
put("op", "getHeadlines"); put("op", "getHeadlines");
put("sid", m_sessionId); put("sid", sessionId);
put("feed_id", String.valueOf(m_feed.id)); put("feed_id", String.valueOf(m_feed.id));
put("show_content", "true"); put("show_content", "true");
put("limit", String.valueOf(30)); put("limit", String.valueOf(30));
@ -138,7 +137,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
public void onSaveInstanceState (Bundle out) { public void onSaveInstanceState (Bundle out) {
super.onSaveInstanceState(out); super.onSaveInstanceState(out);
out.putString("sessionId", m_sessionId); //out.putString("sessionId", m_sessionId);
//out.putInt("feedId", m_feedId); //out.putInt("feedId", m_feedId);
//out.putInt("activeArticleId", m_activeArticleId); //out.putInt("activeArticleId", m_activeArticleId);
} }
@ -174,23 +173,9 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
}); });
} }
} else { } else {
JsonObject content = rv.get("content").getAsJsonObject(); MainActivity activity = (MainActivity)getActivity();
activity.login();
if (content != null) { showLoading(false);
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
}
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -212,9 +197,11 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
ApiRequest ar = new ApiRequest(); ApiRequest ar = new ApiRequest();
ar.setApi(m_prefs.getString("ttrss_url", null)); ar.setApi(m_prefs.getString("ttrss_url", null));
final String sessionId = ((MainActivity)getActivity()).getSessionId();
HashMap<String,String> map = new HashMap<String,String>() { HashMap<String,String> map = new HashMap<String,String>() {
{ {
put("sid", m_sessionId); put("sid", sessionId);
put("op", "updateArticle"); put("op", "updateArticle");
put("article_ids", String.valueOf(article.id)); put("article_ids", String.valueOf(article.id));
put("mode", "0"); put("mode", "0");

View File

@ -17,7 +17,6 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.widget.ViewFlipper;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -107,20 +106,7 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
findViewById(R.id.article_fragment).setVisibility(View.GONE); findViewById(R.id.article_fragment).setVisibility(View.GONE);
LoginRequest ar = new LoginRequest(); login();
ar.setApi(m_prefs.getString("ttrss_url", null));
HashMap<String,String> map = new HashMap<String,String>() {
{
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);
} }
@ -199,6 +185,12 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
case R.id.update: case R.id.update:
refreshFeeds(); refreshFeeds();
return true; return true;
case R.id.logout:
logout();
return true;
case R.id.login:
login();
return true;
case R.id.show_feeds: case R.id.show_feeds:
if (getUnreadOnly()) { if (getUnreadOnly()) {
item.setTitle(R.string.menu_unread_feeds); 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); setLoadingStatus(R.string.loading_message, true);
ViewFlipper vf = (ViewFlipper) findViewById(R.id.main_flipper); findViewById(R.id.loading_container).setVisibility(View.INVISIBLE);
findViewById(R.id.main).setVisibility(View.VISIBLE);
if (vf != null) {
vf.showNext();
}
FeedsFragment frag = new FeedsFragment(); FeedsFragment frag = new FeedsFragment();
@ -242,6 +231,11 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
ft.show(frag); ft.show(frag);
ft.commit(); 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) { if (m_refreshTask != null) {
m_refreshTask.cancel(); m_refreshTask.cancel();
m_refreshTask = null; m_refreshTask = null;
@ -272,6 +266,8 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
} else { } else {
setLoadingStatus(R.string.login_failed, false); setLoadingStatus(R.string.login_failed, false);
} }
m_menu.findItem(R.id.login).setVisible(true);
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -325,4 +321,47 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
public Feed getActiveFeed() { public Feed getActiveFeed() {
return m_activeFeed; 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<String,String> map = new HashMap<String,String>() {
{
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);
}
} }