2011-09-08 11:28:38 +00:00
|
|
|
package org.fox.ttrss;
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.ArrayList;
|
2011-11-23 17:07:13 +00:00
|
|
|
import java.util.Collections;
|
2011-11-23 10:55:05 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.PreferenceManager;
|
2011-11-25 18:37:02 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2011-09-08 12:45:08 +00:00
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
2011-11-23 10:55:05 +00:00
|
|
|
import android.widget.ArrayAdapter;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.widget.ListView;
|
2011-11-23 10:55:05 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
2011-09-08 11:28:38 +00:00
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
public class FeedsFragment extends Fragment implements OnItemClickListener {
|
2011-09-08 11:28:38 +00:00
|
|
|
private final String TAG = this.getClass().getSimpleName();
|
2011-11-22 12:13:02 +00:00
|
|
|
private SharedPreferences m_prefs;
|
2011-11-23 10:55:05 +00:00
|
|
|
private FeedListAdapter m_adapter;
|
2011-11-25 10:13:10 +00:00
|
|
|
private FeedList m_feeds = new FeedList();
|
2011-11-23 16:38:21 +00:00
|
|
|
private OnFeedSelectedListener m_feedSelectedListener;
|
2011-11-24 10:01:51 +00:00
|
|
|
private int m_selectedFeedId;
|
2011-11-23 16:38:21 +00:00
|
|
|
|
|
|
|
public interface OnFeedSelectedListener {
|
|
|
|
public void onFeedSelected(Feed feed);
|
|
|
|
}
|
2011-11-24 05:59:11 +00:00
|
|
|
|
2011-11-23 16:38:21 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2011-11-22 14:30:09 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2011-11-23 16:38:21 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
if (savedInstanceState != null) {
|
2011-11-25 10:13:10 +00:00
|
|
|
m_selectedFeedId = savedInstanceState.getInt("selectedFeedId");
|
|
|
|
m_feeds = savedInstanceState.getParcelable("feeds");
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
View view = inflater.inflate(R.layout.feeds_fragment, container, false);
|
2011-11-22 14:30:09 +00:00
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
ListView list = (ListView)view.findViewById(R.id.feeds);
|
|
|
|
m_adapter = new FeedListAdapter(getActivity(), R.layout.feeds_row, (ArrayList<Feed>)m_feeds);
|
|
|
|
list.setAdapter(m_adapter);
|
|
|
|
list.setOnItemClickListener(this);
|
2011-11-25 10:13:10 +00:00
|
|
|
|
|
|
|
if (m_feeds == null || m_feeds.size() == 0)
|
2011-11-23 16:38:21 +00:00
|
|
|
refresh();
|
2011-11-25 10:13:10 +00:00
|
|
|
else
|
|
|
|
view.findViewById(R.id.loading_container).setVisibility(View.GONE);
|
2011-11-23 16:38:21 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2011-09-09 06:02:31 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2011-09-08 12:45:08 +00:00
|
|
|
}
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
2011-09-08 16:05:00 +00:00
|
|
|
super.onAttach(activity);
|
2011-11-23 16:38:21 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
2011-11-23 16:38:21 +00:00
|
|
|
m_feedSelectedListener = (OnFeedSelectedListener) activity;
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
@Override
|
2011-09-08 11:28:38 +00:00
|
|
|
public void onSaveInstanceState (Bundle out) {
|
|
|
|
super.onSaveInstanceState(out);
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-11-25 10:13:10 +00:00
|
|
|
out.putInt("selectedFeedId", m_selectedFeedId);
|
|
|
|
out.putParcelable("feeds", m_feeds);
|
2011-11-23 12:14:41 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> av, View view, int position, long id) {
|
2011-11-23 10:55:05 +00:00
|
|
|
ListView list = (ListView)av;
|
|
|
|
|
|
|
|
if (list != null) {
|
|
|
|
Feed feed = (Feed)list.getItemAtPosition(position);
|
2011-11-23 16:38:21 +00:00
|
|
|
m_feedSelectedListener.onFeedSelected(feed);
|
2011-11-24 10:01:51 +00:00
|
|
|
m_selectedFeedId = feed.id;
|
|
|
|
m_adapter.notifyDataSetChanged();
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
2011-11-22 14:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void refresh() {
|
2011-11-23 10:55:05 +00:00
|
|
|
FeedsRequest fr = new FeedsRequest();
|
2011-11-22 14:30:09 +00:00
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
fr.setApi(m_prefs.getString("ttrss_url", null));
|
|
|
|
|
2011-11-23 17:19:35 +00:00
|
|
|
final String sessionId = ((MainActivity)getActivity()).getSessionId();
|
2011-11-24 05:59:11 +00:00
|
|
|
final boolean unreadOnly = ((MainActivity)getActivity()).getUnreadOnly();
|
2011-11-23 17:19:35 +00:00
|
|
|
|
|
|
|
if (sessionId != null) {
|
|
|
|
|
|
|
|
HashMap<String,String> map = new HashMap<String,String>() {
|
|
|
|
{
|
|
|
|
put("op", "getFeeds");
|
|
|
|
put("sid", sessionId);
|
|
|
|
put("cat_id", "-3");
|
2011-11-24 05:59:11 +00:00
|
|
|
if (unreadOnly) {
|
|
|
|
put("unread_only", String.valueOf(unreadOnly));
|
|
|
|
}
|
2011-11-23 17:19:35 +00:00
|
|
|
}
|
|
|
|
};
|
2011-11-23 10:55:05 +00:00
|
|
|
|
2011-11-23 17:19:35 +00:00
|
|
|
fr.execute(map);
|
|
|
|
|
|
|
|
}
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setLoadingStatus(int status, boolean showProgress) {
|
|
|
|
TextView tv = (TextView)getView().findViewById(R.id.loading_message);
|
|
|
|
|
|
|
|
if (tv != null) {
|
|
|
|
tv.setText(status);
|
|
|
|
}
|
2011-11-22 14:30:09 +00:00
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
View pb = getView().findViewById(R.id.loading_progress);
|
|
|
|
|
|
|
|
if (pb != null) {
|
|
|
|
pb.setVisibility(showProgress ? View.VISIBLE : View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class FeedsRequest extends ApiRequest {
|
|
|
|
|
|
|
|
protected void onPostExecute(JsonElement result) {
|
|
|
|
if (result != null) {
|
|
|
|
try {
|
|
|
|
JsonObject rv = result.getAsJsonObject();
|
2011-11-22 14:30:09 +00:00
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
Gson gson = new Gson();
|
|
|
|
|
|
|
|
int status = rv.get("status").getAsInt();
|
|
|
|
|
|
|
|
if (status == 0) {
|
|
|
|
JsonArray content = rv.get("content").getAsJsonArray();
|
|
|
|
if (content != null) {
|
|
|
|
Type listType = new TypeToken<List<Feed>>() {}.getType();
|
|
|
|
final List<Feed> feeds = gson.fromJson(content, listType);
|
|
|
|
|
|
|
|
getActivity().runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
m_feeds.clear();
|
|
|
|
|
|
|
|
for (Feed f : feeds)
|
|
|
|
m_feeds.add(f);
|
|
|
|
|
2011-11-23 17:07:13 +00:00
|
|
|
Collections.sort(m_feeds);
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
m_adapter.notifyDataSetInvalidated();
|
|
|
|
|
2011-11-23 16:38:21 +00:00
|
|
|
showLoading(false);
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2011-11-24 13:04:58 +00:00
|
|
|
MainActivity activity = (MainActivity)getActivity();
|
|
|
|
activity.login();
|
|
|
|
showLoading(false);
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2011-11-25 10:13:10 +00:00
|
|
|
// report invalid object received
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-11-25 10:13:10 +00:00
|
|
|
// report null object received
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class FeedListAdapter extends ArrayAdapter<Feed> {
|
|
|
|
private ArrayList<Feed> items;
|
|
|
|
|
2011-11-24 10:01:51 +00:00
|
|
|
public static final int VIEW_NORMAL = 0;
|
|
|
|
public static final int VIEW_SELECTED = 1;
|
|
|
|
|
|
|
|
public static final int VIEW_COUNT = VIEW_SELECTED+1;
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
public FeedListAdapter(Context context, int textViewResourceId, ArrayList<Feed> items) {
|
|
|
|
super(context, textViewResourceId, items);
|
|
|
|
this.items = items;
|
|
|
|
}
|
|
|
|
|
2011-11-24 10:01:51 +00:00
|
|
|
public int getViewTypeCount() {
|
|
|
|
return VIEW_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
Feed feed = items.get(position);
|
2011-11-23 16:38:21 +00:00
|
|
|
|
2011-11-24 10:01:51 +00:00
|
|
|
if (feed.id == m_selectedFeedId) {
|
|
|
|
return VIEW_SELECTED;
|
|
|
|
} else {
|
|
|
|
return VIEW_NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
2011-11-23 10:55:05 +00:00
|
|
|
View v = convertView;
|
|
|
|
|
|
|
|
Feed feed = items.get(position);
|
|
|
|
|
|
|
|
if (v == null) {
|
2011-11-24 10:01:51 +00:00
|
|
|
int layoutId = R.layout.feeds_row;
|
|
|
|
|
|
|
|
switch (getItemViewType(position)) {
|
|
|
|
case VIEW_SELECTED:
|
|
|
|
layoutId = R.layout.feeds_row_selected;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2011-11-24 10:01:51 +00:00
|
|
|
v = vi.inflate(layoutId, null);
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextView tt = (TextView) v.findViewById(R.id.title);
|
|
|
|
|
|
|
|
if (tt != null) {
|
|
|
|
tt.setText(feed.title);
|
|
|
|
}
|
|
|
|
|
|
|
|
TextView tu = (TextView) v.findViewById(R.id.unread_counter);
|
|
|
|
|
|
|
|
if (tu != null) {
|
|
|
|
tu.setText(String.valueOf(feed.unread));
|
|
|
|
tu.setVisibility((feed.unread > 0) ? View.VISIBLE : View.INVISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
2011-11-22 14:30:09 +00:00
|
|
|
}
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|