diff --git a/res/layout/feeds_fragment.xml b/res/layout/feeds_fragment.xml new file mode 100644 index 00000000..049b82ac --- /dev/null +++ b/res/layout/feeds_fragment.xml @@ -0,0 +1,13 @@ + + + + + + + + diff --git a/res/layout/feeds_row.xml b/res/layout/feeds_row.xml new file mode 100644 index 00000000..81c58b46 --- /dev/null +++ b/res/layout/feeds_row.xml @@ -0,0 +1,27 @@ + + + + + + \ No newline at end of file diff --git a/res/layout/main.xml b/res/layout/main.xml index 4361cfe8..c91f2a76 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -1,7 +1,21 @@ - - - + + + + + + + + + + + + + + + + + diff --git a/res/values/attrs.xml b/res/values/attrs.xml index 9ad509d1..fa4748c5 100644 --- a/res/values/attrs.xml +++ b/res/values/attrs.xml @@ -1,4 +1,6 @@ - + + + \ No newline at end of file diff --git a/res/values/style.xml b/res/values/style.xml index 952f70ff..b9c13774 100644 --- a/res/values/style.xml +++ b/res/values/style.xml @@ -1,8 +1,14 @@ \ No newline at end of file diff --git a/src/org/fox/ttrss/ApiRequest.java b/src/org/fox/ttrss/ApiRequest.java index aaac25c2..3495c737 100644 --- a/src/org/fox/ttrss/ApiRequest.java +++ b/src/org/fox/ttrss/ApiRequest.java @@ -1,30 +1,22 @@ package org.fox.ttrss; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; import java.util.HashMap; -import java.util.HashSet; -import java.util.List; import org.apache.http.HttpResponse; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; +import android.os.AsyncTask; +import android.util.Log; + import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonParser; -import android.content.SharedPreferences; -import android.os.AsyncTask; -import android.preference.PreferenceManager; -import android.util.Log; - public class ApiRequest extends AsyncTask, Integer, JsonElement> { private final String TAG = this.getClass().getSimpleName(); diff --git a/src/org/fox/ttrss/FeedsFragment.java b/src/org/fox/ttrss/FeedsFragment.java new file mode 100644 index 00000000..d19739d0 --- /dev/null +++ b/src/org/fox/ttrss/FeedsFragment.java @@ -0,0 +1,196 @@ +package org.fox.ttrss; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +import android.app.Activity; +import android.app.Fragment; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.TextView; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.reflect.TypeToken; + +public class FeedsFragment extends Fragment { + private final String TAG = this.getClass().getSimpleName(); + + protected ArrayList m_feeds = new ArrayList(); + protected FeedsListAdapter m_adapter; + protected SharedPreferences m_prefs; + protected String m_sessionId; + 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"); + } + + View view = inflater.inflate(R.layout.feeds_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); + } + + return view; + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + + m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); + + refresh(); + } + + public void initialize(String sessionId) { + m_sessionId = sessionId; + } + + @SuppressWarnings("unchecked") + private void refresh() { + ApiRequest task = new ApiRequest(null, m_prefs.getString("ttrss_url", null)) { + @Override + protected void onPostExecute(JsonElement result) { + if (result != null) { + try { + JsonObject rv = result.getAsJsonObject(); + + int status = rv.get("status").getAsInt(); + + if (status == 0) { + Type listType = new TypeToken>() {}.getType(); + List feeds = m_gson.fromJson(rv.get("content"), listType); + + Collections.sort(feeds); + + if (feeds != null) { + m_feeds.clear(); + + for (Feed feed : feeds) { + if (feed.id == -4 || feed.id > 0) + m_feeds.add(feed); + } + + m_adapter.notifyDataSetChanged(); + + View v = getView().findViewById(R.id.loading_progress); + + if (v != null) v.setVisibility(View.GONE); + + return; + } + } else { + JsonObject content = rv.get("content").getAsJsonObject(); + + if (content != null) { + String error = content.get("error").getAsString(); + + if (error.equals("NOT_LOGGED_IN")) { + MainActivity ma = (MainActivity)getActivity(); + + if (ma != null) ma.logout(); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + } + }; + + task.execute(new HashMap() { + { + put("sid", m_sessionId); + put("op", "getFeeds"); + put("cat_id", "-4"); + put("unread_only", "true"); + } + }); + + } + + @Override + public void onSaveInstanceState (Bundle out) { + super.onSaveInstanceState(out); + + out.putString("sessionId", m_sessionId); + } + + private class FeedsListAdapter extends ArrayAdapter { + private ArrayList items; + + public FeedsListAdapter(Context context, int textViewResourceId, ArrayList items) { + super(context, textViewResourceId, items); + this.items = items; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + + View v = convertView; + + Feed item = items.get(position); + + if (v == null) { + LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + v = vi.inflate(R.layout.feeds_row, null); + } + + TextView title = (TextView) v.findViewById(R.id.title); + + if (title != null) { + title.setText(item.title); + } + + TextView unread = (TextView) v.findViewById(R.id.unread_counter); + + if (unread != null) { + unread.setText(String.valueOf(item.unread)); + } + + return v; + } + } + + private class Feed implements Comparable { + String feed_url; + String title; + int id; + int unread; + boolean has_icon; + int cat_id; + int last_updated; + + @Override + public int compareTo(Feed feed) { + return feed.unread - this.unread; + } + } +} diff --git a/src/org/fox/ttrss/LoginActivity.java b/src/org/fox/ttrss/LoginActivity.java index 6cf1ff1e..de5db4bd 100644 --- a/src/org/fox/ttrss/LoginActivity.java +++ b/src/org/fox/ttrss/LoginActivity.java @@ -1,46 +1,25 @@ package org.fox.ttrss; -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; - -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.message.BasicNameValuePair; -import org.json.JSONObject; -import org.json.JSONTokener; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; import android.app.Activity; -import android.content.ComponentName; import android.content.Intent; -import android.content.ServiceConnection; import android.content.SharedPreferences; -import android.os.AsyncTask; import android.os.Bundle; -import android.os.IBinder; import android.preference.PreferenceManager; -import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.TextView; -import android.widget.Toast; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; public class LoginActivity extends Activity { private final String TAG = this.getClass().getSimpleName(); private SharedPreferences m_prefs; private String m_themeName = ""; - private String m_sessionId = null; @Override public void onCreate(Bundle savedInstanceState) { @@ -57,6 +36,8 @@ public class LoginActivity extends Activity { m_themeName = m_prefs.getString("theme", "THEME_DARK"); setContentView(R.layout.login); + + performLogin(); } protected void updateLoginStatus(int id) { diff --git a/src/org/fox/ttrss/MainActivity.java b/src/org/fox/ttrss/MainActivity.java index a25e4e8f..c5318f82 100644 --- a/src/org/fox/ttrss/MainActivity.java +++ b/src/org/fox/ttrss/MainActivity.java @@ -1,6 +1,7 @@ package org.fox.ttrss; import android.app.Activity; +import android.app.FragmentTransaction; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; @@ -37,7 +38,17 @@ public class MainActivity extends Activity { m_sessionId = savedInstanceState.getString("sessionId"); } - setContentView(R.layout.main); + setContentView(R.layout.main); + + FragmentTransaction ft = getFragmentManager().beginTransaction(); + FeedsFragment frag = new FeedsFragment(); + + 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(); + } @Override @@ -73,14 +84,18 @@ public class MainActivity extends Activity { startActivityForResult(intent, 0); return true; case R.id.logout: - intent = new Intent(this, LoginActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); - startActivityForResult(intent, 0); - finish(); + logout(); return true; default: return super.onOptionsItemSelected(item); } } + protected void logout() { + Intent intent = new Intent(this, LoginActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); + startActivityForResult(intent, 0); + finish(); + } + } \ No newline at end of file