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 pathUserCoursesLoader.java
More file actions
94 lines (85 loc) · 4.22 KB
/
UserCoursesLoader.java
File metadata and controls
94 lines (85 loc) · 4.22 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
92
93
94
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.Course;
import com.uwflow.flow_android.db_object.UserCourse;
import com.uwflow.flow_android.db_object.UserCourseDetail;
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 UserCoursesLoader extends FlowAbstractDataLoader<UserCourseDetail> {
private static final String TAG = UserCoursesLoader.class.getSimpleName();
private LoaderUpdateReceiver courseLoadedReceiver;
public UserCoursesLoader(Context context, FlowDatabaseHelper flowDatabaseHelper, Fragment baseFragment) {
super(context, flowDatabaseHelper, baseFragment);
}
protected void registerReceiver(){
super.registerReceiver();
// Start watching for changes in the app data.
if (courseLoadedReceiver == null) {
courseLoadedReceiver = new LoaderUpdateReceiver(this, Constants.BroadcastActionId.PROFILE_DATABASE_USER_COURSE_LOADED);
}
}
protected void unregisterReceiver(){
super.unregisterReceiver();
if (courseLoadedReceiver != null) {
LocalBroadcastManager.getInstance(this.getContext().getApplicationContext()).unregisterReceiver(courseLoadedReceiver);
courseLoadedReceiver = null;
}
}
@Override
protected UserCourseDetail loadDelegate() {
//Check if we are loading the logged in user's courseDetail or user friend's course detail
// For all user friend data we fetch from the network
if (mBaseFragment != null && mBaseFragment instanceof ProfileFragment) {
final ProfileFragment profileFragment = (ProfileFragment) mBaseFragment;
if (profileFragment != null && profileFragment.getProfileID() != null) {
// It seems like async client must be run from the Main ui thread
mBaseFragment.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
FlowApiRequests.getUserCourses(
profileFragment.getProfileID(),
new FlowApiRequestCallbackAdapter() {
@Override
public void getUserCoursesCallback(UserCourseDetail userCourseDetail) {
if (userCourseDetail == null) return;
profileFragment.setUserCourses(userCourseDetail);
}
@Override
public void onFailure(String error) {
Crashlytics.log(Log.ERROR, TAG,
"Get user courses API request failed: " + error);
}
});
}
});
return null;
}
}
UserCourseDetail userCourseDetail = new UserCourseDetail();
try {
List<Course> courses = new ArrayList<Course>();
Dao<Course, String> userCourseDao = flowDatabaseHelper.getUserCourseDao();
courses = userCourseDao.queryForAll();
userCourseDetail.setCourses(courses);
List<UserCourse> userCourses = new ArrayList<UserCourse>();
Dao<UserCourse, String> userCourseStringDao = flowDatabaseHelper.getUserCourseExtraDao();
userCourses = userCourseStringDao.queryForAll();
userCourseDetail.setUserCourses(userCourses);
} catch (SQLException e) {
e.printStackTrace();
Crashlytics.logException(e);
}
return userCourseDetail;
}
}