<commit_before>/*
 *
 *  * Copyright (C) 2014 <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 ca.barrenechea.ticker.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import ca.barrenechea.ticker.R;
import ca.barrenechea.ticker.data.Event;
import ca.barrenechea.ticker.data.EventLoader;
import ca.barrenechea.ticker.event.OnEventDelete;
import ca.barrenechea.ticker.event.OnEventEdit;
import ca.barrenechea.ticker.widget.HistoryAdapter;
import io.realm.RealmQuery;
import io.realm.RealmResults;
import rx.Observer;
import rx.Subscription;

public class EventFragment extends BaseFragment implements Observer<RealmResults<Event>> {

    private static final String TAG = "EventFragment";

    private static final long DURATION = 175;
    private static final String KEY_ID = "Event.Id";

    private static final int FLAGS = DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_TIME
            | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;

    private static enum Status {CANCELLED, SUCCESS, FAILURE}

    @InjectView(R.id.text_name)
    TextView mTextName;
    @InjectView(R.id.text_note)
    TextView mTextNote;
    @InjectView(R.id.text_time)
    TextView mTextTime;
    @InjectView(R.id.edit_name)
    EditText mEditName;
    @InjectView(R.id.edit_note)
    EditText mEditNote;
    @InjectView(R.id.list)
    ListView mListView;
    @InjectView(R.id.empty)
    View mEmptyView;

    @Inject
    EventLoader mEventLoader;

    private String mId;
    private boolean mIsDirty = false;

    private HistoryAdapter mAdapter;
    private Event mEvent;
    private Subscription mSubscription;
    private ActionMode mActionMode;
    private ActionMode.Callback mCallback = new ActionMode.Callback() {

        private Status mEditState;

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            if (mActionMode == null) {
                mActionMode = actionMode;
                mEditState = Status.CANCELLED;
                return true;
            }

            return false;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            MenuInflater inflater = actionMode.getMenuInflater();
            inflater.inflate(R.menu.actionmode_event, menu);
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            if (menuItem.getItemId() == R.id.action_save) {
                if (saveEdit()) {
                    mEditState = Status.SUCCESS;
                } else {
                    mEditState = Status.FAILURE;
                }

                actionMode.finish();
                transitionViews(false);
                return true;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {
            mActionMode = null;

            String message = null;
            switch (mEditState) {
                case FAILURE:
                    message = "Invalid changes discarded.";
                    resetForm();
                    break;
                case CANCELLED:
                    message = "Edit cancelled.";
                    resetForm();
                    break;
                case SUCCESS:
                    message = "Changes saved.";
                    break;
            }

            Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
            transitionViews(false);
        }
    };

    public static EventFragment newInstance(String id) {
        Bundle args = new Bundle();
        args.putString(KEY_ID, id);

        EventFragment f = new EventFragment();
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        if (args == null) {
            throw new IllegalArgumentException("Event id cannot be null!");
        }

        mId = args.getString(KEY_ID);

        this.setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_event, container, false);<commit_msg>Use new string messages for editing events.<commit_after>/*
 *
 *  * Copyright (C) 2014 <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 ca.barrenechea.ticker.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import ca.barrenechea.ticker.R;
import ca.barrenechea.ticker.data.Event;
import ca.barrenechea.ticker.data.EventLoader;
import ca.barrenechea.ticker.event.OnEventDelete;
import ca.barrenechea.ticker.event.OnEventEdit;
import ca.barrenechea.ticker.utils.ViewUtils;
import ca.barrenechea.ticker.widget.HistoryAdapter;
import io.realm.RealmQuery;
import io.realm.RealmResults;
import rx.Observer;
import rx.Subscription;

public class EventFragment extends BaseFragment implements Observer<RealmResults<Event>> {

    private static final String TAG = "EventFragment";

    private static final long DURATION = 175;
    private static final String KEY_ID = "Event.Id";

    private static final int FLAGS = DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_TIME
            | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;

    private static enum Status {CANCELLED, SUCCESS, FAILURE}

    @InjectView(R.id.text_name)
    TextView mTextName;
    @InjectView(R.id.text_note)
    TextView mTextNote;
    @InjectView(R.id.text_time)
    TextView mTextTime;
    @InjectView(R.id.edit_name)
    EditText mEditName;
    @InjectView(R.id.edit_note)
    EditText mEditNote;
    @InjectView(R.id.list)
    ListView mListView;
    @InjectView(R.id.empty)
    View mEmptyView;

    @Inject
    EventLoader mEventLoader;

    private String mId;
    private boolean mIsDirty = false;

    private HistoryAdapter mAdapter;
    private Event mEvent;
    private Subscription mSubscription;
    private ActionMode mActionMode;
    private ActionMode.Callback mCallback = new ActionMode.Callback() {

        private Status mEditState;

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            if (mActionMode == null) {
                mActionMode = actionMode;
                mEditState = Status.CANCELLED;
                return true;
            }

            return false;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            MenuInflater inflater = actionMode.getMenuInflater();
            inflater.inflate(R.menu.actionmode_event, menu);
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            if (menuItem.getItemId() == R.id.action_save) {
                if (saveEdit()) {
                    mEditState = Status.SUCCESS;
                } else {
                    mEditState = Status.FAILURE;
                }

                actionMode.finish();
                transitionViews(false);
                return true;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {
            mActionMode = null;

            int message = 0;
            switch (mEditState) {
                case FAILURE:
                    message = R.string.event_not_saved;
                    resetForm();
                    break;
                case CANCELLED:
                    message = R.string.changes_discarded;
                    resetForm();
                    break;
                case SUCCESS:
                    message = R.string.event_saved;
                    break;
            }

            Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
            transitionViews(false);

            ViewUtils.hideSoftInput(mEditName);
        }
    };

    public static EventFragment newInstance(String id) {
        Bundle args = new Bundle();
        args.putString(KEY_ID, id);

        EventFragment f = new EventFragment();
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        if (args == null) {
            throw new IllegalArgumentException("Event id cannot be null!");
        }

        mId = args.getString(KEY_ID);

        this.setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_event, container, false);
{'fraction_non_alphanumeric': 0.08294889992902768, 'fraction_numerical': 0.0020404542228530873, 'mean_word_length': 2.4836217552533992, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1889605', 'n_tokens_mistral': 3204, 'n_tokens_neox': 3032, 'n_words': 780}