Android Open Source - cheepcheep New Tweet Dialog






From Project

Back to project page cheepcheep.

License

The source code is released under:

Apache License

If you think the Android project cheepcheep listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.tarrio.cheepcheep.dialog;
/*  ww w  .ja  v  a 2 s. c  o m*/
import org.tarrio.cheepcheep.R;
import org.tarrio.cheepcheep.service.PreferencesProvider;
import org.tarrio.cheepcheep.task.AsyncTwitterTask;
import org.tarrio.cheepcheep.task.CreateNewTweetTask;
import org.tarrio.cheepcheep.task.TaskCallback;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class NewTweetDialog extends CheepCheepDialog {

  public NewTweetDialog(Activity activity,
      PreferencesProvider preferencesProvider, TaskCallback callback,
      String prefillScreenName, String prefillText, long responseToId) {
    super(activity, preferencesProvider, callback);
    createDialog(prefillScreenName, prefillText, responseToId);
  }

  private void createDialog(String prefillScreenName, String prefillText,
      long responseToId) {
    dialog = new Dialog(activity);
    View view = activity.getLayoutInflater().inflate(R.layout.newtweet,
        null);
    dialog.setContentView(view);
    NewTweetDialogListener listener = new NewTweetDialogListener(activity,
        dialog, view, responseToId);
    ((Button) view.findViewById(R.id.NewTweetPostButton))
        .setOnClickListener(listener);
    EditText editText = (EditText) view.findViewById(R.id.NewTweetEdit);
    editText.addTextChangedListener(listener);
    dialog.setTitle(R.string.new_tweet_title);
    StringBuilder prefill = new StringBuilder();
    if ((prefillScreenName != null) && !"".equals(prefillScreenName)) {
      if (responseToId <= 0)
        prefill.append("RT ");
      prefill.append("@");
      prefill.append(prefillScreenName);
      prefill.append(" ");
    }
    if ((prefillText != null) && !"".equals(prefillText))
      prefill.append(prefillText);
    editText.setText(prefill.toString());
  }

  /**
   * Code that manages the "New Tweet" dialog.
   */
  private class NewTweetDialogListener implements View.OnClickListener,
      TextWatcher {

    private Context context;
    private Dialog dialog;
    private View view;
    private long responseToId;

    public NewTweetDialogListener(Context context, Dialog dialog,
        View view, long responseToId) {
      this.context = context;
      this.dialog = dialog;
      this.view = view;
      this.responseToId = responseToId;
    }

    @Override
    public void onClick(View v) {
      if (v.getId() == R.id.NewTweetPostButton) {
        EditText tweetEdit = (EditText) view
            .findViewById(R.id.NewTweetEdit);
        final String text = tweetEdit.getText().toString();
        TaskCallback repeatDialogCallback = new TaskCallback() {
          @Override
          public void onSuccess(AsyncTwitterTask task) {
            callback.onSuccess(task);
          }

          @Override
          public void onFailure(int statusCode, AsyncTwitterTask task) {
            callback.onFailure(statusCode, task);
            new NewTweetDialog(activity, preferencesProvider,
                callback, null, text, responseToId).show();
          }
        };
        new CreateNewTweetTask(activity, preferencesProvider.get(),
            repeatDialogCallback, text, responseToId).run();
        dialog.dismiss();
      }
    }

    @Override
    public void afterTextChanged(Editable s) {
      int textLength = s.length();
      int charsLeft = 140 - textLength;
      String countText = String.format(context
          .getString(R.string.character_count_fmt), charsLeft);
      ((TextView) view.findViewById(R.id.NewTweetNumCharsLabel))
          .setText(countText);
      Button postButton = (Button) view
          .findViewById(R.id.NewTweetPostButton);
      postButton.setEnabled((charsLeft >= 0) && (textLength > 0));
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
      // nothing, but we have to define this member function.
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
        int count) {
      // nothing, but we have to define this member function.
    }
  }
}




Java Source Code List

org.tarrio.cheepcheep.Constants.java
org.tarrio.cheepcheep.HomeTimelineActivity.java
org.tarrio.cheepcheep.OAuthActivity.java
org.tarrio.cheepcheep.SingleTweetActivity.java
org.tarrio.cheepcheep.TweetListActions.java
org.tarrio.cheepcheep.TweetListAdapter.java
org.tarrio.cheepcheep.UserInfoActivity.java
org.tarrio.cheepcheep.dialog.CheepCheepDialog.java
org.tarrio.cheepcheep.dialog.ConfirmDeleteTweetDialog.java
org.tarrio.cheepcheep.dialog.NewTweetDialog.java
org.tarrio.cheepcheep.exceptions.AuthError.java
org.tarrio.cheepcheep.exceptions.CheepCheepException.java
org.tarrio.cheepcheep.exceptions.NetError.java
org.tarrio.cheepcheep.exceptions.ParseError.java
org.tarrio.cheepcheep.exceptions.TweetNotFoundError.java
org.tarrio.cheepcheep.exceptions.UserNotFoundError.java
org.tarrio.cheepcheep.http.HttpClientFactory.java
org.tarrio.cheepcheep.http.OAuthHttpClient.java
org.tarrio.cheepcheep.model.Preferences.java
org.tarrio.cheepcheep.model.Tweet.java
org.tarrio.cheepcheep.model.User.java
org.tarrio.cheepcheep.service.PreferencesProvider.java
org.tarrio.cheepcheep.service.TwitterJSONSerializer.java
org.tarrio.cheepcheep.service.TwitterService.java
org.tarrio.cheepcheep.service.TwitterStatusSaverService.java
org.tarrio.cheepcheep.task.AsyncTwitterTask.java
org.tarrio.cheepcheep.task.CreateNewTweetTask.java
org.tarrio.cheepcheep.task.DeleteTweetTask.java
org.tarrio.cheepcheep.task.FollowUnfollowUserTask.java
org.tarrio.cheepcheep.task.GetSingleTweetTask.java
org.tarrio.cheepcheep.task.GetUserInfoTask.java
org.tarrio.cheepcheep.task.TaskCallback.java
org.tarrio.cheepcheep.task.UpdateTweetsTask.java
org.tarrio.cheepcheep.task.VerifyCredentialsTask.java