package adullact.publicrowdfunding.model.local.utilities;
+import android.content.SharedPreferences;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
import java.util.regex.Pattern;
-import org.joda.time.DateTime;
-
-import rx.Scheduler;
-import rx.functions.Action0;
-import rx.schedulers.Schedulers;
+import adullact.publicrowdfunding.PublicrowdFundingApplication;
import adullact.publicrowdfunding.model.local.callback.NothingToDo;
import adullact.publicrowdfunding.model.local.callback.WhatToDo;
import adullact.publicrowdfunding.model.local.database.ProjectsDatabase;
-import adullact.publicrowdfunding.model.local.ressource.Account;
import adullact.publicrowdfunding.model.local.ressource.Project;
import adullact.publicrowdfunding.model.server.event.ListerEvent;
+import rx.Scheduler;
+import rx.functions.Action0;
+import rx.schedulers.Schedulers;
/**
- * Created by Ferrand on 19/07/2014.
+ * @author Ferrand and Nelaupe
*/
public class SyncServerToLocal {
/* --- Singleton --- */
for(Project project : projects) {
m_projects.add(project);
+ project.getCache(); // create a cache
}
+
+ SharedPreferences sharedPreferences = PublicrowdFundingApplication.sharedPreferences();
+ m_lastSync = Utility.stringToDateTime(sharedPreferences.getString(KEY_LAST_SYNC, "2000-01-01 00:00:00"));
}
+
public static SyncServerToLocal getInstance() {
if(m_instance == null) {
m_instance = new SyncServerToLocal();
/* ----------------- */
private TreeSet<Project> m_projects;
+ private DateTime m_lastSync;
+ private Searcher m_currentSearcher;
+ private Comparator<Project> m_currentComparator;
+ private static String KEY_LAST_SYNC = "lastSync";
public TreeSet<Project> getProjects() {
return m_projects;
}
+ public void forceSyncAll(final WhatToDo<Project> projectWhatToDo) {
+ m_lastSync = new DateTime(2000, 1, 1, 0, 0);
+ sync(projectWhatToDo);
+ }
+
public void sync() {
sync(new NothingToDo<Project>());
}
public void sync(final WhatToDo<Project> projectWhatToDo) {
- final DateTime now = DateTime.now();
+ final DateTime now = DateTime.now(DateTimeZone.getDefault());
final SyncServerToLocal _this = this;
ListerEvent<Project> event = new ListerEvent<Project>() {
@Override
public void onLister(ArrayList<Project> projects) {
- Account.getOwnOrAnonymous().setLastSync(now);
+ m_lastSync = now;
+ SharedPreferences.Editor editor = PublicrowdFundingApplication.sharedPreferences().edit();
+
+ editor.putString(KEY_LAST_SYNC, Utility.DateTimeToString(m_lastSync));
+ editor.apply();
final ArrayList<Project> newProjects = new ArrayList<Project>();
final ArrayList<Project> updatedProjects = new ArrayList<Project>();
if(project.isActive()) {
if(m_projects.contains(project)) {
updatedProjects.add(project);
+ project.getCache().forceRetrieve().setResource(project);
}
else {
newProjects.add(project);
+ project.getCache(); // create a cache
}
}
else {
projectWhatToDo.hold(project);
}
- m_projects.addAll(projects);
+ m_projects.addAll(newProjects);
+ m_projects.removeAll(deletedProject);
+
_this.syncLocalDatabase(newProjects, updatedProjects, deletedProject);
projectWhatToDo.eventually();
}
public void errorNetwork() {
projectWhatToDo.eventually();
}
+
+ @Override
+ public void errorServer() {
+ projectWhatToDo.eventually();
+ }
};
- if(Account.getOwnOrAnonymous().getLastSync() == null) {
- (new Project()).serverLister(event);
- }
- else {
- (new Project()).serverListerToSync(event, Account.getOwnOrAnonymous().getLastSync());
- }
+
+ new Project().serverListerToSync(event, m_lastSync);
}
private void syncLocalDatabase(final ArrayList<Project> newProjects, final ArrayList<Project> updatedProjects, final ArrayList<Project> deletedProjects) {
boolean searchTest(Project project, String motif);
}
+ private interface Filter {
+ boolean filterTest(Project project);
+ }
+
+
public ArrayList<Project> searchInName(final String motif) {
return search(motif, new Searcher() {
@Override
});
}
+ public ArrayList<Project> restrictToValidatedProjects() {
+ return restrict(new Filter() {
+ @Override
+ public boolean filterTest(Project project) {
+ return project.isValidate();
+ }
+ });
+ }
+
+ public ArrayList<Project> restrictToNotValidatedProjects() {
+ return restrict(new Filter() {
+ @Override
+ public boolean filterTest(Project project) {
+ return !project.isValidate();
+ }
+ });
+ }
+
+ private ArrayList<Project> restrict(Filter filter) {
+ ArrayList<Project> res = new ArrayList<Project>();
+
+ for(Project project : m_projects) {
+ if(filter.filterTest(project)) {
+ res.add(project);
+ }
+ }
+
+ return res;
+ }
+
+
private ArrayList<Project> search(final String motif, Searcher searcher) {
+ m_currentSearcher = searcher;
ArrayList<Project> res = new ArrayList<Project>();
for(Project project : m_projects) {
return res;
}
+
+ public ArrayList<Project> sortByRequestingProjectMaxToMin() {
+ return sort(new Comparator<Project>() {
+ @Override
+ public int compare(Project lhs, Project rhs) {
+ return rhs.getRequestedFundingLongValue().compareTo(lhs.getRequestedFundingLongValue());
+ }
+ });
+ }
+
+ public ArrayList<Project> sortByRequestingProjectMinToMax() {
+ return sort(new Comparator<Project>() {
+ @Override
+ public int compare(Project lhs, Project rhs) {
+ return lhs.getRequestedFundingLongValue().compareTo(rhs.getRequestedFundingLongValue());
+ }
+ });
+ }
+
+ public ArrayList<Project> sortByAlmostFunded() {
+ return sort(new Comparator<Project>() {
+ @Override
+ public int compare(Project lhs, Project rhs) {
+ return rhs.getPercentOfAchievement().compareTo(lhs.getPercentOfAchievement());
+ }
+ });
+
+ }
+
+ public ArrayList<Project> sortByProximity() {
+ return sort(new Comparator<Project>() {
+ @Override
+ public int compare(Project lhs, Project rhs) {
+ return Calcul.distance(Share.position, rhs.getPosition()).compareTo(Calcul.distance(Share.position, lhs.getPosition()));
+ }
+ });
+
+ }
+
+ private ArrayList<Project> sort(final Comparator<Project> sorter) {
+ m_currentComparator = sorter;
+ ArrayList<Project> res = new ArrayList<Project>(m_projects);
+
+ Collections.sort(res, sorter);
+
+ return res;
+ }
}