package fi.reaktor.android.rx;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.BaseAdapter;
import android.widget.ListView;

import fi.reaktor.android.rx.app.RssReaderApplication;
import fi.reaktor.android.rx.data.Feed;
import fi.reaktor.android.rx.data.Feeds;

public class FeedActivity extends RssReaderBaseActivity {

    BroadcastReceiver feedUpdatesReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            ListView articleList = (ListView) findViewById(R.id.article_list);
            ((BaseAdapter) articleList.getAdapter()).notifyDataSetChanged();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setTitle(getIntent().getCharSequenceExtra("feed-title"));

        String feedGuid = getIntent().getStringExtra("feed-guid");
        Feeds feeds = ((RssReaderApplication) getApplication()).getFeeds();
        Feed feed = null;
        for (Feed f : feeds.getFeeds()) {
            if (f.getGuid().equals(feedGuid)) {
                feed = f;
                break;
            }
        }
        if(feed == null) {
            finish();
        } else {
            ListView articleList = (ListView) findViewById(R.id.article_list);
            articleList.setAdapter(new ArticlesAdapter(feed, this));
            LocalBroadcastManager.getInstance(this).registerReceiver(feedUpdatesReceiver, new IntentFilter("feeds-updated"));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_feed, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_favorite) {
            // TODO
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -896,16 +896,40 @@
     %7D;%0A%0A
+    private Feed feed;%0A%0A
     @Ove
@@ -930,24 +930,24 @@
   @Override%0A
-
     protecte
@@ -1083,16 +1083,17 @@
 _feed);%0A
+%0A
         
@@ -1289,16 +1289,446 @@
 guid%22);%0A
+        findFeed(feedGuid);%0A        if(feed == null) %7B%0A            finish();%0A        %7D else %7B%0A            ListView articleList = (ListView) findViewById(R.id.article_list);%0A            articleList.setAdapter(new ArticlesAdapter(feed, this));%0A            LocalBroadcastManager.getInstance(this).registerReceiver(feedUpdatesReceiver, new IntentFilter(%22feeds-updated%22));%0A        %7D%0A    %7D%0A%0A    private void findFeed(String feedGuid) %7B%0A
         
@@ -1799,34 +1799,8 @@
 ();%0A
-        Feed feed = null;%0A
     
@@ -1962,358 +1962,8 @@
   %7D%0A
-        if(feed == null) %7B%0A            finish();%0A        %7D else %7B%0A            ListView articleList = (ListView) findViewById(R.id.article_list);%0A            articleList.setAdapter(new ArticlesAdapter(feed, this));%0A            LocalBroadcastManager.getInstance(this).registerReceiver(feedUpdatesReceiver, new IntentFilter(%22feeds-updated%22));%0A        %7D%0A
     
@@ -2171,16 +2171,191 @@
  menu);%0A
+        MenuItem favoriteItem = menu.findItem(R.id.action_favorite);%0A        if(feed.isFavorite()) %7B%0A            favoriteItem.setIcon(android.R.drawable.ic_delete);%0A        %7D%0A
         
@@ -2792,15 +2792,155 @@
     
-// TODO
+feed.setFavorite(!feed.isFavorite());%0A            item.setIcon(feed.isFavorite() ? android.R.drawable.ic_delete : android.R.drawable.ic_input_add);
 %0A   

Save favorite bit when action icon is clicked