package com.moko256.twitterviewer256;

import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;

import com.twitter.Validator;

import twitter4j.TwitterException;

/**
 * Created by moko256 on 2015/11/08.
 *
 * @author moko256
 */
public class SendTweetActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_tweet);

        ActionBar actionBar=getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_clear_white_24dp);

        TextView counterTextView=(TextView)findViewById(R.id.tweet_text_edit_counter);
        AppCompatEditText editText=(AppCompatEditText)findViewById(R.id.tweet_text_edit);
        onEditTextChanged(editText.getText(),counterTextView);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                onEditTextChanged(s,counterTextView);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        AppCompatButton button=(AppCompatButton)findViewById(R.id.tweet_text_submit);
        button.setOnClickListener(v -> {
            button.setEnabled(false);
            new AsyncTask<String,Void,Void>(){
            @Override
            public Void doInBackground(String... str){
                try {
                    Static.twitter.updateStatus(str[0]);
                    finish();
                } catch (TwitterException e) {
                    e.printStackTrace();
                }
                return null;
            }
            }.execute(editText.getText().toString());
        });

    }

    @Override
    public boolean onSupportNavigateUp(){
        finish();
        return false;
    }

    private void onEditTextChanged(CharSequence s,TextView counterTextView){
        int textLength=(new Validator()).getTweetLength(s.toString());
        int maxLength=Validator.MAX_TWEET_LENGTH;
        counterTextView.setText(String.valueOf(textLength)+"/"+String.valueOf(maxLength));
        if (textLength>=maxLength){
            counterTextView.setTextColor(Color.RED);
        }
        else{
            counterTextView.setTextColor(Color.GRAY);
        }
    }

}

@@ -32,16 +32,89 @@
 er256;%0A%0A
+import android.content.ClipData;%0Aimport android.content.ClipDescription;%0A
 import a
@@ -136,16 +136,16 @@
 .Color;%0A
-
 import a
@@ -165,16 +165,41 @@
 ncTask;%0A
+import android.os.Build;%0A
 import a
@@ -471,16 +471,73 @@
 atcher;%0A
+import android.view.DragEvent;%0Aimport android.view.View;%0A
 import a
@@ -1184,16 +1184,16 @@
 unter);%0A
-
         
@@ -1270,24 +1270,1440 @@
 text_edit);%0A
+        editText.setOnLongClickListener(view -%3E %7B%0A            if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.N) %7B%0A                editText.startDragAndDrop(%0A                        new ClipData(%0A                                %22text%22,%0A                                new String%5B%5D%7BClipDescription.MIMETYPE_TEXT_PLAIN%7D,%0A                                new ClipData.Item(editText.getText().subSequence(editText.getSelectionStart(), editText.getSelectionEnd()))%0A                        ),%0A                        new View.DragShadowBuilder(editText),%0A                        null,%0A                        View.DRAG_FLAG_GLOBAL);%0A            %7D%0A            return true;%0A        %7D);%0A        editText.setOnDragListener((view, dragEvent) -%3E %7B%0A            switch(dragEvent.getAction())%7B%0A                case DragEvent.ACTION_DROP:%0A                    if(dragEvent.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))%7B%0A                        editText.setText(%0A                                editText.getText().replace(%0A                                        editText.getSelectionStart(),%0A                                        editText.getSelectionEnd(),%0A                                        dragEvent.getClipData().getItemAt(0).getText()%0A                                )%0A                        );%0A                    %7D%0A                    break;%0A            %7D%0A            return true;%0A        %7D);%0A
         onEd

add drag-and-drop share