-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathMainActivity.java
206 lines (181 loc) · 10.5 KB
/
MainActivity.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.apps.nishtha.twitterlec12;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
ProgressDialog progressDialog;
RecyclerView recyclerView;
// Button btnGo;
FloatingActionButton floatingButton;
// EditText editText;
OkHttpClient okHttpClient;
String userName;
String baseUrl = "http://loklak.org/api/search.json?q=";
ArrayList<Statuses> statusesArrayList = new ArrayList<>();
EditText editTextDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recView);
final TweetAdapter tweetAdapter = new TweetAdapter(statusesArrayList, this);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
recyclerView.setAdapter(tweetAdapter);
// btnGo = (Button) findViewById(R.id.btnGo);
okHttpClient = new OkHttpClient();
// editText = (EditText) findViewById(R.id.editText);
floatingButton = (FloatingActionButton) findViewById(R.id.floatingButton);
// statusesArrayList=new ArrayList<>(); //NOT AGAIN! now pointing to a new location other than where Adapter is pointing
floatingButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final View v = ((LayoutInflater.from(MainActivity.this))).inflate(R.layout.alert_dialog, null, false);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog
.setView(v)
.setTitle("Search tweets")
.setIcon(R.drawable.twitter_icon)
.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
editTextDialog = v.findViewById(R.id.editTextDialog);
userName = editTextDialog.getText().toString();
Request request = new Request.Builder()
.url(baseUrl + userName)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("TAG", "onFailure: " + e.getLocalizedMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
MainActivity.this.runOnUiThread(new Runnable() { //TODO
@Override
public void run() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.show();
}
});
String result = response.body().string();
Gson gson = new Gson();
final Tweet tweet = gson.fromJson(result, Tweet.class);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.hide();
// Log.d("TAG text : ", "run: " + tweet.getStatuses());
// Log.d("TAG user screen name ", "run: " + tweet.getStatuses().size());
Log.d("TAG size AL ", "run: " + statusesArrayList.size());
if (statusesArrayList.size() != 0) {
statusesArrayList.clear();
tweetAdapter.notifyDataSetChanged();
//All this didnt work!!
// for(int i=0;i<statusesArrayList.size();i++){
// statusesArrayList.remove(i); //why?
// }
// statusesArrayList=new ArrayList<Statuses>();//new lcation in mmemory
// statusesArrayList.removeAll(tweet.getStatuses());
//
}
statusesArrayList.addAll(tweet.getStatuses());
if (statusesArrayList.size() == 0) { //TODO
Toast.makeText(MainActivity.this, "No results found", Toast.LENGTH_SHORT).show();
final Snackbar snackbar = Snackbar.make(new CoordinatorLayout(MainActivity.this), "No results found", Snackbar.LENGTH_SHORT);
snackbar.setAction("Ok", new View.OnClickListener() {
@Override
public void onClick(View view) {
snackbar.dismiss();
}
});
TextView textSnackbar = snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
textSnackbar.setTextColor(Color.CYAN);
snackbar.setActionTextColor(Color.RED);
snackbar.show();
// }
tweetAdapter.notifyDataSetChanged();
}
}
});
}
});
dialogInterface.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) { //TODO
Log.d("TAG", "onClick: cancelled");
final Snackbar snackbar = Snackbar.make(new CoordinatorLayout(MainActivity.this), "Cancelled", Snackbar.LENGTH_SHORT);
snackbar.setAction("Ok", new View.OnClickListener() {
@Override
public void onClick(View view) {
snackbar.dismiss();
}
});
snackbar.setActionTextColor(Color.RED);
TextView textViewSnackbar = snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
textViewSnackbar.setTextColor(Color.CYAN);
snackbar.show();
dialogInterface.dismiss();
}
})
.show();
}
});
// btnGo.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// userName = editText.getText().toString();
// Request request = new Request.Builder()
// .url(baseUrl + userName)
// .build();
// okHttpClient.newCall(request).enqueue(new Callback() {
// @Override
// public void onFailure(Call call, IOException e) {
// Log.e("TAG", "onFailure: " + e.getLocalizedMessage());
// }
//
// @Override
// public void onResponse(Call call, Response response) throws IOException {
// String result = response.body().string();
// Gson gson = new Gson();
// final Tweet tweet = gson.fromJson(result, Tweet.class);
//
// MainActivity.this.runOnUiThread(new Runnable() {
// @Override
// public void run() {
// Log.d("TAG text : ", "run: " + tweet.getStatuses());
// Log.d("TAG user screen name ", "run: " + tweet.getStatuses().size());
// statusesArrayList.addAll(tweet.getStatuses());
// tweetAdapter.notifyDataSetChanged();
// }
// });
// }
// });
// }
// });
}
}