<commit_before>package com.jamesg.forecastr;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;

import com.jamesg.forecastr.base.BaseActivity;
import com.jamesg.forecastr.base.BaseFragment;
import com.jamesg.forecastr.SpotFragment.SpotWrapperFragment;
import com.jamesg.forecastr.data.Spot;
import com.jamesg.forecastr.manager.AppManager;
import com.jamesg.forecastr.manager.SpotManager;
import com.jamesg.forecastr.utils.Logger;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.splunk.mint.Mint;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import java.util.Stack;

import javax.inject.Inject;

public class MainActivity extends BaseActivity implements BaseFragment.BaseFragmentInteractionListener {

    static final int SPOTS_FRAGMENT = 0;
    static final int ABOUT_FRAGMENT = 1;

    @Inject
    SpotManager spotManager;

    @Inject
    AppManager appManager;

    @Inject
    Bus bus;

    private Stack<BaseFragment> stack;
    private BaseFragment current;
    private int currentID;

    private int dateTab = 0;

    private SlidingMenu menu;

    private String openSpot = "";


    public MainActivity() {
        super(R.string.app_name);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ((WindFinderApplication) getApplication()).inject(this);
        super.onCreate(savedInstanceState);

        Mint.initAndStartSession(MainActivity.this, "91e59a0e");

        setTitle("Favourite Spots");
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setDisplayShowHomeEnabled(false);

        stack = new Stack<BaseFragment>();

        if (savedInstanceState != null) {
            current = (SpotWrapperFragment) getSupportFragmentManager().findFragmentByTag("currentFragment");
        }else{
            current = SpotWrapperFragment.newInstance();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.content_frame, current,"currentFragment")
                    .commit();
            currentID = SPOTS_FRAGMENT;
        }

        setSlidingActionBarEnabled(true);

        SlidingMenu menu = getSlidingMenu();
        menu.setOnClosedListener(new SlidingMenu.OnClosedListener() {
            @Override
            public void onClosed() {
                //drawerFragment.clearSearch();
                //getActionBar().setDisplayHomeAsUpEnabled(false);
            }
        });

