package com.ae.apps.tripmeter.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.ae.apps.common.vo.ContactVo;
import com.ae.apps.tripmeter.R;
import com.ae.apps.tripmeter.listeners.ExpensesInteractionListener;
import com.ae.apps.tripmeter.managers.ExpenseManager;
import com.ae.apps.tripmeter.models.Trip;
import com.ae.apps.tripmeter.utils.AppConstants;
import com.ae.apps.tripmeter.views.adapters.TripRecyclerViewAdapter;

import java.util.List;

/**
 * A fragment representing a list of Trips. A trip can also be added.
 * <p>
 * Activities containing this fragment MUST implement the {@link ExpensesInteractionListener}
 * interface.
 */
public class TripsListFragment extends Fragment
        implements AddTripDialogFragment.AddTripDialogListener,
        PickProfileDialogFragment.SelectProfileListener {

    private ExpensesInteractionListener mListener;

    private ExpenseManager mExpenseManager;

    private TripRecyclerViewAdapter mViewAdapter;

    private List<Trip> mTrips;

    public TripsListFragment() {
    }

    public static TripsListFragment newInstance() {
        return new TripsListFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mExpenseManager = ExpenseManager.newInstance(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_trips_list, container, false);
        View list = view.findViewById(R.id.list);

        checkForDefaultProfile();

        mTrips = mExpenseManager.getAllTrips();
        mViewAdapter = new TripRecyclerViewAdapter(mTrips, mListener);

        if (list instanceof RecyclerView) {
            Context context = view.getContext();
            RecyclerView recyclerView = (RecyclerView) list;
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setAdapter(mViewAdapter);
        }

        // Locate the FAB and add a trip when its clicked
        FloatingActionButton actionButton = (FloatingActionButton) view.findViewById(R.id.fab);
        actionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddTripDialog();
            }
        });

        return view;
    }

    private void checkForDefaultProfile() {
        ContactVo contactVo = mExpenseManager.getDefaultProfile();
        if (null == contactVo) {
            showSelectProfileDialog();
        }
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof ExpensesInteractionListener) {
            mListener = (ExpensesInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnListFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    private void showAddTripDialog() {
        FragmentManager fragmentManager = getFragmentManager();
        AddTripDialogFragment dialogFragment = AddTripDialogFragment.newInstance();
        dialogFragment.setTargetFragment(TripsListFragment.this, 300);
        dialogFragment.show(fragmentManager, "fragment_add_trip");
    }

    private void showSelectProfileDialog() {
        FragmentManager fragmentManager = getFragmentManager();
        PickProfileDialogFragment dialogFragment = PickProfileDialogFragment.newInstance();
        dialogFragment.setTargetFragment(TripsListFragment.this, 300);
        dialogFragment.setCancelable(false);
        dialogFragment.show(fragmentManager, "fragment_select_profile");
    }

    @Override
    public void onTripAdded(Trip trip) {
        // add trip to database, update with the tripId
        mExpenseManager.addTrip(trip);

        Toast.makeText(getActivity(), "added new row with id " + trip.getId(), Toast.LENGTH_SHORT).show();

        // add trip to list view
        mTrips.add(trip);
        if (null != mViewAdapter) {
            mViewAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onProfileSelected(String contactId) {
        // Save this id in shared preferences
        mExpenseManager.saveDefaultProfile(contactId);
    }
}
@@ -1,8 +1,1104 @@
+/**%0A * MIT License%0A Copyright (c) 2016 Midhun Harikumar%0A Permission is hereby granted, free of charge, to any person obtaining a copy%0A of this software and associated documentation files (the %22Software%22), to deal%0A in the Software without restriction, including without limitation the rights%0A to use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0A copies of the Software, and to permit persons to whom the Software is%0A furnished to do so, subject to the following conditions:%0A The above copyright notice and this permission notice shall be included in all%0A copies or substantial portions of the Software.%0A THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE%0A SOFTWARE.%0A */%0A
 package 
@@ -2106,35 +2106,33 @@
 rips
-. A trip can also be added.
+ with option to add trips
 %0A * 
@@ -2243,16 +2243,24 @@
 erface.%0A
+ * %3C/p%3E%0A
  */%0Apubl
@@ -3213,24 +3213,166 @@
 .id.list);%0A%0A
+        // Expenses needs default profile to be set inorder to function%0A        // Check if one has been selected or ask for 1 to be selected%0A
         chec
@@ -4226,24 +4226,78 @@
 Profile() %7B%0A
+        // Ask ExpenseManager for the default profile%0A
         Cont
@@ -4440,17 +4440,16 @@
 %0A    %7D%0A%0A
-%0A
     @Ove
@@ -6250,9 +6250,10 @@
 ;%0A    %7D%0A
-
 %7D
+%0A

Update TripsListFragment.java