package com.platncare.app.activities;

import android.app.Activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.platncare.app.R;
import com.platncare.app.fragments.PlantsFeedFragment;
import com.platncare.app.utils.FragmentUtils;
import com.platncare.app.utils.IntentKeys;
import com.platncare.app.utils.Preferences;
import model.Token;

public class FeedActivity extends Activity {


    private String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed);
        getExtras();

        attachInitialFragment();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.feed_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            case R.id.menu_logout:
                startLogout();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void startLogout() {
        Preferences.clearAllPreferences(FeedActivity.this);
        finish();
    }

    private void getExtras() {
        Bundle args = getIntent().getExtras();

        if(args != null) {
            if(args.containsKey(IntentKeys.TOKEN_KEY)) {
                token = args.getString(IntentKeys.TOKEN_KEY);
            } else {
               throw new RuntimeException("To initiate we need TokenKey.");
            }
        }
    }

    private void attachInitialFragment() {
        PlantsFeedFragment plantsFeedFragment = PlantsFeedFragment.newInstance(token);
        FragmentUtils.setFragment(this, plantsFeedFragment, R.id.frameLayoutContainer);
    }
}

@@ -61,17 +61,16 @@
 tivity;%0A
-%0A
 import a
@@ -362,28 +362,8 @@
 ces;
-%0Aimport model.Token;
 %0A%0Apu
@@ -1364,24 +1364,25 @@
 %0A%0A        if
+ 
 (args != nul
@@ -1400,16 +1400,17 @@
       if
+ 
 (args.co
@@ -1546,66 +1546,45 @@
     
-throw new RuntimeException(%22To initiate we need TokenKey.%22
+ token = Preferences.getAppToken(this
 );%0A 

Load token from prefs if does not exist