        menu.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
            @Override
            public void onOpened() {
               // getActionBar().setDisplayHomeAsUpEnabled(true);
            }
        });

        //deleteDatabase("spotsTable");

        /*if(spotManager.getSpot("Aberavon") == null){
            spotManager.addSpot(new Spot("Aberavon",322661));
        }
        if(spotManager.getSpot("Portland Harbour") == null){
            spotManager.addSpot(new Spot("Portland Harbour",354618));
        }
        if(spotManager.getSpot("Westward Ho!") == null){
            spotManager.addSpot(new Spot("Westward Ho!",354583));
        }
        if(spotManager.getSpot("Westbury") == null){
            spotManager.addSpot(new Spot("Westbury",354152));
        }*/

        spotManager.checkForUpdates(false);
        appManager.checkForUpdates();
    }
    @Subscribe
    public void getMessage(String s) {
        if(s.equals("NewAppAvailable")){
            try {
                new AlertDialog.Builder(this)
                        .setTitle("New Version of the app is available.")
                        .setMessage("Version " + appManager.getVersionName() + " is available now. Would you like to update to this version now.")
                        .setPositiveButton("Update", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String url = getString(R.string.base_url)+"releases/latest/WindForecastApp.apk";
                                Intent i = new Intent(Intent.ACTION_VIEW);
                                i.setData(Uri.parse(url));
                                startActivity(i);
                            }
                        })
                        .setNegativeButton("Later", null)
                        .show();
            }catch(Exception e){
                //DO NOTHING
            }
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        Mint.startSession(MainActivity.this);
        bus.register(this);
        spotManager.checkForUpdates(false);
    }

    @Override
    public void onPause() {
        super.onPause();
        Mint.flush();
        Mint.closeSession(MainActivity.this);
        bus.unregister(this);
    }

    public void removeSpot(Spot spot){
        spotManager.deleteSpot(spot);
        //mainContentFragment.removeSpot(spot.getName());
        drawerFragment.refreshSpots();
        if(currentID == SPOTS_FRAGMENT && openSpot.equals("")){
            ((SpotWrapperFragment)current).removeSpot(spot);
        }
        if(spot.getName().equals(openSpot)){
            allSpots(false);
        }
        Mint.logEvent("Spot Removed - "+spot.getName());
    }

    public void addSpot(String name){
        Spot newSpot = spotManager.getSpot(name);
        Logger.d("NEW SPOT DATA - "+newSpot.getRawData());
        spotManager.addSpot(newSpot);
        drawerFragment.refreshSpots();
        drawerFragment.setSpot(name);
        spotManager.parseSpotData(newSpot.getName());
        spotManager.checkForUpdates(false);
        Toast.makeText(this, name + "added to favourites.",Toast.LENGTH_SHORT).show();
        Mint.logEvent("Spot Added - "+name);
    }

    public void loadSpot(String name, int listClick){
        popBackStack(false);
        openSpot = name;
        ((SpotWrapperFragment)current).loadSpot(name, true, false);<commit_msg>Fix for issue #33<commit_after>package com.jamesg.forecastr;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;

import com.jamesg.forecastr.base.BaseActivity;
import com.jamesg.forecastr.base.BaseFragment;
import com.jamesg.forecastr.SpotFragment.SpotWrapperFragment;
import com.jamesg.forecastr.data.Spot;
import com.jamesg.forecastr.manager.AppManager;
import com.jamesg.forecastr.manager.SpotManager;
import com.jamesg.forecastr.utils.Logger;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.splunk.mint.Mint;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import java.util.Stack;

import javax.inject.Inject;

public class MainActivity extends BaseActivity implements BaseFragment.BaseFragmentInteractionListener {

    static final int SPOTS_FRAGMENT = 0;
    static final int ABOUT_FRAGMENT = 1;

    @Inject
    SpotManager spotManager;

    @Inject
    AppManager appManager;

    @Inject
    Bus bus;

    private Stack<BaseFragment> stack;
    private BaseFragment current;
    private int currentID;

    private int dateTab = 0;

    private SlidingMenu menu;

    private String openSpot = "";


    public MainActivity() {
        super(R.string.app_name);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ((WindFinderApplication) getApplication()).inject(this);
        super.onCreate(savedInstanceState);

        Mint.initAndStartSession(MainActivity.this, "91e59a0e");

        setTitle("Favourite Spots");
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setDisplayShowHomeEnabled(false);

        stack = new Stack<BaseFragment>();

        if (savedInstanceState != null) {
            current = (SpotWrapperFragment) getSupportFragmentManager().findFragmentByTag("currentFragment");
        }else{
            current = SpotWrapperFragment.newInstance();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.content_frame, current,"currentFragment")
                    .commit();
            currentID = SPOTS_FRAGMENT;
        }

        setSlidingActionBarEnabled(true);

        SlidingMenu menu = getSlidingMenu();
        menu.setOnClosedListener(new SlidingMenu.OnClosedListener() {
            @Override
            public void onClosed() {
                //drawerFragment.clearSearch();
                //getActionBar().setDisplayHomeAsUpEnabled(false);
            }
        });

        menu.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
            @Override
            public void onOpened() {
               // getActionBar().setDisplayHomeAsUpEnabled(true);
            }
        });

        //deleteDatabase("spotsTable");

        /*if(spotManager.getSpot("Aberavon") == null){
            spotManager.addSpot(new Spot("Aberavon",322661));
        }
        if(spotManager.getSpot("Portland Harbour") == null){
            spotManager.addSpot(new Spot("Portland Harbour",354618));
        }
        if(spotManager.getSpot("Westward Ho!") == null){
            spotManager.addSpot(new Spot("Westward Ho!",354583));
        }
        if(spotManager.getSpot("Westbury") == null){
            spotManager.addSpot(new Spot("Westbury",354152));
        }*/

        spotManager.checkForUpdates(false);
        appManager.checkForUpdates();
    }
    @Subscribe
    public void getMessage(String s) {
        if(s.equals("NewAppAvailable")){
            try {
                new AlertDialog.Builder(this)
                        .setTitle("New Version of the app is available.")
                        .setMessage("Version " + appManager.getVersionName() + " is available now. Would you like to update to this version now.")
                        .setPositiveButton("Update", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String url = getString(R.string.base_url)+"releases/latest/WindForecastApp.apk";
                                Intent i = new Intent(Intent.ACTION_VIEW);
                                i.setData(Uri.parse(url));
                                startActivity(i);
                            }
                        })
                        .setNegativeButton("Later", null)
                        .show();
            }catch(Exception e){
                //DO NOTHING
            }
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        Mint.startSession(MainActivity.this);
        bus.register(this);
        spotManager.checkForUpdates(false);
    }

    @Override
    public void onPause() {
        super.onPause();
        Mint.flush();
        Mint.closeSession(MainActivity.this);
        bus.unregister(this);
    }

    public void removeSpot(Spot spot){
        spotManager.deleteSpot(spot);
        //mainContentFragment.removeSpot(spot.getName());
        drawerFragment.refreshSpots();
        if(currentID == SPOTS_FRAGMENT && openSpot.equals("")){
            ((SpotWrapperFragment)current).removeSpot(spot);
        }
        if(spot.getName().equals(openSpot)){
            allSpots(false);
        }
        Mint.logEvent("Spot Removed - "+spot.getName());
    }

    public void addSpot(String name){
        Spot newSpot = spotManager.getSpot(name);
        if(newSpot == null){
            Toast.makeText(this, "Error : Could not add "+ name + " to favourites.", Toast.LENGTH_SHORT).show();
        }else {
            spotManager.addSpot(newSpot);
            drawerFragment.refreshSpots();
            drawerFragment.setSpot(name);
            spotManager.parseSpotData(newSpot.getName());
            spotManager.checkForUpdates(false);
            Toast.makeText(this, name + "added to favourites.", Toast.LENGTH_SHORT).show();
            Mint.logEvent("Spot Added - " + name);
        }
    }

    public void loadSpot(String name, int listClick){
        popBackStack(false);
        openSpot = name;
        ((SpotWrapperFragment)current).loadSpot(name, true, false);
{'fraction_non_alphanumeric': 0.1082864038616251, 'fraction_numerical': 0.005470635559131134, 'mean_word_length': 2.2868852459016393, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5531613', 'n_tokens_mistral': 3555, 'n_tokens_neox': 3322, 'n_words': 587}