summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2011-11-23 19:38:21 +0300
committerAndrew Dolgov <[email protected]>2011-11-23 19:38:21 +0300
commitd391312d04027d1d8e8983be76e10ef684018927 (patch)
tree0aa18766bf0794d23975891cf734544c505427ce
parentacd27869614a60610a93f59dbf6869059ee5dc81 (diff)
rework fragment interaction through listeners
articlefragment now works
-rw-r--r--res/layout/article_fragment.xml4
-rw-r--r--res/layout/feeds_fragment.xml2
-rw-r--r--res/layout/main.xml22
-rw-r--r--src/org/fox/ttrss/ArticleFragment.java49
-rw-r--r--src/org/fox/ttrss/FeedsFragment.java74
-rw-r--r--src/org/fox/ttrss/HeadlinesFragment.java54
-rw-r--r--src/org/fox/ttrss/MainActivity.java88
7 files changed, 159 insertions, 134 deletions
diff --git a/res/layout/article_fragment.xml b/res/layout/article_fragment.xml
index cec8e0b2..31a105e4 100644
--- a/res/layout/article_fragment.xml
+++ b/res/layout/article_fragment.xml
@@ -1,7 +1,7 @@
<LinearLayout android:id="@+id/article_fragment" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
- <LinearLayout android:layout_gravity="center_vertical" android:padding="6dip" android:background="#e0e0e0" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/article_header">
- <TextView android:layout_weight="1" android:text="TextView" android:layout_height="wrap_content" android:textColor="#909090" android:layout_width="match_parent" android:id="@+id/title"></TextView>
+ <LinearLayout android:layout_gravity="center_vertical" android:padding="3dip" android:background="#e0e0e0" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/article_header">
+ <TextView android:singleLine="true" android:layout_weight="1" android:text="TextView" android:layout_height="wrap_content" android:textColor="#909090" android:ellipsize="end" android:layout_width="match_parent" android:id="@+id/title"></TextView>
<Button android:text="Close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/close_button"></Button>
</LinearLayout>
<WebView android:padding="3dip" android:layout_width="match_parent" android:id="@+id/content" android:layout_height="match_parent"></WebView>
diff --git a/res/layout/feeds_fragment.xml b/res/layout/feeds_fragment.xml
index c66895b3..32f2d229 100644
--- a/res/layout/feeds_fragment.xml
+++ b/res/layout/feeds_fragment.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="fill_parent" android:id="@+id/feeds_container" android:layout_width="match_parent">
+ android:layout_height="fill_parent" android:layout_width="match_parent" android:id="@+id/feeds_fragment">
<LinearLayout android:id="@+id/loading_container" android:gravity="center" android:layout_height="match_parent" android:layout_width="match_parent">
<ProgressBar android:layout_width="wrap_content" style="?android:attr/progressBarStyleLarge" android:id="@+id/loading_progress" android:layout_height="wrap_content"></ProgressBar>
<TextView android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/loading_message" android:layout_height="wrap_content" ></TextView>
diff --git a/res/layout/main.xml b/res/layout/main.xml
index fb049eeb..9593924a 100644
--- a/res/layout/main.xml
+++ b/res/layout/main.xml
@@ -1,22 +1,16 @@
<ViewFlipper 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_height="match_parent" android:layout_width="match_parent" android:layout_gravity="center_vertical" android:id="@+id/sync_splash">
- <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/linearLayout1" android:gravity="center">
- <ProgressBar android:id="@+id/loading_progress" style="?android:attr/progressBarStyleLarge" android:layout_height="wrap_content" android:layout_width="wrap_content"></ProgressBar>
- <TextView android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:text="@string/loading_message" android:id="@+id/loading_message"></TextView>
+ <FrameLayout android:layout_height="match_parent" android:id="@+id/sync_splash" android:layout_width="match_parent" android:layout_gravity="center_vertical">
+ <LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout1" android:gravity="center" android:layout_width="match_parent">
+ <ProgressBar style="?android:attr/progressBarStyleLarge" android:id="@+id/loading_progress" android:layout_height="wrap_content" android:layout_width="wrap_content"></ProgressBar>
+ <TextView android:id="@+id/loading_message" android:text="@string/loading_message" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
</LinearLayout>
</FrameLayout>
- <LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/main" android:orientation="horizontal">
- <fragment android:layout_weight="0" android:id="@+id/feeds_fragment" android:layout_height="match_parent" android:layout_width="300dp" android:name="org.fox.ttrss.FeedsFragment">
- <!-- Preview: layout=@layout/feeds_fragment -->
- </fragment>
- <fragment android:layout_weight="1" android:id="@+id/headlines_fragment" android:layout_height="match_parent" android:layout_width="wrap_content" android:name="org.fox.ttrss.HeadlinesFragment">
- <!-- Preview: layout=@layout/headlines_fragment -->
- </fragment>
- <fragment android:layout_weight="0" android:id="@+id/article_fragment" android:layout_height="match_parent" android:name="org.fox.ttrss.ArticleFragment" android:layout_width="500dp">
- <!-- Preview: layout=@layout/article_fragment -->
- </fragment>
+ <LinearLayout android:layout_height="fill_parent" android:id="@+id/main" android:layout_width="fill_parent" android:orientation="horizontal">
+ <LinearLayout android:id="@+id/feeds_fragment" android:layout_weight="0" android:layout_height="match_parent" android:orientation="vertical" android:layout_width="300dp"></LinearLayout>
+ <LinearLayout android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/headlines_fragment" android:orientation="vertical" android:layout_width="match_parent"></LinearLayout>
+ <LinearLayout android:id="@+id/article_fragment" android:layout_weight="0" android:layout_height="match_parent" android:orientation="vertical" android:layout_width="500dp"></LinearLayout>
</LinearLayout>
diff --git a/src/org/fox/ttrss/ArticleFragment.java b/src/org/fox/ttrss/ArticleFragment.java
index b2323fa5..ab92f9bf 100644
--- a/src/org/fox/ttrss/ArticleFragment.java
+++ b/src/org/fox/ttrss/ArticleFragment.java
@@ -1,15 +1,13 @@
package org.fox.ttrss;
-import java.sql.SQLData;
+import java.net.URLEncoder;
+
+import org.jsoup.Jsoup;
import android.app.Activity;
import android.app.Fragment;
import android.content.SharedPreferences;
-import android.database.Cursor;
-import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
-import android.preference.PreferenceManager;
-import android.provider.BaseColumns;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@@ -22,30 +20,42 @@ public class ArticleFragment extends Fragment {
protected SharedPreferences m_prefs;
- private int m_articleId;
+ //private int m_articleId;
private String m_sessionId;
+ private Article m_article;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
m_sessionId = savedInstanceState.getString("sessionId");
- m_articleId = savedInstanceState.getInt("articleId");
+ //m_articleId = savedInstanceState.getInt("articleId");
}
View view = inflater.inflate(R.layout.article_fragment, container, false);
+ if (m_article != null) {
+
+ TextView title = (TextView)view.findViewById(R.id.title);
+
+ if (title != null) {
+ title.setText(m_article.title);
+ }
+
+ WebView web = (WebView)view.findViewById(R.id.content);
+
+ if (web != null) {
+
+ // this is ridiculous
+ String content = "<html><body>" + URLEncoder.encode(m_article.content).replace('+', ' ') + "</body></html>";
+
+ web.loadData(content, "text/html", "utf-8");
+ }
+ }
+
return view;
}
- public void initialize(String sessionId, int articleId, SharedPreferences prefs) {
- m_articleId = articleId;
- m_prefs = prefs;
- m_sessionId = sessionId;
-
- Log.d(TAG, "Article: " + articleId);
- }
-
@Override
public void onDestroy() {
super.onDestroy();
@@ -56,13 +66,18 @@ public class ArticleFragment extends Fragment {
super.onSaveInstanceState(out);
out.putString("sessionId", m_sessionId);
- out.putInt("articleId", m_articleId);
+ //out.putInt("articleId", m_articleId);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
- m_prefs = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
+
+ m_sessionId = ((MainActivity)activity).getSessionId();
+ m_article = ((MainActivity)activity).getSelectedArticle();
+
+ //m_prefs = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
+
}
}
diff --git a/src/org/fox/ttrss/FeedsFragment.java b/src/org/fox/ttrss/FeedsFragment.java
index b321b48e..40c559ae 100644
--- a/src/org/fox/ttrss/FeedsFragment.java
+++ b/src/org/fox/ttrss/FeedsFragment.java
@@ -7,11 +7,11 @@ import java.util.List;
import android.app.Activity;
import android.app.Fragment;
-import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
+import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -32,16 +32,32 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs;
private String m_sessionId;
- private int m_activeFeedId;
+ //private int m_activeFeedId;
private FeedListAdapter m_adapter;
private List<Feed> m_feeds = new ArrayList<Feed>();
+ private OnFeedSelectedListener m_feedSelectedListener;
+
+ public interface OnFeedSelectedListener {
+ public void onFeedSelected(Feed feed);
+ }
+
+ public void showLoading(boolean show) {
+ View v = getView();
+
+ if (v != null) {
+ v = v.findViewById(R.id.loading_container);
+
+ if (v != null)
+ v.setVisibility(show ? View.VISIBLE : View.GONE);
+ }
+ }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
-
+
if (savedInstanceState != null) {
- m_sessionId = savedInstanceState.getString("sessionId");
- m_activeFeedId = savedInstanceState.getInt("activeFeedId");
+ //m_sessionId = savedInstanceState.getString("sessionId");
+ //m_activeFeedId = savedInstanceState.getInt("activeFeedId");
}
View view = inflater.inflate(R.layout.feeds_fragment, container, false);
@@ -52,6 +68,9 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
list.setOnItemClickListener(this);
+ if (m_sessionId != null)
+ refresh();
+
return view;
}
@@ -63,7 +82,12 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
+
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
+ m_feedSelectedListener = (OnFeedSelectedListener) activity;
+
+ m_sessionId = ((MainActivity)activity).getSessionId();
+
}
@Override
@@ -71,29 +95,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
super.onSaveInstanceState(out);
out.putString("sessionId", m_sessionId);
- out.putInt("activeFeedId", m_activeFeedId);
- }
-
- public void setActiveFeedId(int feedId) {
- m_activeFeedId = feedId;
- }
-
- public void viewFeed(int feedId) {
- //FragmentManager fm = getFragmentManager();
- //HeadlinesFragment hf = (HeadlinesFragment) fm.findFragmentById(R.id.headlines_fragment);
-
- HeadlinesFragment hf = new HeadlinesFragment();
- hf.initialize(m_sessionId, feedId, m_prefs);
-
- if (hf != null) {
- setActiveFeedId(feedId);
-
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
- ft.show(getFragmentManager().findFragmentById(R.id.headlines_fragment));
- ft.replace(R.id.headlines_fragment, hf);
- ft.commit();
- }
+ //out.putInt("activeFeedId", m_activeFeedId);
}
@Override
@@ -102,17 +104,10 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
if (list != null) {
Feed feed = (Feed)list.getItemAtPosition(position);
-
- viewFeed(feed.id);
+ m_feedSelectedListener.onFeedSelected(feed);
}
}
- public void initialize(String sessionId) {
- m_sessionId = sessionId;
-
- refresh();
- }
-
public void refresh() {
FeedsRequest fr = new FeedsRequest();
@@ -170,8 +165,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
m_adapter.notifyDataSetInvalidated();
- View v = getView().findViewById(R.id.loading_container);
- v.setVisibility(View.GONE);
+ showLoading(false);
}
});
}
@@ -221,6 +215,8 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
+ Feed active_feed = ((MainActivity)getActivity()).getActiveFeed();
+
View v = convertView;
Feed feed = items.get(position);
@@ -235,7 +231,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
if (tt != null) {
tt.setText(feed.title);
- if (feed.id == m_activeFeedId)
+ if (active_feed != null && feed.id == active_feed.id)
tt.setTextAppearance(getContext(), R.style.SelectedFeed);
else
tt.setTextAppearance(getContext(), R.style.Feed);
diff --git a/src/org/fox/ttrss/HeadlinesFragment.java b/src/org/fox/ttrss/HeadlinesFragment.java
index be7c96eb..10cabeb1 100644
--- a/src/org/fox/ttrss/HeadlinesFragment.java
+++ b/src/org/fox/ttrss/HeadlinesFragment.java
@@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
+import org.fox.ttrss.FeedsFragment.OnFeedSelectedListener;
import org.jsoup.Jsoup;
import android.app.Activity;
@@ -34,19 +35,23 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
protected SharedPreferences m_prefs;
private String m_sessionId;
- private int m_feedId;
- private int m_activeArticleId;
+ private Feed m_feed;
+ //private int m_activeArticleId;
private ArticleListAdapter m_adapter;
private List<Article> m_articles = new ArrayList<Article>();
+ private OnArticleSelectedListener m_articleSelectedListener;
+ public interface OnArticleSelectedListener {
+ public void onArticleSelected(Article article);
+ }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
m_sessionId = savedInstanceState.getString("sessionId");
- m_feedId = savedInstanceState.getInt("feedId");
- m_activeArticleId = savedInstanceState.getInt("activeArticleId");
+ //m_feedId = savedInstanceState.getInt("feedId");
+ //m_activeArticleId = savedInstanceState.getInt("activeArticleId");
}
View view = inflater.inflate(R.layout.headlines_fragment, container, false);
@@ -56,6 +61,8 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
list.setAdapter(m_adapter);
list.setOnItemClickListener(this);
+ if (m_feed != null) refresh();
+
return view;
}
@@ -73,8 +80,12 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
@Override
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;
}
@Override
@@ -83,35 +94,10 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
if (list != null) {
Article article = (Article)list.getItemAtPosition(position);
-
- viewArticle(article.id);
+ m_articleSelectedListener.onArticleSelected(article);
}
}
- public void viewArticle(int articleId) {
- ArticleFragment frag = new ArticleFragment();
- frag.initialize(m_sessionId, articleId, m_prefs);
-
- if (frag != null) {
- m_activeArticleId = articleId;
-
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
- ft.show(getFragmentManager().findFragmentById(R.id.article_fragment));
- //ft.hide(getFragmentManager().findFragmentById(R.id.feeds_fragment));
- ft.replace(R.id.article_fragment, frag);
- ft.commit();
- }
-
- }
-
- public void initialize(String sessionId, int feedId, SharedPreferences prefs) {
- m_sessionId = sessionId;
- m_feedId = feedId;
- m_prefs = prefs;
- refresh();
- }
-
public void refresh() {
HeadlinesRequest req = new HeadlinesRequest();
@@ -121,7 +107,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
{
put("op", "getHeadlines");
put("sid", m_sessionId);
- put("feed_id", String.valueOf(m_feedId));
+ put("feed_id", String.valueOf(m_feed.id));
put("show_content", "true");
put("limit", String.valueOf(30));
put("offset", String.valueOf(0));
@@ -137,8 +123,8 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
super.onSaveInstanceState(out);
out.putString("sessionId", m_sessionId);
- out.putInt("feedId", m_feedId);
- out.putInt("activeArticleId", m_activeArticleId);
+ //out.putInt("feedId", m_feedId);
+ //out.putInt("activeArticleId", m_activeArticleId);
}
private class HeadlinesRequest extends ApiRequest {
diff --git a/src/org/fox/ttrss/MainActivity.java b/src/org/fox/ttrss/MainActivity.java
index 1fa20f7e..f458af79 100644
--- a/src/org/fox/ttrss/MainActivity.java
+++ b/src/org/fox/ttrss/MainActivity.java
@@ -21,12 +21,14 @@ import android.widget.ViewFlipper;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
-public class MainActivity extends Activity {
+public class MainActivity extends Activity implements FeedsFragment.OnFeedSelectedListener, HeadlinesFragment.OnArticleSelectedListener {
private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs;
private String m_themeName = "";
- protected String m_sessionId;
+ private String m_sessionId;
+ private Article m_selectedArticle;
+ private Feed m_activeFeed;
protected MenuItem m_syncStatus;
@@ -61,22 +63,15 @@ public class MainActivity extends Activity {
setContentView(R.layout.main);
- /* HeadlinesFragment hf = new HeadlinesFragment();
- FeedsFragment ff = new FeedsFragment();
+ HeadlinesFragment hf = new HeadlinesFragment();
ArticleFragment af = new ArticleFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
-
- ft.add(R.id.main, ff);
- ft.add(R.id.main, hf);
- ft.add(R.id.main, af);
- ft.hide(hf);
+ ft.replace(R.id.feeds_fragment, new FeedsFragment());
+ ft.replace(R.id.headlines_fragment, hf);
+ ft.replace(R.id.article_fragment, af);
ft.hide(af);
- ft.commit(); */
-
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- ft.hide(getFragmentManager().findFragmentById(R.id.headlines_fragment));
- ft.hide(getFragmentManager().findFragmentById(R.id.article_fragment));
+ ft.hide(hf);
ft.commit();
LoginRequest ar = new LoginRequest();
@@ -93,13 +88,7 @@ public class MainActivity extends Activity {
ar.execute(map);
setLoadingStatus(R.string.login_in_progress, true);
-
- /* ViewFlipper vf = (ViewFlipper) findViewById(R.id.main_flipper);
-
- if (vf != null) {
- vf.showNext();
- } */
-
+
}
public void setLoadingStatus(int status, boolean showProgress) {
@@ -176,18 +165,20 @@ public class MainActivity extends Activity {
setLoadingStatus(R.string.loading_message, true);
- FragmentManager fm = getFragmentManager();
- FeedsFragment ff = (FeedsFragment) fm.findFragmentById(R.id.feeds_fragment);
-
- if (ff != null) {
- ff.initialize(m_sessionId);
- }
+ //FragmentManager fm = getFragmentManager();
+ //FeedsFragment ff = (FeedsFragment) fm.findFragmentById(R.id.feeds_fragment);
ViewFlipper vf = (ViewFlipper) findViewById(R.id.main_flipper);
if (vf != null) {
vf.showNext();
}
+
+ FeedsFragment frag = new FeedsFragment();
+
+ FragmentTransaction ft = getFragmentManager().beginTransaction();
+ ft.replace(R.id.feeds_fragment, frag);
+ ft.commit();
}
} else {
JsonObject content = rv.get("content").getAsJsonObject();
@@ -212,4 +203,47 @@ public class MainActivity extends Activity {
}
}
}
+
+ @Override
+ public void onFeedSelected(Feed feed) {
+ Log.d(TAG, "Selected feed: " + feed.toString());
+
+ m_activeFeed = feed;
+
+ HeadlinesFragment hf = new HeadlinesFragment();
+ //hf.initialize(m_sessionId, feed.id, m_prefs);
+
+ FragmentTransaction ft = getFragmentManager().beginTransaction();
+ ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
+ ft.show(getFragmentManager().findFragmentById(R.id.headlines_fragment));
+ ft.replace(R.id.headlines_fragment, hf);
+ ft.addToBackStack(null);
+ ft.commit();
+ }
+
+ public Article getSelectedArticle() {
+ return m_selectedArticle;
+ }
+
+ @Override
+ public void onArticleSelected(Article article) {
+ Log.d(TAG, "Selected article: " + article.toString());
+
+ m_selectedArticle = article;
+
+ ArticleFragment frag = new ArticleFragment();
+
+ FragmentTransaction ft = getFragmentManager().beginTransaction();
+ ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
+ ft.show(getFragmentManager().findFragmentById(R.id.article_fragment));
+ //ft.hide(getFragmentManager().findFragmentById(R.id.feeds_fragment));
+ ft.replace(R.id.article_fragment, frag);
+ ft.addToBackStack(null);
+ ft.commit();
+
+ }
+
+ public Feed getActiveFeed() {
+ return m_activeFeed;
+ }
} \ No newline at end of file