/*
* This file is part of ZSE Info.
*
* ZSE Info is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* any later version.
*
* ZSE Info is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.enbyted.android.zseinfo.data;
import android.os.AsyncTask;
import android.os.Looper;
import android.widget.Toast;
import org.enbyted.android.zseinfo.InfoApp;
import org.enbyted.android.zseinfo.data.configuration.Configuration;
import org.enbyted.android.zseinfo.data.lucky.LuckyHandler;
import org.enbyted.android.zseinfo.data.replacements.AsyncReplacementsTask;
import org.enbyted.android.zseinfo.data.replacements.Replacements;
import org.enbyted.android.zseinfo.data.timetable.AsyncTimetableTask;
import org.enbyted.android.zseinfo.data.timetable.Timetable;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Bartek on 19.02.14.
*/
public class SynchroniseService {
private static SynchroniseService instance = null;
private Map<Integer, Timetable> timetables;
private Replacements replacements;
private SynchroniseService() {
timetables = new HashMap<>();
}
public static SynchroniseService instance() {
if(null == instance) {
instance = new SynchroniseService();
}
return instance;
}
public void refreshCache() {
getReplacements().refreshCache();
for(Branch branch : Configuration.getInstance().getBranches()) {
if(Timetable.isCachedTimetable( branch.getId())) {
getTimetable(branch.getId()).refreshCache();
}
}
}
public void reloadTimetable(int id) {
reloadTimetable(id, true);
}
public void reloadTimetable(int id, boolean async) {
if(async) {
Toast.makeText(InfoApp.getContext(), "Pobieranie aktualnego planu lekcji.", Toast.LENGTH_SHORT).show();
new AsyncTimetableTask().execute(id);
} else {
getTimetable(id).updateTimetable();
}
}
public void reloadReplacements() {
reloadReplacements(true);
}
public void reloadReplacements(boolean async) {
if(! async) {
getReplacements().updateReplacements();
} else {
Toast.makeText(InfoApp.getContext(), "Pobieranie aktualnej listy zastępstw.", Toast.LENGTH_SHORT).show();
new AsyncReplacementsTask().execute();
}
}
public void reloadLuckyTable(boolean async) {
if(getLucky() == null) return;
if(! async) {
getLucky().update();
} else {
Toast.makeText(InfoApp.getContext(), "Pobieranie nowej listy szczęśliwych numerków.", Toast.LENGTH_SHORT).show();
new AsyncTask<LuckyHandler, Void, Void>() {
@Override
protected Void doInBackground(LuckyHandler... luckyHandlers) {
luckyHandlers[0].update();
return null;
}
}.execute(getLucky());
}
}
public LuckyHandler getLucky() {
return Configuration.getInstance().getLuckyConfig().getHandler();
}
public Timetable getTimetable(int id) {
if(! timetables.containsKey(id)) {
timetables.put(id, Timetable.getCachedTimetable(id));
}
if(timetables.get(id).isEmpty() & Looper.myLooper() == Looper.getMainLooper())
reloadTimetable(id);
return timetables.get(id);
}
public Replacements getReplacements() {
if(null == replacements) {
replacements = Replacements.getCachedReplacements();
}
return replacements;
}
}