<commit_before>/*
 *   Copyright 2015 <NAME>,
 *                  <NAME>,
 *                  <NAME>,
 *                  <NAME>,
 *                  <NAME>,
 *                  <NAME>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package cmput301w15t07.TravelTracker.util;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import cmput301w15t07.TravelTracker.R;
import cmput301w15t07.TravelTracker.model.Claim;
import cmput301w15t07.TravelTracker.model.Destination;
import cmput301w15t07.TravelTracker.model.Geolocation;

/**
 * Custom adapter for dealing with displaying and editing destinations dynamically
 * in a LinearLayout.
 * 
 * @author therabidsquirel
 * 
 */
public class DestinationAdapter {
    private Context context;
    private Claim claim;
    private ArrayList<Destination> destinations;
    private FragmentManager manager;
    private LinearLayout linearLayout;
    
    /** Whether list elements should be editable. */
    private boolean editable;
    
    private final static int LIST_VIEW_ID = R.layout.claim_info_destinations_list_item;
    private final static int LIST_LOCATION_ID = R.id.claimInfoDestinationsListItemLocationTextView;
    private final static int LIST_REASON_ID = R.id.claimInfoDestinationsListItemReasonTextView;
    
    private final static int COLOR_SELECTED = android.R.color.holo_blue_light;
    private final static int COLOR_PLAIN = android.R.color.transparent;
    
    public DestinationAdapter (Claim claim, ArrayList<Destination> destinations, FragmentManager manager) {
        this.claim = claim;
        this.destinations = destinations;
        this.manager = manager;
    }
    
    /**
     * Set the destinations list on the adapter
     * @param destinations The new ArrayList.
     */
    public void setDestinations(ArrayList<Destination> destinations) {
        this.destinations = destinations;
    }
    
    /**
     * Given a LinearLayout, recreate all of the views within it from the list of
     * destinations in this adapter.
     * @param context The context of the activity containing the LinearLayout.
     * @param linearLayout The LinearLayout the adapter will add views to.
     */
    public void createList(Context context, LinearLayout linearLayout) {
        this.context = context;
        this.linearLayout = linearLayout;
        
        linearLayout.removeAllViews();
        
        for (Destination destination : destinations) {
            View view = createView(destination);
            linearLayout.addView(view);
        }
    }
    
