This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUserScheduleLoader.java
More file actions
91 lines (81 loc) · 3.91 KB
/
UserScheduleLoader.java
File metadata and controls
91 lines (81 loc) · 3.91 KB
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
package com.uwflow.flow_android.loaders;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.crashlytics.android.Crashlytics;
import com.j256.ormlite.dao.Dao;
import com.uwflow.flow_android.constant.Constants;
import com.uwflow.flow_android.dao.FlowDatabaseHelper;
import com.uwflow.flow_android.db_object.ScheduleCourse;
import com.uwflow.flow_android.db_object.ScheduleCourses;
import com.uwflow.flow_android.fragment.ProfileFragment;
import com.uwflow.flow_android.network.FlowApiRequestCallbackAdapter;
import com.uwflow.flow_android.network.FlowApiRequests;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserScheduleLoader extends FlowAbstractDataLoader<ScheduleCourses> {
private static final String TAG = UserScheduleLoader.class.getSimpleName();
private LoaderUpdateReceiver UserScheduleLoadedReceiver;
public UserScheduleLoader(Context context, FlowDatabaseHelper flowDatabaseHelper, Fragment baseFragment) {
super(context, flowDatabaseHelper, baseFragment);
}
protected void registerReceiver(){
super.registerReceiver();
// Start watching for changes in the app data.
if (UserScheduleLoadedReceiver == null) {
UserScheduleLoadedReceiver = new LoaderUpdateReceiver(this, Constants.BroadcastActionId.PROFILE_DATABASE_USER_SCHEDULE_LOADED);
}
}
protected void unregisterReceiver(){
super.unregisterReceiver();
if (UserScheduleLoadedReceiver != null) {
LocalBroadcastManager.getInstance(this.getContext().getApplicationContext()).unregisterReceiver(UserScheduleLoadedReceiver);
UserScheduleLoadedReceiver = null;
}
}
@Override
protected ScheduleCourses loadDelegate() {
// we first check if we should load from database or from the network
if (mBaseFragment != null) {
final ProfileFragment profileFragment = (ProfileFragment) mBaseFragment;
if (profileFragment != null && profileFragment.getProfileID() != null) {
mBaseFragment.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
FlowApiRequests.getUserSchedule(
profileFragment.getProfileID(),
new FlowApiRequestCallbackAdapter() {
@Override
public void getUserScheduleCallback(ScheduleCourses scheduleCourses) {
if (scheduleCourses == null) return;
profileFragment.setUserSchedule(scheduleCourses);
}
@Override
public void onFailure(String error) {
Crashlytics.log(Log.ERROR, TAG,
"Get user schedule API request failed: " + error);
}
});
}
});
return null;
}
}
// load from database for profile
ScheduleCourses scheduleCourses = new ScheduleCourses();
try {
Dao<ScheduleCourse, String> userScheduleDao = flowDatabaseHelper.getUserScheduleCourseDao();
List<ScheduleCourse> courses = userScheduleDao.queryForAll();
scheduleCourses.setScheduleCourses(new ArrayList<ScheduleCourse>(courses));
if (!courses.isEmpty()) {
scheduleCourses.setScreenshotUrl(courses.get(0).getScheduleUrl());
}
} catch (SQLException e) {
e.printStackTrace();
Crashlytics.logException(e);
}
return scheduleCourses;
}
}