implement placeholder headlinesfragment

fix reloading feedsfragment on rotate
This commit is contained in:
Andrew Dolgov 2011-09-08 16:45:08 +04:00
parent a14e9cd14e
commit 30eea4c5e4
9 changed files with 208 additions and 31 deletions

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/headlines_fragment">
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_weight="1" android:background="?headlinesBackground"
android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/headlines"></ListView>
</LinearLayout>
<ProgressBar android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center" android:id="@+id/loading_progress"/>
</FrameLayout>

View File

@ -1,9 +1,15 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal">
<LinearLayout android:layout_weight="1.5" android:layout_height="match_parent" android:id="@+id/linearLayout4" android:layout_width="match_parent">
<LinearLayout android:layout_height="match_parent" android:id="@+id/feeds_container" android:layout_width="match_parent">
<LinearLayout android:layout_height="match_parent" android:id="@+id/feeds_container"
android:layout_weight="1"
android:layout_width="match_parent">
</LinearLayout>
<ImageView
@ -19,3 +25,7 @@
</LinearLayout>
<TextView android:layout_gravity="center" android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/no_unread_feeds" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/no_unread_feeds"></TextView>
</FrameLayout>

View File

@ -6,7 +6,8 @@
android:showAsAction="ifRoom|withText"/>
<item android:id="@+id/logout"
android:title="@string/logout"
android:showAsAction=""/>
android:title="@string/logout"
android:icon="@android:drawable/presence_offline"
android:showAsAction="ifRoom|withText"/>
</menu>

View File

@ -3,4 +3,5 @@
<attr name="feedlistDivider" format="reference|color" />
<attr name="feedlistBackground" format="reference|color" />
<attr name="unreadCounterColor" format="reference|color" />
<attr name="headlinesBackground" format="reference|color" />
</resources>

View File

@ -23,4 +23,5 @@
<string name="login_api_disabled">Login failed: API disabled.</string>
<string name="login_wrong_password">Login failed: username or password incorrect.</string>
<string name="login_success">Logged in.</string>
<string name="no_unread_feeds">No unread feeds.</string>
</resources>

View File

@ -3,12 +3,22 @@
<item name="feedlistDivider">@android:drawable/divider_horizontal_bright</item>
<item name="feedlistBackground">#fafafa</item>
<item name="unreadCounterColor">#0000ff</item>
<item name="headlinesBackground">#ffffff</item>
</style>
<style name="DarkTheme" parent="android:Theme.Holo">
<item name="feedlistDivider">@android:drawable/divider_horizontal_dark</item>
<item name="feedlistBackground">#101010</item>
<item name="unreadCounterColor">#0000ff</item>
<item name="headlinesBackground">#000000</item>
</style>
<style name="SelectedFeed">
<item name="android:textStyle">bold</item>
</style>
<style name="Feed">
<item name="android:textStyle">normal</item>
</style>
</resources>

View File

