/*
 * Copyright 2012 GitHub Inc.
 *
 * 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 com.github.mobile.ui.gist;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.EditText;

import com.actionbarsherlock.R.drawable;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.github.mobile.R.id;
import com.github.mobile.R.layout;
import com.github.mobile.R.menu;
import com.github.mobile.R.string;
import com.github.mobile.ui.TextWatcherAdapter;
import com.github.mobile.util.ShareUtils;
import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity;

import org.eclipse.egit.github.core.Gist;

import roboguice.inject.InjectView;

/**
 * Activity to share a text selection as a public or private Gist
 */
public class CreateGistActivity extends RoboSherlockFragmentActivity {

    @InjectView(id.et_gist_description)
    private EditText descriptionText;

    @InjectView(id.et_gist_name)
    private EditText nameText;

    @InjectView(id.et_gist_content)
    private EditText contentText;

    @InjectView(id.cb_public)
    private CheckBox publicCheckBox;

    private MenuItem createItem;

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

        setContentView(layout.gist_create);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(string.new_gist);
        actionBar.setIcon(drawable.action_gist);

        String text = ShareUtils.getBody(getIntent());
        if (!TextUtils.isEmpty(text))
            contentText.setText(text);

        String subject = ShareUtils.getSubject(getIntent());
        if (!TextUtils.isEmpty(subject))
            descriptionText.setText(subject);

        contentText.addTextChangedListener(new TextWatcherAdapter() {

            @Override
            public void afterTextChanged(Editable s) {
                updateCreateMenu(s);
            }
        });
        updateCreateMenu();
    }

    private void updateCreateMenu() {
        if (contentText != null)
            updateCreateMenu(contentText.getText());
    }

    private void updateCreateMenu(CharSequence text) {
        if (createItem != null)
            createItem.setEnabled(!TextUtils.isEmpty(text));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu options) {
        getSupportMenuInflater().inflate(menu.gist_create, options);
        createItem = options.findItem(id.m_apply);
        updateCreateMenu();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case id.m_apply:
            createGist();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    private void createGist() {
        final boolean isPublic = publicCheckBox.isChecked();

        String enteredDescription = descriptionText.getText().toString().trim();
        final String description = enteredDescription.length() > 0 ? enteredDescription
                : getString(string.gist_description_hint);

        String enteredName = nameText.getText().toString().trim();
        final String name = enteredName.length() > 0 ? enteredName
                : getString(string.gist_file_name_hint);

        final String content = contentText.getText().toString();

        new CreateGistTask(this, description, isPublic, name, content) {

            @Override
            protected void onSuccess(Gist gist) throws Exception {
                super.onSuccess(gist);

                startActivity(GistsViewActivity.createIntent(gist));
                setResult(RESULT_OK);
                finish();
            }
        }.create();
    }
}

@@ -1076,16 +1076,68 @@
 string;%0A
+import com.github.mobile.ui.DialogFragmentActivity;%0A
 import c
@@ -1221,99 +1221,8 @@
 ils;
-%0Aimport com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity;
 %0A%0Aim
@@ -1266,45 +1266,8 @@
 t;%0A%0A
-import roboguice.inject.InjectView;%0A%0A
 /**%0A
@@ -1380,20 +1380,14 @@
 nds 
-RoboSherlock
+Dialog
 Frag
@@ -1410,183 +1410,74 @@
     
-@InjectView(id.et_gist_description)%0A    private EditText descriptionText;%0A%0A    @InjectView(id.et_gist_name)%0A    private EditText nameText;%0A%0A    @InjectView(id.et_gist_content)
+private EditText descriptionText;%0A%0A    private EditText nameText;%0A
 %0A   
@@ -1512,38 +1512,8 @@
 t;%0A%0A
-    @InjectView(id.cb_public)%0A
     
@@ -1737,24 +1737,244 @@
 t_create);%0A%0A
+        descriptionText = finder.find(id.et_gist_description);%0A        nameText = finder.find(id.et_gist_name);%0A        contentText = finder.find(id.et_gist_content);%0A        publicCheckBox = finder.find(id.cb_public);%0A%0A
         Acti

Use view finder instead of InjectView annotations