Search in sources :

Example 1 with SyncManager

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);
}
Also used : CafeteriaViewModel(de.tum.in.tumcampusapp.component.ui.cafeteria.details.CafeteriaViewModel) SyncManager(de.tum.in.tumcampusapp.utils.sync.SyncManager) KinoViewModel(de.tum.in.tumcampusapp.component.ui.news.KinoViewModel) CafeteriaRemoteRepository(de.tum.in.tumcampusapp.component.ui.cafeteria.repository.CafeteriaRemoteRepository) CafeteriaLocalRepository(de.tum.in.tumcampusapp.component.ui.cafeteria.repository.CafeteriaLocalRepository)

Example 2 with SyncManager

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);
}
Also used : NewsSources(de.tum.in.tumcampusapp.component.ui.news.model.NewsSources) NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) JSONObject(org.json.JSONObject) News(de.tum.in.tumcampusapp.component.ui.news.model.News) SyncManager(de.tum.in.tumcampusapp.utils.sync.SyncManager) JSONArray(org.json.JSONArray)

Example 3 with SyncManager

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);
}
Also used : StudyRoom(de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoom) NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) JSONObject(org.json.JSONObject) SyncManager(de.tum.in.tumcampusapp.utils.sync.SyncManager) StudyRoomGroup(de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoomGroup)

Example 4 with SyncManager

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);
}
Also used : JSONObject(org.json.JSONObject) SyncManager(de.tum.in.tumcampusapp.utils.sync.SyncManager) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 5 with SyncManager

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);
}
Also used : SyncManager(de.tum.in.tumcampusapp.utils.sync.SyncManager) Before(org.junit.Before)

Aggregations

SyncManager (de.tum.in.tumcampusapp.utils.sync.SyncManager)7 JSONObject (org.json.JSONObject)3 NetUtils (de.tum.in.tumcampusapp.utils.NetUtils)2 JSONArray (org.json.JSONArray)2 Intent (android.content.Intent)1 CalendarRow (de.tum.in.tumcampusapp.component.tumui.calendar.model.CalendarRow)1 CafeteriaViewModel (de.tum.in.tumcampusapp.component.ui.cafeteria.details.CafeteriaViewModel)1 CafeteriaLocalRepository (de.tum.in.tumcampusapp.component.ui.cafeteria.repository.CafeteriaLocalRepository)1 CafeteriaRemoteRepository (de.tum.in.tumcampusapp.component.ui.cafeteria.repository.CafeteriaRemoteRepository)1 KinoViewModel (de.tum.in.tumcampusapp.component.ui.news.KinoViewModel)1 News (de.tum.in.tumcampusapp.component.ui.news.model.News)1 NewsSources (de.tum.in.tumcampusapp.component.ui.news.model.NewsSources)1 StudyRoom (de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoom)1 StudyRoomGroup (de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoomGroup)1 Date (java.util.Date)1 JSONException (org.json.JSONException)1 Before (org.junit.Before)1