/
UploadProjectActivity.java
324 lines (283 loc) · 13.5 KB
/
UploadProjectActivity.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package com.dev.engineerrant;
import static android.text.format.DateUtils.getRelativeTimeSpanString;
import static com.dev.engineerrant.app.toast;
import static com.dev.engineerrant.network.RetrofitClient.BASE_URL;
import static com.dev.engineerrant.network.RetrofitClient.SKY_SERVER_URL;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.dev.engineerrant.adapters.CommunityPostItem;
import com.dev.engineerrant.animations.Tools;
import com.dev.engineerrant.auth.Account;
import com.dev.engineerrant.auth.MyApplication;
import com.dev.engineerrant.network.RetrofitClient;
import com.dev.engineerrant.network.methods.dev.MethodsProfile;
import com.dev.engineerrant.network.methods.git.MethodsRepo;
import com.dev.engineerrant.network.models.dev.ModelProfile;
import com.dev.engineerrant.network.models.git.ModelRepo;
import com.dev.engineerrant.network.models.sky.ModelSuccess;
import com.dev.engineerrant.network.post.RantClient;
import com.dev.engineerrant.network.post.sky.UploadProjectClient;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class UploadProjectActivity extends AppCompatActivity {
EditText editTextTitle, editTextType, editTextDesc, editTextGithub, editTextWebsite, editTextOwner, editTextOwnerUserId, editTextOs, editTextLanguage,editTextDRUrl;
SwitchCompat active, archived;
ProgressBar progressBar;
TextView upload;
@Override
protected void onCreate(Bundle savedInstanceState) {
Tools.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_project);
if (!Account.isLoggedIn()) {
Intent intent = new Intent(UploadProjectActivity.this, LoginActivity.class);
startActivity(intent);
finish();
return;
}
if (!Account.isSessionSkyVerified()) {
Intent intent = new Intent(UploadProjectActivity.this, SkyLoginActivity.class);
startActivity(intent);
finish();
return;
}
initialize();
checkScore();
}
private void checkScore() {
MethodsProfile methods = RetrofitClient.getRetrofitInstance().create(MethodsProfile.class);
String total_url = BASE_URL + "users/"+Account.user_id()+"?app=3";
progressBar.setVisibility(View.VISIBLE);
Call<ModelProfile> call = methods.getAllData(total_url);
call.enqueue(new Callback<ModelProfile>() {
@SuppressLint("SetTextI18n")
@Override
public void onResponse(@NonNull Call<ModelProfile> call, @NonNull Response<ModelProfile> response) {
if (response.isSuccessful()) {
// Do awesome stuff
assert response.body() != null;
if (response.body().getProfile().getScore()<500) {
finish();
toast("500 devRant points required");
}
} else if (response.code() == 429) {
// Handle unauthorized
} else {
toast(response.message());
}
progressBar.setVisibility(View.GONE);
}
@Override
public void onFailure(@NonNull Call<ModelProfile> call, @NonNull Throwable t) {
Log.d("error_contact", t.toString());
toast(t.toString());
progressBar.setVisibility(View.GONE);
}
});
}
private void initialize() {
editTextTitle = findViewById(R.id.editTextTitle);
editTextType = findViewById(R.id.editTextType);
editTextDesc = findViewById(R.id.editTextDesc);
editTextWebsite = findViewById(R.id.editTextWebsite);
editTextGithub = findViewById(R.id.editTextGithub);
editTextOwner = findViewById(R.id.editTextOwner);
editTextOwnerUserId = findViewById(R.id.editTextOwnerUserId);
editTextOs = findViewById(R.id.editTextOs);
editTextLanguage = findViewById(R.id.editTextLanguage);
editTextDRUrl = findViewById(R.id.editTextDRUrl);
active = findViewById(R.id.switchActive);
archived = findViewById(R.id.switchArchived);
upload = findViewById(R.id.upload);
progressBar = findViewById(R.id.progressBar);
progressBar.setVisibility(View.GONE);
}
public void fetchGithub(View view) {
String invalid_message = "enter valid GitHub Repository URL";
String github_url = editTextGithub.getText().toString();
if (github_url.length()<12) {
toast(invalid_message);
return;
}
if (!github_url.contains("github.com")) {
toast(invalid_message);
return;
}
getRepo(github_url);
}
private void getRepo(String github_url) {
String total_url = "https://api.github.com/repos/"+github_url.split("com/")[1];
if (total_url.endsWith("/")) {
total_url = total_url.substring(0, total_url.length() - 1);
}
MethodsRepo methods = RetrofitClient.getRetrofitInstance().create(MethodsRepo.class);
progressBar.setVisibility(View.VISIBLE);
String header = null;
if (Account.githubKey()!=null) {
header = "token "+Account.githubKey();
}
Call<ModelRepo> call = methods.getAllData(header,total_url);
call.enqueue(new Callback<ModelRepo>() {
@SuppressLint({"SetTextI18n", "SimpleDateFormat"})
@Override
public void onResponse(@NonNull Call<ModelRepo> call, @NonNull Response<ModelRepo> response) {
if (response.isSuccessful()) {
// Do awesome stuff
assert response.body() != null;
editTextTitle.setText(response.body().getName());
editTextDesc.setText(response.body().getDescription());
editTextOwner.setText(response.body().getOwner().getLogin());
editTextOs = findViewById(R.id.editTextOs);
archived.setChecked(response.body().getArchived());
editTextLanguage.setText(response.body().getLanguage());
toast("done");
} else if (response.code() == 429) {
// Handle unauthorized
toast("error contacting github error 429");
} else {
toast("error contacting github");
}
progressBar.setVisibility(View.GONE);
}
@Override
public void onFailure(@NonNull Call<ModelRepo> call, @NonNull Throwable t) {
Log.d("error_contact", t.toString());
toast("no network");
progressBar.setVisibility(View.GONE);
}
});
}
public void uploadProject(View view) {
String title = editTextTitle.getText().toString();
String type = editTextType.getText().toString();
String desc = editTextDesc.getText().toString();
String website = editTextWebsite.getText().toString();
String github = editTextGithub.getText().toString();
String owner = editTextOwner.getText().toString();
String ownerUserId = editTextOwnerUserId.getText().toString();
String os = editTextOs.getText().toString();
String language = editTextLanguage.getText().toString();
String relevant_dr_url = editTextDRUrl.getText().toString();
Boolean isActive = active.isChecked();
Boolean isArchived = active.isChecked();
if (title.length()<4||title.length()>2000) {
toast("title must be 4-2000 characters long");
return;
}
if (os.length()>2000) {
toast("os must be max 2000 characters long");
return;
}
if (type.length()<3||type.length()>2000) {
toast("type must be at least 3 characters long");
return;
}
if (desc.length()<3||desc.length()>2000) {
toast("description must be 3-2000 characters long");
return;
}
if (relevant_dr_url.length()>2000) {
toast("relevant_dr_url must be max 2000 characters long");
return;
}
if (website.length()>2000) {
toast("website must be max 2000 characters long");
return;
}
String invalid_message = "enter valid GitHub Repository URL";
if (github.length()<12||github.length()>2000) {
toast(invalid_message);
return;
}
if (!github.contains("github.com")) {
toast(invalid_message);
return;
}
if (owner.length()<3||owner.length()>2000) {
toast("owner name must be at least 3 characters long");
return;
}
upload.setVisibility(View.GONE);
try {
RequestBody _title = RequestBody.create(MediaType.parse("multipart/form-data"), title);
RequestBody _type= RequestBody.create(MediaType.parse("multipart/form-data"), type);
RequestBody _desc= RequestBody.create(MediaType.parse("multipart/form-data"), desc);
RequestBody _website= RequestBody.create(MediaType.parse("multipart/form-data"), website);
RequestBody _github= RequestBody.create(MediaType.parse("multipart/form-data"), github);
RequestBody _owner= RequestBody.create(MediaType.parse("multipart/form-data"), owner);
RequestBody _ownerUserId= RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(ownerUserId));
RequestBody _os= RequestBody.create(MediaType.parse("multipart/form-data"), os);
RequestBody _language= RequestBody.create(MediaType.parse("multipart/form-data"), language);
RequestBody _relevant_dr_url= RequestBody.create(MediaType.parse("multipart/form-data"), relevant_dr_url);
RequestBody _active= RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(isActive));
RequestBody _archived= RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(isArchived));
RequestBody _user_id = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(Account.user_id()));
RequestBody _session_id = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(Account.id()));
Retrofit.Builder builder = new Retrofit.Builder().baseUrl(SKY_SERVER_URL).addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
UploadProjectClient client = retrofit.create(UploadProjectClient.class);
// finally, execute the request
Call<ModelSuccess> call = client.upload(_session_id,_user_id,_title,_os,_type,null,_desc,_relevant_dr_url,_website,_github,_language,_active,_archived,_ownerUserId,_owner);
call.enqueue(new Callback<ModelSuccess>() {
@Override
public void onResponse(@NonNull Call<ModelSuccess> call, @NonNull Response<ModelSuccess> response) {
Log.v("Upload", response + " ");
if (response.isSuccessful()) {
// Do awesome stuff
assert response.body() != null;
Boolean success = response.body().getSuccess();
if (success) {
toast("success!");
Intent intent = new Intent(UploadProjectActivity.this, CommunityActivity.class);
startActivity(intent);
finish();
} else {
toast("failed.");
upload.setVisibility(View.VISIBLE);
// editTextPost.setText(response.message()+" "+response.toString());
}
} else if (response.code() == 400) {
toast("Invalid login credentials entered. Please try again. :(");
// editTextPost.setText(response.message());
upload.setVisibility(View.VISIBLE);
} else if (response.code() == 429) {
// Handle unauthorized
toast("You are not authorized :P");
upload.setVisibility(View.VISIBLE);
} else {
toast(response.message());
upload.setVisibility(View.VISIBLE);
}
}
@Override
public void onFailure(@NonNull Call<ModelSuccess> call, @NonNull Throwable t) {
toast("Request failed! " + t.getMessage());
upload.setVisibility(View.VISIBLE);
}
});
} catch (Exception e) {
toast("failed");
upload.setVisibility(View.VISIBLE);
}
}
}