/*
 *
 *  * Copyright (C) 2014 Eduardo Barrenechea
 *  *
 *  * 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.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import ca.barrenechea.ticker.R;
import ca.barrenechea.ticker.data.EventLoader;

public class CreateEventDialog extends BaseDialog {
    @InjectView(R.id.edit_name)
    EditText mEditName;
    @InjectView(R.id.button_positive)
    Button mButtonPositive;

    @Inject
    EventLoader mEventLoader;

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

        ButterKnife.inject(this, view);

        mEditName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                // do nothing
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                // do nothing
            }

            @Override
            public void afterTextChanged(Editable editable) {
                mButtonPositive.setEnabled(editable.length() > 0);
            }
        });

        mButtonPositive.setEnabled(false);
        mButtonPositive.setOnClickListener(v -> {
            final String name = mEditName.getText().toString();

            mEventLoader.create(name, null);

            CreateEventDialog.this.dismiss();
        });

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        ButterKnife.reset(this);
    }
}

@@ -722,21 +722,79 @@
 oid.
-text.Editable
+os.Handler;%0Aimport android.text.Editable;%0Aimport android.text.TextUtils
 ;%0Aim
@@ -917,16 +917,60 @@
 wGroup;%0A
+import android.view.inputmethod.EditorInfo;%0A
 import a
@@ -1022,16 +1022,45 @@
 ditText;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -1223,16 +1223,62 @@
 tLoader;
+%0Aimport ca.barrenechea.ticker.utils.ViewUtils;
 %0A%0Apublic
@@ -1323,16 +1323,69 @@
 ialog %7B%0A
+    private static final int ANIMATION_DELAY = 325;%0A%0A
     @Inj
@@ -2358,86 +2358,550 @@
    m
-ButtonPositive.setEnabled(false);%0A        mButtonPositive.setOnClickListener(v
+EditName.setOnEditorActionListener((textView, action, keyEvent) -%3E %7B%0A            if (action == EditorInfo.IME_ACTION_DONE) %7B%0A                create();%0A                return true;%0A            %7D%0A            return false;%0A        %7D);%0A%0A        mButtonPositive.setEnabled(false);%0A        mButtonPositive.setOnClickListener(v -%3E create());%0A%0A        return view;%0A    %7D%0A%0A    private void create() %7B%0A        ViewUtils.hideSoftInput(mEditName);%0A%0A        // wait for keyboard to dismiss%0A        final Handler h = new Handler();%0A        h.postDelayed(()
  -%3E 
@@ -2963,24 +2963,72 @@
 oString();%0A%0A
+            if (!TextUtils.isEmpty(name)) %7B%0A    
             
@@ -3060,17 +3060,16 @@
  null);%0A
-%0A
         
@@ -3076,38 +3076,223 @@
     
-CreateEventDialog.this.dismiss
+    Toast.makeText(this.getActivity(), R.string.event_created, Toast.LENGTH_SHORT).show();%0A            %7D else %7B%0A                Toast.makeText(this.getActivity(), R.string.event_not_created, Toast.LENGTH_SHORT).show
 ();%0A
@@ -3291,35 +3291,37 @@
 show();%0A        
+ 
-%7D);
+   %7D
 %0A%0A        return
@@ -3310,35 +3310,66 @@
  %7D%0A%0A        
-return view
+    dismiss();%0A        %7D, ANIMATION_DELAY)
 ;%0A    %7D%0A%0A   

Improve create event dialog ux.