use of de.tum.in.tumcampusapp.utils.sync.SyncManager in project TumCampusApp by TCA-Team.
the class DownloadService method onCreate.
@Override
public void onCreate() {
super.onCreate();
Utils.log("DownloadService service has started");
broadcastManager = LocalBroadcastManager.getInstance(this);
new SyncManager(this);
CafeteriaRemoteRepository remoteRepository = CafeteriaRemoteRepository.INSTANCE;
remoteRepository.setTumCabeClient(TUMCabeClient.getInstance(this));
CafeteriaLocalRepository localRepository = CafeteriaLocalRepository.INSTANCE;
localRepository.setDb(TcaDb.getInstance(this));
cafeteriaViewModel = new CafeteriaViewModel(localRepository, remoteRepository, mDisposable);
// Init sync table
KinoLocalRepository.INSTANCE.setDb(TcaDb.getInstance(this));
KinoRemoteRepository.INSTANCE.setTumCabeClient(TUMCabeClient.getInstance(this));
kinoViewModel = new KinoViewModel(KinoLocalRepository.INSTANCE, KinoRemoteRepository.INSTANCE, mDisposable);
}
use of de.tum.in.tumcampusapp.utils.sync.SyncManager in project TumCampusApp by TCA-Team.
the class NewsController method downloadFromExternal.
/**
* Download news from external interface (JSON)
*
* @param force True to force download over normal sync period, else false
* @throws JSONException parsing could fail
*/
public void downloadFromExternal(boolean force) throws JSONException {
SyncManager sync = new SyncManager(mContext);
if (!force && !sync.needSync(this, TIME_TO_SYNC)) {
return;
}
NetUtils net = new NetUtils(mContext);
// Load all news sources
Optional<JSONArray> jsonArray = net.downloadJsonArray(NEWS_SOURCES_URL, CacheManager.VALIDITY_ONE_MONTH, force);
if (jsonArray.isPresent()) {
JSONArray arr = jsonArray.get();
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
newsSourcesDao.insert(new NewsSources(obj.getInt(Const.JSON_SOURCE), obj.getString(Const.JSON_TITLE), obj.has(Const.JSON_ICON) ? obj.getString(Const.JSON_ICON) : ""));
}
}
// Load all news since the last sync
jsonArray = net.downloadJsonArray(NEWS_URL + getLastId(), CacheManager.VALIDITY_ONE_DAY, force);
// Delete all too old items
cleanupDb();
if (!jsonArray.isPresent()) {
return;
}
JSONArray arr = jsonArray.get();
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
newsDao.insert(new News(obj.getString(Const.JSON_NEWS), obj.getString(Const.JSON_TITLE), obj.getString(Const.JSON_LINK), obj.getString(Const.JSON_SRC), obj.getString(Const.JSON_IMAGE), DateUtils.getDateTime(obj.getString(Const.JSON_DATE)), DateUtils.getDateTime(obj.getString(Const.JSON_CREATED)), 0));
}
sync.replaceIntoDb(this);
}
use of de.tum.in.tumcampusapp.utils.sync.SyncManager in project TumCampusApp by TCA-Team.
the class StudyRoomGroupManager method downloadFromExternal.
public void downloadFromExternal() throws JSONException {
Optional<JSONObject> jsonObject = new NetUtils(mContext).downloadJsonObject(STUDYROOM_URL, CacheManager.VALIDITY_DO_NOT_CACHE, true);
if (!jsonObject.isPresent()) {
return;
}
dao.removeCache();
groupDao.removeCache();
List<StudyRoomGroup> groups = getAllFromJson(jsonObject.get());
for (StudyRoomGroup group : groups) {
groupDao.insert(group);
for (StudyRoom room : group.getRooms()) {
room.setStudyRoomGroup(group.getId());
dao.insert(room);
}
}
new SyncManager(mContext).replaceIntoDb(this);
}
use of de.tum.in.tumcampusapp.utils.sync.SyncManager in project TumCampusApp by TCA-Team.
the class CafeteriaMenuManager method downloadFromExternal.
/**
* Download cafeteria menus from external interface (JSON)
*
* @param force True to force download over normal sync period, else false
*/
public void downloadFromExternal(Context context, boolean force) {
SyncManager sync = new SyncManager(mContext);
if (!force && !sync.needSync(this, TIME_TO_SYNC)) {
return;
}
String url = "http://lu32kap.typo3.lrz.de/mensaapp/exportDB.php?mensa_id=all";
Optional<JSONObject> json = NetUtils.downloadJson(context, url);
if (!json.isPresent()) {
return;
}
JSONObject obj = json.get();
menuDao.removeCache();
try {
JSONArray menu = obj.getJSONArray("mensa_menu");
for (int j = 0; j < menu.length(); j++) {
menuDao.insert(getFromJson(menu.getJSONObject(j)));
}
JSONArray beilagen = obj.getJSONArray("mensa_beilagen");
for (int j = 0; j < beilagen.length(); j++) {
menuDao.insert(getFromJsonAddendum(beilagen.getJSONObject(j)));
}
} catch (JSONException e) {
Utils.log(e);
}
sync.replaceIntoDb(this);
scheduleFoodAlarms(true);
}
use of de.tum.in.tumcampusapp.utils.sync.SyncManager in project TumCampusApp by TCA-Team.
the class SyncManagerTest method setUp.
@Before
public void setUp() {
dao = TcaDb.getInstance(RuntimeEnvironment.application).syncDao();
dao.removeCache();
syncManager = new SyncManager(RuntimeEnvironment.application);
}
Aggregations