-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassDownloaderHelper.java
179 lines (139 loc) · 7.01 KB
/
ClassDownloaderHelper.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
package naomi.me.spotopen;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import java.util.ArrayList;
import java.util.List;
import naomi.me.spotopen.Api.UWApiInterface;
import naomi.me.spotopen.Model.UWClass;
import naomi.me.spotopen.Model.UWClassWrapper;
import naomi.me.spotopen.Model.UWTermData;
import naomi.me.spotopen.Model.UWTermWrapper;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by naomikoo on 2016-09-04.
*/
public class ClassDownloaderHelper {
public static final String BASE_URL = "https://api.uwaterloo.ca/v2/"; //todo
private static String API_KEY = "c17f04337a20f48f2644be97c63cba74"; //todo
public static void downloadAndUpdateDB(String term, String subject, String number, final String section, final Context context) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
UWApiInterface service = retrofit.create(UWApiInterface.class);
Call<UWClassWrapper> call = service.getClass(term, subject, number, API_KEY); //todo replace subjects with db stuff
call.enqueue(new Callback<UWClassWrapper>() {
@Override
public void onResponse(Call<UWClassWrapper> call, Response<UWClassWrapper> response) {
UWClassWrapper uwClassWrapper = response.body();
List<UWClass> list = uwClassWrapper.getUwClasses();
// can you specify sections? research // todo: check if you can specify section in api
for (UWClass uwclass : list) {
if (uwclass.getSection().equals(section)) { //todo: replace section with db section
ClassApplication.db.updateClassCapacity(uwclass);
if (uwclass.getTotalEnrolled() < uwclass.getTotalCapacity()) {
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://quest.uwaterloo.ca"));
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification n = new NotificationCompat.Builder(context)
.setContentTitle("Spot open in " + uwclass.getSubject() + uwclass.getNumber())
.setContentText(uwclass.getSection())
.setContentIntent(contentIntent)
.setPriority(Notification.PRIORITY_HIGH)
.setSmallIcon(R.drawable.pluscircle)
.setAutoCancel(true).build();
notificationManager.notify(289, n);
}
}
}
}
@Override
public void onFailure(Call<UWClassWrapper> call, Throwable t) {
}
});
}
public static void downloadClassesAndReturnClasses(String term, String subject, String number, final ClassDownloadCallback classDownloadCallback) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
UWApiInterface service = retrofit.create(UWApiInterface.class);
final List<UWClass> classList = new ArrayList<>();
Call<UWClassWrapper> call = service.getClass(term, subject, number, API_KEY); //todo replace subjects with db stuff
call.enqueue(new Callback<UWClassWrapper>() {
@Override
public void onResponse(Call<UWClassWrapper> call, Response<UWClassWrapper> response) {
UWClassWrapper uwClassWrapper = response.body();
List<UWClass> list = uwClassWrapper.getUwClasses();
// can you specify sections? research // todo: check if you can specify section in api
for (UWClass uwclass : list) {
if (uwclass.getSection().contains("LEC")) {
classList.add(uwclass);
}
}
classDownloadCallback.onDownloadFinish(classList);
}
@Override
public void onFailure(Call<UWClassWrapper> call, Throwable t) {
}
});
}
public static List<String> getListOfTerms(final AdapterCallback callback) {
final List<String> terms = new ArrayList<>();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
UWApiInterface service = retrofit.create(UWApiInterface.class);
Call<UWTermWrapper> call = service.getTerms(API_KEY);
call.enqueue(new Callback<UWTermWrapper>() {
@Override
public void onResponse(Call<UWTermWrapper> call, Response<UWTermWrapper> response) {
UWTermWrapper wrapper = response.body();
UWTermData data = wrapper.getTermData();
terms.add(Integer.toString(data.getPreviousTerm()));
terms.add(Integer.toString(data.getCurrentTerm()));
terms.add(Integer.toString(data.getNextTerm()));
callback.notifyDatasetChanged();
}
@Override
public void onFailure(Call<UWTermWrapper> call, Throwable t) {
}
});
return terms;
}
public static void getListOfClasses(final String term, final AdapterCallback callback) {
final List<UWClass> courses = new ArrayList<>();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
UWApiInterface service = retrofit.create(UWApiInterface.class);
Call<UWClassWrapper> call = service.getCourses(term, API_KEY);
call.enqueue(new Callback<UWClassWrapper>() {
@Override
public void onResponse(Call<UWClassWrapper> call, Response<UWClassWrapper> response) {
if (response != null && response.body() != null) {
List<UWClass> classList = response.body().getUwClasses();
for (UWClass uwClass : classList) {
courses.add(uwClass);
}
}
callback.setupSubjectSpinner(courses, term);
}
@Override
public void onFailure(Call<UWClassWrapper> call, Throwable t) {
}
});
}
}