handle login/logout and session errors in child fragments
This commit is contained in:
parent
fb86971ca8
commit
7ad712cb41
@ -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_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:gravity="center" android:layout_height="match_parent" android:id="@+id/linearLayout1">
|
||||
<ProgressBar android:layout_height="wrap_content" android:layout_width="wrap_content" style="?android:attr/progressBarStyleLarge" android:id="@+id/loading_progress"></ProgressBar>
|
||||
<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>
|
||||
</FrameLayout>
|
||||
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:id="@+id/loading_container">
|
||||
<ProgressBar android:id="@+id/loading_progress" android:layout_height="wrap_content" android:layout_width="wrap_content" style="?android:attr/progressBarStyleLarge"></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>
|
||||
</LinearLayout>
|
||||
<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="0dp" android:layout_weight="0.5" android:id="@+id/headlines_fragment"></FrameLayout>
|
||||
@ -14,4 +12,4 @@
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
</ViewFlipper>
|
||||
</FrameLayout>
|
@ -1,11 +1,25 @@
|
||||
|
||||
<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"
|
||||
android:visible="false"
|
||||
android:icon="@android:drawable/ic_menu_rotate"
|
||||
android:title="@string/menu_all_feeds"
|
||||
android:showAsAction=""/>
|
||||
|
||||
<item android:id="@+id/update"
|
||||
android:visible="false"
|
||||
android:icon="@android:drawable/ic_menu_rotate"
|
||||
android:title="@string/update"
|
||||
android:showAsAction="ifRoom|withText"/>
|
||||
|
@ -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();
|
||||
|
@ -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<String,String> map = new HashMap<String,String>() {
|
||||
{
|
||||
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<String,String> map = new HashMap<String,String>() {
|
||||
{
|
||||
put("sid", m_sessionId);
|
||||
put("sid", sessionId);
|
||||
put("op", "updateArticle");
|
||||
put("article_ids", String.valueOf(article.id));
|
||||
put("mode", "0");
|
||||
|
@ -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<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);
|
||||
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<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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user