@ -3,18 +3,23 @@ package org.fox.ttrss;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
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;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
@ -24,25 +29,25 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
public class FeedsFragment extends Fragment {
public class FeedsFragment extends Fragment implements OnItemClickListener {
private final String TAG = this.getClass().getSimpleName();
protected ArrayList<Feed> m_feeds = new ArrayList<Feed>();
protected FeedsListAdapter m_adapter;
protected SharedPreferences m_prefs;
protected String m_sessionId;
protected int m_activeFeedId;
protected long m_lastUpdate;
protected Gson m_gson = new Gson();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
m_sessionId = savedInstanceState.getString("sessionId");
m_sessionId = savedInstanceState.getString("sessionId");
m_activeFeedId = savedInstanceState.getInt("activeFeedId");
m_lastUpdate = savedInstanceState.getLong("lastUpdate");
}
View view = inflater.inflate(R.layout.feeds_fragment, container, false);
@ -52,19 +57,34 @@ public class FeedsFragment extends Fragment {
ListView list = (ListView) view.findViewById(R.id.feeds);
if (list != null) {
list.setAdapter(m_adapter);
list.setAdapter(m_adapter);
list.setOnItemClickListener(this);
}
return view;
}
@Override
public void onStart() {
super.onStart();
if (new Date().getTime() - m_lastUpdate > 30*1000) {
refresh();
} else {
//
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
refresh();
}
public void initialize(String sessionId) {
@ -77,7 +97,9 @@ public class FeedsFragment extends Fragment {
@Override
protected void onPostExecute(JsonElement result) {
if (result != null) {
try {
try {
m_lastUpdate = new Date().getTime();
JsonObject rv = result.getAsJsonObject();
int status = rv.get("status").getAsInt();
@ -98,9 +120,20 @@ public class FeedsFragment extends Fragment {
m_adapter.notifyDataSetChanged();
View v = getView().findViewById(R.id.loading_progress);
/* if (getView() != null) {
View v = getView().findViewById(R.id.loading_progress);
if (v != null) v.setVisibility(View.GONE);
if (v != null) v.setVisibility(View.GONE);
v = getView().findViewById(R.id.no_unread_feeds);
if (v != null) {
if (m_feeds.size() > 0)
v.setVisibility(View.INVISIBLE);
else
v.setVisibility(View.VISIBLE);
}
} */
return;
}
@ -119,9 +152,8 @@ public class FeedsFragment extends Fragment {
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
};
@ -141,6 +173,8 @@ public class FeedsFragment extends Fragment {
super.onSaveInstanceState(out);
out.putString("sessionId", m_sessionId);
out.putInt("activeFeedId", m_activeFeedId);
out.putLong("lastUpdate", m_lastUpdate);
}
private class FeedsListAdapter extends ArrayAdapter<Feed> {
@ -156,7 +190,7 @@ public class FeedsFragment extends Fragment {
View v = convertView;
Feed item = items.get(position);
Feed feed = items.get(position);
if (v == null) {
LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@ -166,13 +200,19 @@ public class FeedsFragment extends Fragment {
TextView title = (TextView) v.findViewById(R.id.title);
if (title != null) {
title.setText(item.title);
title.setText(feed.title);
if (feed.id == m_activeFeedId) {
title.setTextAppearance(getContext(), R.style.SelectedFeed);
} else {
title.setTextAppearance(getContext(), R.style.Feed);
}
}
TextView unread = (TextView) v.findViewById(R.id.unread_counter);
if (unread != null) {
unread.setText(String.valueOf(item.unread));
unread.setText(String.valueOf(feed.unread));
}
return v;
@ -190,7 +230,42 @@ public class FeedsFragment extends Fragment {
@Override
public int compareTo(Feed feed) {
return feed.unread - this.unread;
if (feed.unread != this.unread)
return feed.unread - this.unread;
else
return this.title.compareTo(feed.title);
}
}
@Override
public void onItemClick(AdapterView<?> av, View view, int position, long id) {
ListView list = (ListView)getActivity().findViewById(R.id.feeds);
if (list != null) {
Feed feed = (Feed) list.getItemAtPosition(position);
if (feed != null) {
Log.d(TAG, "clicked on feed " + feed.id);
viewFeed(feed.id);
}
}
}
private void viewFeed(int feedId) {
m_activeFeedId = feedId;
FragmentTransaction ft = getFragmentManager().beginTransaction();
HeadlinesFragment frag = new HeadlinesFragment();
frag.initialize(m_sessionId, feedId);
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
ft.replace(R.id.headlines_container, frag);
ft.commit();
m_adapter.notifyDataSetChanged();
}
}

View File

@ -0,0 +1,51 @@
package org.fox.ttrss;
import android.app.Activity;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HeadlinesFragment extends Fragment {
private final String TAG = this.getClass().getSimpleName();
protected String m_sessionId;
protected int m_feedId;
protected SharedPreferences m_prefs;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
m_sessionId = savedInstanceState.getString("sessionId");
}
View view = inflater.inflate(R.layout.headlines_fragment, container, false);
/* m_adapter = new FeedsListAdapter(getActivity(), R.id.feeds_row, m_feeds);
ListView list = (ListView) view.findViewById(R.id.feeds);
if (list != null) {
list.setAdapter(m_adapter);
list.setOnItemClickListener(this);
} */
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
}
public void initialize(String sessionId, int feedId) {
m_sessionId = sessionId;
m_feedId = feedId;
}
}

View File

@ -6,14 +6,18 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends Activity {
private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs;
private String m_themeName = "";
private String m_sessionId;
private boolean m_feedsOpened = false;
/** Called when the activity is first created. */
@Override
@ -34,20 +38,30 @@ public class MainActivity extends Activity {
if (extras != null) {
m_sessionId = extras.getString("sessionId");
} else if (savedInstanceState != null) {
}
if (savedInstanceState != null) {
m_sessionId = savedInstanceState.getString("sessionId");
m_feedsOpened = savedInstanceState.getBoolean("feedsOpened");
Log.d(TAG, "FU: " + m_feedsOpened);
}
setContentView(R.layout.main);
FragmentTransaction ft = getFragmentManager().beginTransaction();
FeedsFragment frag = new FeedsFragment();
if (!m_feedsOpened) {
Log.d(TAG, "Opening feeds fragment...");
FragmentTransaction ft = getFragmentManager().beginTransaction();
FeedsFragment frag = new FeedsFragment();
frag.initialize(m_sessionId);
frag.initialize(m_sessionId);
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
ft.replace(R.id.feeds_container, frag);
ft.commit();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
ft.replace(R.id.feeds_container, frag);
ft.commit();
m_feedsOpened = true;
}
}
@ -56,6 +70,7 @@ public class MainActivity extends Activity {
super.onSaveInstanceState(out);
out.putString("sessionId", m_sessionId);
out.putBoolean("feedsOpened", m_feedsOpened);
}
@Override