    /**
     * Given a destination, create a view populated with its information for display in
     * a custom LinearLayout. Also add click and long-click listeners to the view for
     * editing and deleting respectively.
     * @param destination The destination to create a view for.
     * @return The newly created view.
     */
    private View createView(Destination destination) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(LIST_VIEW_ID, linearLayout, false);
        setDestinationOnView(view, destination);
        
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setBackgroundColor(context.getResources().getColor(COLOR_SELECTED));
                promptEditDestination(v, getDestinationFromView(v));
            }
        });
        
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                v.setBackgroundColor(context.getResources().getColor(COLOR_SELECTED));
                promptDeleteDestination(v);
                return false;
            }
        });
        
        return view;
    }
    
    /**
     * Starts an editing dialog with a new destination.
     * @param context The Context the dialog will run in.
     * @param linearLayout The LinearLayout the new destination's view will be added to.
     */
    public void addDestination(Context context, LinearLayout linearLayout) {
        this.context = context;
        this.linearLayout = linearLayout;
        
        Destination destination = new Destination("", null, "");
        promptEditDestination(null, destination);
    }
    
    /**
     * Open the fragment for editing a destination.
     * @param view The view the destination belongs to. Pass null if a new destination.
     * @param destination The destination to be edited.
     */
    private void promptEditDestination(View view, Destination destination) {
        DestinationEditorFragment editor = new DestinationEditorFragment(new DestinationCallback(), view, destination, editable);
        editor.show(manager, "destinationEditor");
    }
    
    /**
     * Given a view an edited destination belongs to, or null if the destination is new and
     * does not belong to a view yet, create a new destination from the new attributes.
     * Update the existing view or create and add a new one, and update the list of destinations
     * in the claim.
     * @param view The view of the destination to be edited or added. Will be null if a new destination.
     * @param location The new location of the destination.
     * @param geolocation The new geolocation of the destination.
     * @param reason The new reason of the destination.
     */
    private void editDestination(View view, String location, Geolocation geolocation, String reason) {
        Destination destination = new Destination(location, geolocation, reason);
        
        // A null view means a new destination.
        if (view == null) {
            view = createView(destination);
            linearLayout.addView(view);
            destinations.add(destination);<commit_msg>Fixed new destination's dialog not being editable.<commit_after>/*
 *   Copyright 2015 <NAME>,
 *                  <NAME>,
 *                  <NAME>,
 *                  <NAME>,
 *                  <NAME>,
 *                  <NAME>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package cmput301w15t07.TravelTracker.util;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import cmput301w15t07.TravelTracker.R;
import cmput301w15t07.TravelTracker.model.Claim;
import cmput301w15t07.TravelTracker.model.Destination;
import cmput301w15t07.TravelTracker.model.Geolocation;

/**
 * Custom adapter for dealing with displaying and editing destinations dynamically
 * in a LinearLayout.
 * 
 * @author therabidsquirel
 * 
 */
public class DestinationAdapter {
    private Context context;
    private Claim claim;
    private ArrayList<Destination> destinations;
    private FragmentManager manager;
    private LinearLayout linearLayout;
    
    /** Whether list elements should be editable. */
    private boolean editable;
    
    private final static int LIST_VIEW_ID = R.layout.claim_info_destinations_list_item;
    private final static int LIST_LOCATION_ID = R.id.claimInfoDestinationsListItemLocationTextView;
    private final static int LIST_REASON_ID = R.id.claimInfoDestinationsListItemReasonTextView;
    
    private final static int COLOR_SELECTED = android.R.color.holo_blue_light;
    private final static int COLOR_PLAIN = android.R.color.transparent;
    
    public DestinationAdapter (Claim claim, ArrayList<Destination> destinations, FragmentManager manager) {
        this.claim = claim;
        this.destinations = destinations;
        this.manager = manager;
    }
    
    /**
     * Set the destinations list on the adapter
     * @param destinations The new ArrayList.
     */
    public void setDestinations(ArrayList<Destination> destinations) {
        this.destinations = destinations;
    }
    
    /**
     * Given a LinearLayout, recreate all of the views within it from the list of
     * destinations in this adapter.
     * @param context The context of the activity containing the LinearLayout.
     * @param linearLayout The LinearLayout the adapter will add views to.
     */
    public void createList(Context context, LinearLayout linearLayout) {
        this.context = context;
        this.linearLayout = linearLayout;
        
        linearLayout.removeAllViews();
        
        for (Destination destination : destinations) {
            View view = createView(destination);
            linearLayout.addView(view);
        }
    }
    
    /**
     * Given a destination, create a view populated with its information for display in
     * a custom LinearLayout. Also add click and long-click listeners to the view for
     * editing and deleting respectively.
     * @param destination The destination to create a view for.
     * @return The newly created view.
     */
    private View createView(Destination destination) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(LIST_VIEW_ID, linearLayout, false);
        setDestinationOnView(view, destination);
        
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setBackgroundColor(context.getResources().getColor(COLOR_SELECTED));
                promptEditDestination(v, getDestinationFromView(v));
            }
        });
        
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                v.setBackgroundColor(context.getResources().getColor(COLOR_SELECTED));
                promptDeleteDestination(v);
                return false;
            }
        });
        
        return view;
    }
    
    /**
     * Starts an editing dialog with a new destination.
     * @param context The Context the dialog will run in.
     * @param linearLayout The LinearLayout the new destination's view will be added to.
     */
    public void addDestination(Context context, LinearLayout linearLayout) {
        this.context = context;
        this.linearLayout = linearLayout;
        
        Destination destination = new Destination("", null, "");
        promptEditDestination(null, destination);
    }
    
    /**
     * Open the fragment for editing a destination.
     * @param view The view the destination belongs to. Pass null if a new destination.
     * @param destination The destination to be edited.
     */
    private void promptEditDestination(View view, Destination destination) {
        // We know if a new destination is being added the claim is editable.
        editable = (view == null) ? true : editable;
        
        DestinationEditorFragment editor = new DestinationEditorFragment(new DestinationCallback(), view, destination, editable);
        editor.show(manager, "destinationEditor");
    }
    
    /**
     * Given a view an edited destination belongs to, or null if the destination is new and
     * does not belong to a view yet, create a new destination from the new attributes.
     * Update the existing view or create and add a new one, and update the list of destinations
     * in the claim.
     * @param view The view of the destination to be edited or added. Will be null if a new destination.
     * @param location The new location of the destination.
     * @param geolocation The new geolocation of the destination.
     * @param reason The new reason of the destination.
     */
    private void editDestination(View view, String location, Geolocation geolocation, String reason) {
        Destination destination = new Destination(location, geolocation, reason);
        
        // A null view means a new destination.
        if (view == null) {
            view = createView(destination);
            linearLayout.addView(view);
            destinations.add(destination);
{'fraction_non_alphanumeric': 0.07161371547063615, 'fraction_numerical': 0.0063689550470265865, 'mean_word_length': 2.7626079687935357, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3018348', 'n_tokens_mistral': 3444, 'n_tokens_neox': 3318, 'n_words': 1177}