-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComposeTweetDialogFragment.java
142 lines (119 loc) · 4.63 KB
/
ComposeTweetDialogFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.shawn_duan.mytwitter.fragments;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.shawn_duan.mytwitter.MyTwitterApplication;
import com.shawn_duan.mytwitter.R;
import com.shawn_duan.mytwitter.activities.MainActivity;
import com.shawn_duan.mytwitter.network.TwitterClient;
import com.shawn_duan.mytwitter.rxbus.RefreshTimelineEvent;
import com.shawn_duan.mytwitter.rxbus.RxBus;
import com.shawn_duan.mytwitter.utils.Utils;
import org.json.JSONObject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;
import cz.msebera.android.httpclient.Header;
/**
* Created by sduan on 10/30/16.
*/
public class ComposeTweetDialogFragment extends DialogFragment {
private final static String TAG = ComposeTweetDialogFragment.class.getSimpleName();
private MainActivity mActivity;
private TwitterClient mClient;
@BindView(R.id.etComposeBody)
EditText etComposeBody;
@BindView(R.id.btSubmit)
Button btSubmit;
@BindView(R.id.tvRemainingCount)
TextView tvRemainingCounter;
private Unbinder unbinder;
private SharedPreferences mSharedPreferences;
private String draft;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mClient = MyTwitterApplication.getRestClient(); // singleton client
mActivity = (MainActivity) getActivity();
mSharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
draft = mSharedPreferences.getString(getString(R.string.compose_draft), "");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_compose, container, false);
unbinder = ButterKnife.bind(this, view);
etComposeBody.append(draft);
return view;
}
@OnClick(R.id.btSubmit)
void submitNewTweet() {
if (etComposeBody != null) {
String content = etComposeBody.getText().toString();
mClient.composeTweet(content, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
draft = "";
mActivity.onBackPressed();
Snackbar.make(mActivity.mToolbar, "Tweet submitted successfully!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
Log.e(TAG, "Failed to post new Tweet.");
}
});
}
}
@OnTextChanged(R.id.etComposeBody)
void updateRemainingCount(Editable editable) {
draft = editable.toString();
int remaining = 140 - draft.length();
tvRemainingCounter.setText(String.valueOf(140 - draft.length()));
if (remaining < 0) {
tvRemainingCounter.setTextColor(getResources().getColor(R.color.red));
btSubmit.setClickable(false);
} else {
tvRemainingCounter.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
btSubmit.setClickable(true);
}
}
@Override
public void onResume() {
super.onResume();
((MainActivity)getActivity()).hideFab();
Utils.showSoftKeyboard(getActivity(), etComposeBody);
}
@Override
public void onPause() {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(getString(R.string.compose_draft), draft);
editor.commit();
((MainActivity)getActivity()).showFab();
Utils.hideSoftKeyboard(getActivity(), etComposeBody);
super.onPause();
}
@Override
public void onDestroyView() {
unbinder.unbind();
super.onDestroyView();
}
}