Refactor model layout (#92)

This commit is contained in:
Rafał Borcz 2018-04-29 12:45:05 +02:00 committed by Mikołaj Pich
parent 3443b01b9a
commit 09a8cc38f9
31 changed files with 359 additions and 454 deletions

View File

@ -45,9 +45,9 @@ public class WulkanowyApp extends Application {
} }
private void initializeUserSession() { private void initializeUserSession() {
if (repository.getCurrentUserId() != 0) { if (repository.getSharedRepo().isUserLoggedIn()) {
try { try {
repository.initLastUser(); repository.getSyncRepo().initLastUser();
} catch (Exception e) { } catch (Exception e) {
LogUtils.error("An error occurred when the application was started", e); LogUtils.error("An error occurred when the application was started", e);
} }

View File

@ -1,240 +1,50 @@
package io.github.wulkanowy.data; package io.github.wulkanowy.data;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.data.db.dao.DbContract;
import io.github.wulkanowy.data.db.dao.entities.Account;
import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson;
import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.dao.entities.DiaryDao;
import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.data.db.dao.entities.GradeDao;
import io.github.wulkanowy.data.db.dao.entities.SemesterDao;
import io.github.wulkanowy.data.db.dao.entities.StudentDao;
import io.github.wulkanowy.data.db.dao.entities.Subject;
import io.github.wulkanowy.data.db.dao.entities.SymbolDao;
import io.github.wulkanowy.data.db.dao.entities.Week;
import io.github.wulkanowy.data.db.dao.entities.WeekDao;
import io.github.wulkanowy.data.db.resources.ResourcesContract; import io.github.wulkanowy.data.db.resources.ResourcesContract;
import io.github.wulkanowy.data.db.shared.SharedPrefContract; import io.github.wulkanowy.data.db.shared.SharedPrefContract;
import io.github.wulkanowy.data.sync.SyncContract; import io.github.wulkanowy.data.sync.SyncContract;
import io.github.wulkanowy.data.sync.account.AccountSyncContract;
import io.github.wulkanowy.data.sync.attendance.AttendanceSyncContract;
import io.github.wulkanowy.data.sync.timetable.TimetableSyncContract;
import io.github.wulkanowy.di.annotations.SyncGrades;
import io.github.wulkanowy.di.annotations.SyncSubjects;
import io.github.wulkanowy.utils.security.CryptoException;
@Singleton @Singleton
public class Repository implements RepositoryContract { public class Repository implements RepositoryContract {
private final SharedPrefContract sharedPref; private final DbContract database;
private final ResourcesContract resources; private final ResourcesContract resources;
private final DaoSession daoSession; private final SharedPrefContract sharedPref;
private final AccountSyncContract accountSync; private final SyncContract synchronization;
private final AttendanceSyncContract attendanceSync;
private final TimetableSyncContract timetableSync;
private final SyncContract gradeSync;
private final SyncContract subjectSync;
@Inject @Inject
Repository(SharedPrefContract sharedPref, Repository(DbContract database, ResourcesContract resources, SharedPrefContract sharedPref,
ResourcesContract resources, SyncContract synchronization) {
DaoSession daoSession, this.database = database;
AccountSyncContract accountSync,
AttendanceSyncContract attendanceSync,
TimetableSyncContract timetableSync,
@SyncGrades SyncContract gradeSync,
@SyncSubjects SyncContract subjectSync) {
this.sharedPref = sharedPref;
this.resources = resources; this.resources = resources;
this.daoSession = daoSession; this.sharedPref = sharedPref;
this.accountSync = accountSync; this.synchronization = synchronization;
this.attendanceSync = attendanceSync;
this.timetableSync = timetableSync;
this.gradeSync = gradeSync;
this.subjectSync = subjectSync;
} }
@Override @Override
public long getCurrentUserId() { public SharedPrefContract getSharedRepo() {
return sharedPref.getCurrentUserId(); return sharedPref;
} }
@Override @Override
public void setTimetableWidgetState(boolean nextDay) { public ResourcesContract getResRepo() {
sharedPref.setTimetableWidgetState(nextDay); return resources;
} }
@Override @Override
public boolean getTimetableWidgetState() { public DbContract getDbRepo() {
return sharedPref.getTimetableWidgetState(); return database;
} }
@Override @Override
public int getStartupTab() { public SyncContract getSyncRepo() {
return sharedPref.getStartupTab(); return synchronization;
}
@Override
public boolean isShowGradesSummary() {
return sharedPref.isShowGradesSummary();
}
@Override
public int getServicesInterval() {
return sharedPref.getServicesInterval();
}
@Override
public boolean isServicesEnable() {
return sharedPref.isServicesEnable();
}
@Override
public boolean isNotifyEnable() {
return sharedPref.isNotifyEnable();
}
@Override
public boolean isMobileDisable() {
return sharedPref.isMobileDisable();
}
@Override
public String[] getSymbolsKeysArray() {
return resources.getSymbolsKeysArray();
}
@Override
public String[] getSymbolsValuesArray() {
return resources.getSymbolsValuesArray();
}
@Override
public String getErrorLoginMessage(Exception e) {
return resources.getErrorLoginMessage(e);
}
@Override
public String getAttendanceLessonDescription(AttendanceLesson lesson) {
return resources.getAttendanceLessonDescription(lesson);
}
@Override
public void registerUser(String email, String password, String symbol) throws VulcanException,
IOException, CryptoException {
accountSync.registerUser(email, password, symbol);
}
@Override
public void initLastUser() throws VulcanException, IOException, CryptoException {
accountSync.initLastUser();
}
@Override
public void syncGrades() throws VulcanException, IOException, ParseException {
gradeSync.sync(getCurrentSemesterId());
}
@Override
public void syncSubjects() throws VulcanException, IOException, ParseException {
subjectSync.sync(getCurrentSemesterId());
}
@Override
public void syncAttendance() throws ParseException, IOException, VulcanException {
attendanceSync.syncAttendance(getCurrentDiaryId());
}
@Override
public void syncAttendance(String date) throws ParseException, IOException, VulcanException {
attendanceSync.syncAttendance(getCurrentDiaryId(), date);
}
@Override
public void syncTimetable() throws VulcanException, IOException, ParseException {
timetableSync.syncTimetable(getCurrentDiaryId());
}
@Override
public void syncTimetable(String date) throws VulcanException, IOException, ParseException {
timetableSync.syncTimetable(getCurrentDiaryId(), date);
}
@Override
public void syncAll() throws VulcanException, IOException, ParseException {
syncSubjects();
syncGrades();
syncAttendance();
syncTimetable();
}
@Override
public Account getCurrentUser() {
return daoSession.getAccountDao().load(sharedPref.getCurrentUserId());
}
@Override
public Week getWeek(String date) {
return daoSession.getWeekDao().queryBuilder().where(
WeekDao.Properties.StartDayDate.eq(date),
WeekDao.Properties.DiaryId.eq(getCurrentDiaryId())
).unique();
}
public List<Subject> getSubjectList() {
return daoSession.getSemesterDao().load(getCurrentSemesterId()).getSubjectList();
}
@Override
public List<Grade> getNewGrades() {
return daoSession.getGradeDao().queryBuilder().where(
GradeDao.Properties.IsNew.eq(1),
GradeDao.Properties.SemesterId.eq(getCurrentSemesterId())
).list();
}
@Override
public long getCurrentSymbolId() {
return daoSession.getSymbolDao().queryBuilder().where(
SymbolDao.Properties.UserId.eq(getCurrentUserId())
).unique().getId();
}
@Override
public long getCurrentStudentId() {
return daoSession.getStudentDao().queryBuilder().where(
StudentDao.Properties.SymbolId.eq(getCurrentSymbolId()),
StudentDao.Properties.Current.eq(true)
).unique().getId();
}
@Override
public long getCurrentDiaryId() {
return daoSession.getDiaryDao().queryBuilder().where(
DiaryDao.Properties.StudentId.eq(getCurrentStudentId()),
DiaryDao.Properties.Current.eq(true)
).unique().getId();
}
@Override
public long getCurrentSemesterId() {
return daoSession.getSemesterDao().queryBuilder().where(
SemesterDao.Properties.DiaryId.eq(getCurrentDiaryId()),
SemesterDao.Properties.Current.eq(true)
).unique().getId();
} }
} }

View File

@ -1,67 +1,20 @@
package io.github.wulkanowy.data; package io.github.wulkanowy.data;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import javax.inject.Singleton; import javax.inject.Singleton;
import io.github.wulkanowy.api.VulcanException; import io.github.wulkanowy.data.db.dao.DbContract;
import io.github.wulkanowy.data.db.dao.entities.Account;
import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.data.db.dao.entities.Subject;
import io.github.wulkanowy.data.db.dao.entities.Week;
import io.github.wulkanowy.data.db.resources.ResourcesContract; import io.github.wulkanowy.data.db.resources.ResourcesContract;
import io.github.wulkanowy.data.sync.account.AccountSyncContract; import io.github.wulkanowy.data.db.shared.SharedPrefContract;
import io.github.wulkanowy.data.sync.SyncContract;
@Singleton @Singleton
public interface RepositoryContract extends ResourcesContract, AccountSyncContract { public interface RepositoryContract {
long getCurrentUserId(); SharedPrefContract getSharedRepo();
int getStartupTab(); ResourcesContract getResRepo();
void setTimetableWidgetState(boolean nextDay); DbContract getDbRepo();
boolean getTimetableWidgetState(); SyncContract getSyncRepo();
boolean isServicesEnable();
boolean isNotifyEnable();
boolean isShowGradesSummary();
int getServicesInterval();
boolean isMobileDisable();
void syncGrades() throws VulcanException, IOException, ParseException;
void syncSubjects() throws VulcanException, IOException, ParseException;
void syncAttendance() throws ParseException, IOException, VulcanException;
void syncAttendance(String date) throws ParseException, IOException, VulcanException;
void syncTimetable() throws VulcanException, IOException, ParseException;
void syncTimetable(String date) throws VulcanException, IOException, ParseException;
void syncAll() throws VulcanException, IOException, ParseException;
Account getCurrentUser();
Week getWeek(String date);
List<Subject> getSubjectList();
List<Grade> getNewGrades();
long getCurrentStudentId();
long getCurrentSymbolId();
long getCurrentDiaryId();
long getCurrentSemesterId();
} }

View File

@ -0,0 +1,24 @@
package io.github.wulkanowy.data.db.dao;
import java.util.List;
import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.data.db.dao.entities.Subject;
import io.github.wulkanowy.data.db.dao.entities.Week;
public interface DbContract {
Week getWeek(String date);
List<Subject> getSubjectList();
List<Grade> getNewGrades();
long getCurrentStudentId();
long getCurrentSymbolId();
long getCurrentDiaryId();
long getCurrentSemesterId();
}

View File

@ -0,0 +1,82 @@
package io.github.wulkanowy.data.db.dao;
import java.util.List;
import javax.inject.Inject;
import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.dao.entities.DiaryDao;
import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.data.db.dao.entities.GradeDao;
import io.github.wulkanowy.data.db.dao.entities.SemesterDao;
import io.github.wulkanowy.data.db.dao.entities.StudentDao;
import io.github.wulkanowy.data.db.dao.entities.Subject;
import io.github.wulkanowy.data.db.dao.entities.SymbolDao;
import io.github.wulkanowy.data.db.dao.entities.Week;
import io.github.wulkanowy.data.db.dao.entities.WeekDao;
import io.github.wulkanowy.data.db.shared.SharedPrefContract;
public class DbRepository implements DbContract {
private final DaoSession daoSession;
private final SharedPrefContract sharedPref;
@Inject
DbRepository(DaoSession daoSession, SharedPrefContract sharedPrefContract) {
this.daoSession = daoSession;
this.sharedPref = sharedPrefContract;
}
@Override
public Week getWeek(String date) {
return daoSession.getWeekDao().queryBuilder().where(
WeekDao.Properties.StartDayDate.eq(date),
WeekDao.Properties.DiaryId.eq(getCurrentDiaryId())
).unique();
}
public List<Subject> getSubjectList() {
return daoSession.getSemesterDao().load(getCurrentSemesterId()).getSubjectList();
}
@Override
public List<Grade> getNewGrades() {
return daoSession.getGradeDao().queryBuilder().where(
GradeDao.Properties.IsNew.eq(1),
GradeDao.Properties.SemesterId.eq(getCurrentSemesterId())
).list();
}
@Override
public long getCurrentSymbolId() {
return daoSession.getSymbolDao().queryBuilder().where(
SymbolDao.Properties.UserId.eq(sharedPref.getCurrentUserId())
).unique().getId();
}
@Override
public long getCurrentStudentId() {
return daoSession.getStudentDao().queryBuilder().where(
StudentDao.Properties.SymbolId.eq(getCurrentSymbolId()),
StudentDao.Properties.Current.eq(true)
).unique().getId();
}
@Override
public long getCurrentDiaryId() {
return daoSession.getDiaryDao().queryBuilder().where(
DiaryDao.Properties.StudentId.eq(getCurrentStudentId()),
DiaryDao.Properties.Current.eq(true)
).unique().getId();
}
@Override
public long getCurrentSemesterId() {
return daoSession.getSemesterDao().queryBuilder().where(
SemesterDao.Properties.DiaryId.eq(getCurrentDiaryId()),
SemesterDao.Properties.Current.eq(true)
).unique().getId();
}
}

View File

@ -1,7 +1,10 @@
package io.github.wulkanowy.data.db.resources; package io.github.wulkanowy.data.db.resources;
import javax.inject.Singleton;
import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson; import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson;
@Singleton
public interface ResourcesContract { public interface ResourcesContract {
String[] getSymbolsKeysArray(); String[] getSymbolsKeysArray();

View File

@ -21,12 +21,12 @@ import io.github.wulkanowy.utils.LogUtils;
import io.github.wulkanowy.utils.security.CryptoException; import io.github.wulkanowy.utils.security.CryptoException;
@Singleton @Singleton
public class AppResources implements ResourcesContract { public class ResourcesRepository implements ResourcesContract {
private Resources resources; private Resources resources;
@Inject @Inject
AppResources(@ApplicationContext Context context) { ResourcesRepository(@ApplicationContext Context context) {
resources = context.getResources(); resources = context.getResources();
} }

View File

@ -1,9 +1,14 @@
package io.github.wulkanowy.data.db.shared; package io.github.wulkanowy.data.db.shared;
import javax.inject.Singleton;
@Singleton
public interface SharedPrefContract { public interface SharedPrefContract {
long getCurrentUserId(); long getCurrentUserId();
boolean isUserLoggedIn();
void setCurrentUserId(long userId); void setCurrentUserId(long userId);
void setTimetableWidgetState(boolean nextDay); void setTimetableWidgetState(boolean nextDay);

View File

@ -13,7 +13,7 @@ import io.github.wulkanowy.di.annotations.SharedPreferencesInfo;
import io.github.wulkanowy.ui.main.settings.SettingsFragment; import io.github.wulkanowy.ui.main.settings.SettingsFragment;
@Singleton @Singleton
public class SharedPref implements SharedPrefContract { public class SharedPrefRepository implements SharedPrefContract {
private static final String SHARED_KEY_USER_ID = "USER_ID"; private static final String SHARED_KEY_USER_ID = "USER_ID";
@ -24,7 +24,7 @@ public class SharedPref implements SharedPrefContract {
private final SharedPreferences settingsSharedPref; private final SharedPreferences settingsSharedPref;
@Inject @Inject
SharedPref(@ApplicationContext Context context, @SharedPreferencesInfo String sharedName) { SharedPrefRepository(@ApplicationContext Context context, @SharedPreferencesInfo String sharedName) {
appSharedPref = context.getSharedPreferences(sharedName, Context.MODE_PRIVATE); appSharedPref = context.getSharedPreferences(sharedName, Context.MODE_PRIVATE);
settingsSharedPref = PreferenceManager.getDefaultSharedPreferences(context); settingsSharedPref = PreferenceManager.getDefaultSharedPreferences(context);
} }
@ -34,6 +34,11 @@ public class SharedPref implements SharedPrefContract {
return appSharedPref.getLong(SHARED_KEY_USER_ID, 0); return appSharedPref.getLong(SHARED_KEY_USER_ID, 0);
} }
@Override
public boolean isUserLoggedIn() {
return getCurrentUserId() != 0;
}
@Override @Override
public void setCurrentUserId(long userId) { public void setCurrentUserId(long userId) {
appSharedPref.edit().putLong(SHARED_KEY_USER_ID, userId).apply(); appSharedPref.edit().putLong(SHARED_KEY_USER_ID, userId).apply();

View File

@ -1,4 +1,4 @@
package io.github.wulkanowy.data.sync.account; package io.github.wulkanowy.data.sync;
import android.content.Context; import android.content.Context;
@ -27,7 +27,7 @@ import io.github.wulkanowy.utils.security.CryptoException;
import io.github.wulkanowy.utils.security.Scrambler; import io.github.wulkanowy.utils.security.Scrambler;
@Singleton @Singleton
public class AccountSync implements AccountSyncContract { public class AccountSync {
private final DaoSession daoSession; private final DaoSession daoSession;
@ -46,7 +46,6 @@ public class AccountSync implements AccountSyncContract {
this.context = context; this.context = context;
} }
@Override
public void registerUser(String email, String password, String symbol) public void registerUser(String email, String password, String symbol)
throws VulcanException, IOException, CryptoException { throws VulcanException, IOException, CryptoException {
@ -119,7 +118,6 @@ public class AccountSync implements AccountSyncContract {
daoSession.getSemesterDao().insertInTx(semesterList); daoSession.getSemesterDao().insertInTx(semesterList);
} }
@Override
public void initLastUser() throws IOException, CryptoException { public void initLastUser() throws IOException, CryptoException {
long userId = sharedPref.getCurrentUserId(); long userId = sharedPref.getCurrentUserId();
@ -131,26 +129,27 @@ public class AccountSync implements AccountSyncContract {
LogUtils.debug("Initialization current user id=" + userId); LogUtils.debug("Initialization current user id=" + userId);
Account account = daoSession.getAccountDao().load(userId); Account account = daoSession.getAccountDao().load(userId);
String email = account.getEmail();
String pass = Scrambler.decrypt(account.getEmail(), account.getPassword());
Symbol symbolE = daoSession.getSymbolDao().queryBuilder().where( Symbol symbol = daoSession.getSymbolDao().queryBuilder().where(
SymbolDao.Properties.UserId.eq(account.getId())).unique(); SymbolDao.Properties.UserId.eq(account.getId())).unique();
String symbol = symbolE.getSymbol();
String schoolId = symbolE.getSchoolId();
Student studentE = daoSession.getStudentDao().queryBuilder().where( Student student = daoSession.getStudentDao().queryBuilder().where(
StudentDao.Properties.SymbolId.eq(symbolE.getId()), StudentDao.Properties.SymbolId.eq(symbol.getId()),
StudentDao.Properties.Current.eq(true) StudentDao.Properties.Current.eq(true)
).unique(); ).unique();
String studentId = studentE.getRealId();
Diary diaryE = daoSession.getDiaryDao().queryBuilder().where( Diary diary = daoSession.getDiaryDao().queryBuilder().where(
DiaryDao.Properties.StudentId.eq(studentE.getId()), DiaryDao.Properties.StudentId.eq(student.getId()),
DiaryDao.Properties.Current.eq(true) DiaryDao.Properties.Current.eq(true)
).unique(); ).unique();
String diaryId = diaryE.getValue();
vulcan.setCredentials(email, pass, symbol, schoolId, studentId, diaryId); vulcan.setCredentials(
account.getEmail(),
Scrambler.decrypt(account.getEmail(), account.getPassword()),
symbol.getSymbol(),
symbol.getSchoolId(),
student.getRealId(),
diary.getValue()
);
} }
} }

View File

@ -1,4 +1,4 @@
package io.github.wulkanowy.data.sync.attendance; package io.github.wulkanowy.data.sync;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
@ -23,7 +23,7 @@ import io.github.wulkanowy.utils.LogUtils;
import io.github.wulkanowy.utils.TimeUtils; import io.github.wulkanowy.utils.TimeUtils;
@Singleton @Singleton
public class AttendanceSync implements AttendanceSyncContract { public class AttendanceSync {
private final DaoSession daoSession; private final DaoSession daoSession;
@ -37,12 +37,6 @@ public class AttendanceSync implements AttendanceSyncContract {
this.vulcan = vulcan; this.vulcan = vulcan;
} }
@Override
public void syncAttendance(long diaryId) throws IOException, ParseException, VulcanException {
syncAttendance(diaryId, null);
}
@Override
public void syncAttendance(long diaryId, String date) throws IOException, ParseException, VulcanException { public void syncAttendance(long diaryId, String date) throws IOException, ParseException, VulcanException {
this.diaryId = diaryId; this.diaryId = diaryId;

View File

@ -1,4 +1,4 @@
package io.github.wulkanowy.data.sync.grades; package io.github.wulkanowy.data.sync;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
@ -14,13 +14,12 @@ import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.dao.entities.Grade; import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.data.db.dao.entities.Semester; import io.github.wulkanowy.data.db.dao.entities.Semester;
import io.github.wulkanowy.data.db.dao.entities.SubjectDao; import io.github.wulkanowy.data.db.dao.entities.SubjectDao;
import io.github.wulkanowy.data.sync.SyncContract;
import io.github.wulkanowy.utils.DataObjectConverter; import io.github.wulkanowy.utils.DataObjectConverter;
import io.github.wulkanowy.utils.EntitiesCompare; import io.github.wulkanowy.utils.EntitiesCompare;
import io.github.wulkanowy.utils.LogUtils; import io.github.wulkanowy.utils.LogUtils;
@Singleton @Singleton
public class GradeSync implements SyncContract { public class GradeSync {
private final DaoSession daoSession; private final DaoSession daoSession;
@ -34,7 +33,6 @@ public class GradeSync implements SyncContract {
this.vulcan = vulcan; this.vulcan = vulcan;
} }
@Override
public void sync(long semesterId) throws IOException, VulcanException, ParseException { public void sync(long semesterId) throws IOException, VulcanException, ParseException {
this.semesterId = semesterId; this.semesterId = semesterId;

View File

@ -1,4 +1,4 @@
package io.github.wulkanowy.data.sync.subjects; package io.github.wulkanowy.data.sync;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -12,12 +12,11 @@ import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.data.db.dao.entities.DaoSession; import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.dao.entities.Semester; import io.github.wulkanowy.data.db.dao.entities.Semester;
import io.github.wulkanowy.data.db.dao.entities.Subject; import io.github.wulkanowy.data.db.dao.entities.Subject;
import io.github.wulkanowy.data.sync.SyncContract;
import io.github.wulkanowy.utils.DataObjectConverter; import io.github.wulkanowy.utils.DataObjectConverter;
import io.github.wulkanowy.utils.LogUtils; import io.github.wulkanowy.utils.LogUtils;
@Singleton @Singleton
public class SubjectSync implements SyncContract { public class SubjectSync {
private final DaoSession daoSession; private final DaoSession daoSession;
@ -31,7 +30,6 @@ public class SubjectSync implements SyncContract {
this.vulcan = vulcan; this.vulcan = vulcan;
} }
@Override
public void sync(long semesterId) throws VulcanException, IOException { public void sync(long semesterId) throws VulcanException, IOException {
this.semesterId = semesterId; this.semesterId = semesterId;

View File

@ -3,9 +3,34 @@ package io.github.wulkanowy.data.sync;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import io.github.wulkanowy.api.VulcanException; import javax.inject.Singleton;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.utils.security.CryptoException;
@Singleton
public interface SyncContract { public interface SyncContract {
void sync(long semesterId) throws VulcanException, IOException, ParseException; void registerUser(String email, String password, String symbol) throws VulcanException,
IOException, CryptoException;
void initLastUser() throws IOException, CryptoException;
void syncGrades(long semesterId) throws VulcanException, IOException, ParseException;
void syncGrades() throws VulcanException, IOException, ParseException;
void syncSubjects(long semesterId) throws VulcanException, IOException;
void syncSubjects() throws VulcanException, IOException;
void syncAttendance() throws ParseException, IOException, VulcanException;
void syncAttendance(long diaryId, String date) throws ParseException, IOException, VulcanException;
void syncTimetable() throws VulcanException, IOException, ParseException;
void syncTimetable(long diaryId, String date) throws VulcanException, IOException, ParseException;
void syncAll() throws VulcanException, IOException, ParseException;
} }

View File

@ -0,0 +1,105 @@
package io.github.wulkanowy.data.sync;
import java.io.IOException;
import java.text.ParseException;
import javax.inject.Inject;
import javax.inject.Singleton;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.data.db.dao.DbContract;
import io.github.wulkanowy.utils.security.CryptoException;
@Singleton
public class SyncRepository implements SyncContract {
private final GradeSync gradeSync;
private final SubjectSync subjectSync;
private final AttendanceSync attendanceSync;
private final TimetableSync timetableSync;
private final AccountSync accountSync;
private final DbContract database;
@Inject
SyncRepository(GradeSync gradeSync, SubjectSync subjectSync, AttendanceSync attendanceSync,
TimetableSync timetableSync, AccountSync accountSync, DbContract database) {
this.gradeSync = gradeSync;
this.subjectSync = subjectSync;
this.attendanceSync = attendanceSync;
this.timetableSync = timetableSync;
this.accountSync = accountSync;
this.database = database;
}
@Override
public void registerUser(String email, String password, String symbol) throws VulcanException,
IOException, CryptoException {
accountSync.registerUser(email, password, symbol);
}
@Override
public void initLastUser() throws IOException, CryptoException {
accountSync.initLastUser();
}
@Override
public void syncGrades(long semesterId) throws VulcanException, IOException, ParseException {
gradeSync.sync(semesterId);
}
@Override
public void syncGrades() throws VulcanException, IOException, ParseException {
gradeSync.sync(database.getCurrentSemesterId());
}
@Override
public void syncSubjects(long semesterId) throws VulcanException, IOException {
subjectSync.sync(semesterId);
}
@Override
public void syncSubjects() throws VulcanException, IOException {
subjectSync.sync(database.getCurrentSemesterId());
}
@Override
public void syncAttendance() throws ParseException, IOException, VulcanException {
attendanceSync.syncAttendance(database.getCurrentDiaryId(), null);
}
@Override
public void syncAttendance(long diaryId, String date) throws ParseException, IOException, VulcanException {
if (diaryId != 0) {
attendanceSync.syncAttendance(diaryId, date);
} else {
attendanceSync.syncAttendance(database.getCurrentDiaryId(), date);
}
}
@Override
public void syncTimetable() throws VulcanException, IOException, ParseException {
timetableSync.syncTimetable(database.getCurrentDiaryId(), null);
}
@Override
public void syncTimetable(long diaryId, String date) throws VulcanException, IOException, ParseException {
if (diaryId != 0) {
timetableSync.syncTimetable(diaryId, date);
} else {
timetableSync.syncTimetable(database.getCurrentDiaryId(), date);
}
}
@Override
public void syncAll() throws VulcanException, IOException, ParseException {
syncSubjects();
syncGrades();
syncAttendance();
syncTimetable();
}
}

View File

@ -1,4 +1,4 @@
package io.github.wulkanowy.data.sync.timetable; package io.github.wulkanowy.data.sync;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
@ -23,7 +23,7 @@ import io.github.wulkanowy.utils.LogUtils;
import io.github.wulkanowy.utils.TimeUtils; import io.github.wulkanowy.utils.TimeUtils;
@Singleton @Singleton
public class TimetableSync implements TimetableSyncContract { public class TimetableSync {
private final DaoSession daoSession; private final DaoSession daoSession;
@ -37,12 +37,6 @@ public class TimetableSync implements TimetableSyncContract {
this.vulcan = vulcan; this.vulcan = vulcan;
} }
@Override
public void syncTimetable(long diaryId) throws IOException, ParseException, VulcanException {
syncTimetable(diaryId, null);
}
@Override
public void syncTimetable(long diaryId, String date) throws IOException, ParseException, VulcanException { public void syncTimetable(long diaryId, String date) throws IOException, ParseException, VulcanException {
this.diaryId = diaryId; this.diaryId = diaryId;

View File

@ -1,16 +0,0 @@
package io.github.wulkanowy.data.sync.account;
import java.io.IOException;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.utils.security.CryptoException;
public interface AccountSyncContract {
void registerUser(String email, String password, String symbol)
throws VulcanException, IOException,
CryptoException;
void initLastUser() throws VulcanException, IOException,
CryptoException;
}

View File

@ -1,13 +0,0 @@
package io.github.wulkanowy.data.sync.attendance;
import java.io.IOException;
import java.text.ParseException;
import io.github.wulkanowy.api.VulcanException;
public interface AttendanceSyncContract {
void syncAttendance(long diaryId, String date) throws IOException, ParseException, VulcanException;
void syncAttendance(long diaryId) throws IOException, ParseException, VulcanException;
}

View File

@ -1,13 +0,0 @@
package io.github.wulkanowy.data.sync.timetable;
import java.io.IOException;
import java.text.ParseException;
import io.github.wulkanowy.api.VulcanException;
public interface TimetableSyncContract {
void syncTimetable(long diaryId, String date) throws VulcanException, IOException, ParseException;
void syncTimetable(long diaryId) throws VulcanException, IOException, ParseException;
}

View File

@ -1,11 +0,0 @@
package io.github.wulkanowy.di.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.inject.Qualifier;
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface SyncGrades {
}

View File

@ -1,11 +0,0 @@
package io.github.wulkanowy.di.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.inject.Qualifier;
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface SyncSubjects {
}

View File

@ -13,27 +13,20 @@ import dagger.Provides;
import io.github.wulkanowy.api.Vulcan; import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.data.Repository; import io.github.wulkanowy.data.Repository;
import io.github.wulkanowy.data.RepositoryContract; import io.github.wulkanowy.data.RepositoryContract;
import io.github.wulkanowy.data.db.dao.DbContract;
import io.github.wulkanowy.data.db.dao.DbHelper; import io.github.wulkanowy.data.db.dao.DbHelper;
import io.github.wulkanowy.data.db.dao.DbRepository;
import io.github.wulkanowy.data.db.dao.entities.DaoMaster; import io.github.wulkanowy.data.db.dao.entities.DaoMaster;
import io.github.wulkanowy.data.db.dao.entities.DaoSession; import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.resources.AppResources;
import io.github.wulkanowy.data.db.resources.ResourcesContract; import io.github.wulkanowy.data.db.resources.ResourcesContract;
import io.github.wulkanowy.data.db.shared.SharedPref; import io.github.wulkanowy.data.db.resources.ResourcesRepository;
import io.github.wulkanowy.data.db.shared.SharedPrefContract; import io.github.wulkanowy.data.db.shared.SharedPrefContract;
import io.github.wulkanowy.data.db.shared.SharedPrefRepository;
import io.github.wulkanowy.data.sync.SyncContract; import io.github.wulkanowy.data.sync.SyncContract;
import io.github.wulkanowy.data.sync.account.AccountSync; import io.github.wulkanowy.data.sync.SyncRepository;
import io.github.wulkanowy.data.sync.account.AccountSyncContract;
import io.github.wulkanowy.data.sync.attendance.AttendanceSync;
import io.github.wulkanowy.data.sync.attendance.AttendanceSyncContract;
import io.github.wulkanowy.data.sync.grades.GradeSync;
import io.github.wulkanowy.data.sync.subjects.SubjectSync;
import io.github.wulkanowy.data.sync.timetable.TimetableSync;
import io.github.wulkanowy.data.sync.timetable.TimetableSyncContract;
import io.github.wulkanowy.di.annotations.ApplicationContext; import io.github.wulkanowy.di.annotations.ApplicationContext;
import io.github.wulkanowy.di.annotations.DatabaseInfo; import io.github.wulkanowy.di.annotations.DatabaseInfo;
import io.github.wulkanowy.di.annotations.SharedPreferencesInfo; import io.github.wulkanowy.di.annotations.SharedPreferencesInfo;
import io.github.wulkanowy.di.annotations.SyncGrades;
import io.github.wulkanowy.di.annotations.SyncSubjects;
import io.github.wulkanowy.utils.AppConstant; import io.github.wulkanowy.utils.AppConstant;
@Module @Module
@ -88,46 +81,27 @@ public class ApplicationModule {
@Singleton @Singleton
@Provides @Provides
SharedPrefContract provideSharedPref(SharedPref sharedPref) { SharedPrefContract provideSharedPref(SharedPrefRepository sharedPrefRepository) {
return sharedPref; return sharedPrefRepository;
} }
@Singleton @Singleton
@Provides @Provides
ResourcesContract provideAppResources(AppResources appResources) { ResourcesContract provideAppResources(ResourcesRepository resourcesRepository) {
return appResources; return resourcesRepository;
}
@Singleton
@Provides
DbContract provideDatabase(DbRepository dbRepository) {
return dbRepository;
} }
@Singleton @Singleton
@Provides @Provides
AccountSyncContract provideLoginSync(AccountSync accountSync) { SyncContract provideSync(SyncRepository syncRepository) {
return accountSync; return syncRepository;
}
@SyncGrades
@Singleton
@Provides
SyncContract provideGradesSync(GradeSync gradeSync) {
return gradeSync;
}
@SyncSubjects
@Singleton
@Provides
SyncContract provideSubjectSync(SubjectSync subjectSync) {
return subjectSync;
}
@Singleton
@Provides
TimetableSyncContract provideTimetableSync(TimetableSync timetableSync) {
return timetableSync;
}
@Singleton
@Provides
AttendanceSyncContract provideAttendanceSync(AttendanceSync attendanceSync) {
return attendanceSync;
} }
@Provides @Provides

View File

@ -65,12 +65,12 @@ public class SyncJob extends SimpleJobService {
@Override @Override
public int onRunJob(JobParameters job) { public int onRunJob(JobParameters job) {
try { try {
repository.initLastUser(); repository.getSyncRepo().initLastUser();
repository.syncAll(); repository.getSyncRepo().syncAll();
gradeList = repository.getNewGrades(); gradeList = repository.getDbRepo().getNewGrades();
if (!gradeList.isEmpty() && repository.isNotifyEnable()) { if (!gradeList.isEmpty() && repository.getSharedRepo().isNotifyEnable()) {
showNotification(); showNotification();
} }
return JobService.RESULT_SUCCESS; return JobService.RESULT_SUCCESS;

View File

@ -64,10 +64,10 @@ public class LoginPresenter extends BasePresenter<LoginContract.View>
public void onDoInBackground(int stepNumber) throws Exception { public void onDoInBackground(int stepNumber) throws Exception {
switch (stepNumber) { switch (stepNumber) {
case 1: case 1:
getRepository().registerUser(email, password, symbol); getRepository().getSyncRepo().registerUser(email, password, symbol);
break; break;
case 2: case 2:
getRepository().syncAll(); getRepository().getSyncRepo().syncAll();
break; break;
} }
} }
@ -93,7 +93,7 @@ public class LoginPresenter extends BasePresenter<LoginContract.View>
getView().setErrorSymbolRequired(); getView().setErrorSymbolRequired();
getView().showSoftInput(); getView().showSoftInput();
} else { } else {
getView().onError(getRepository().getErrorLoginMessage(exception)); getView().onError(getRepository().getResRepo().getErrorLoginMessage(exception));
} }
getView().showActionBar(true); getView().showActionBar(true);
@ -121,8 +121,8 @@ public class LoginPresenter extends BasePresenter<LoginContract.View>
return AppConstant.DEFAULT_SYMBOL; return AppConstant.DEFAULT_SYMBOL;
} }
String[] keys = getRepository().getSymbolsKeysArray(); String[] keys = getRepository().getResRepo().getSymbolsKeysArray();
String[] values = getRepository().getSymbolsValuesArray(); String[] values = getRepository().getResRepo().getSymbolsValuesArray();
LinkedHashMap<String, String> map = new LinkedHashMap<>(); LinkedHashMap<String, String> map = new LinkedHashMap<>();
for (int i = 0; i < Math.min(keys.length, values.length); ++i) { for (int i = 0; i < Math.min(keys.length, values.length); ++i) {

View File

@ -27,7 +27,7 @@ public class MainPresenter extends BasePresenter<MainContract.View>
if (tabPositionIntent != -1) { if (tabPositionIntent != -1) {
tabPosition = tabPositionIntent; tabPosition = tabPositionIntent;
} else { } else {
tabPosition = getRepository().getStartupTab(); tabPosition = getRepository().getSharedRepo().getStartupTab();
} }
getView().initiationBottomNav(tabPosition); getView().initiationBottomNav(tabPosition);

View File

@ -85,18 +85,18 @@ public class AttendanceTabPresenter extends BasePresenter<AttendanceTabContract.
getView().onRefreshSuccess(); getView().onRefreshSuccess();
} else { } else {
getView().onError(getRepository().getErrorLoginMessage(exception)); getView().onError(getRepository().getResRepo().getErrorLoginMessage(exception));
} }
getView().hideRefreshingBar(); getView().hideRefreshingBar();
} }
@Override @Override
public void onDoInBackgroundLoading() throws Exception { public void onDoInBackgroundLoading() throws Exception {
Week week = getRepository().getWeek(date); Week week = getRepository().getDbRepo().getWeek(date);
if (week == null || !week.getAttendanceSynced()) { if (week == null || !week.getAttendanceSynced()) {
syncData(); syncData();
week = getRepository().getWeek(date); week = getRepository().getDbRepo().getWeek(date);
} }
List<Day> dayList = week.getDayList(); List<Day> dayList = week.getDayList();
@ -118,7 +118,7 @@ public class AttendanceTabPresenter extends BasePresenter<AttendanceTabContract.
List<AttendanceSubItem> subItems = new ArrayList<>(); List<AttendanceSubItem> subItems = new ArrayList<>();
for (AttendanceLesson lesson : lessonList) { for (AttendanceLesson lesson : lessonList) {
lesson.setDescription(getRepository().getAttendanceLessonDescription(lesson)); lesson.setDescription(getRepository().getResRepo().getAttendanceLessonDescription(lesson));
subItems.add(new AttendanceSubItem(headerItem, lesson)); subItems.add(new AttendanceSubItem(headerItem, lesson));
} }
@ -155,7 +155,7 @@ public class AttendanceTabPresenter extends BasePresenter<AttendanceTabContract.
} }
private void syncData() throws Exception { private void syncData() throws Exception {
getRepository().syncAttendance(date); getRepository().getSyncRepo().syncAttendance(0, date);
} }
private void cancelAsyncTasks() { private void cancelAsyncTasks() {

View File

@ -71,8 +71,8 @@ public class GradesPresenter extends BasePresenter<GradesContract.View>
@Override @Override
public void onDoInBackgroundRefresh() throws Exception { public void onDoInBackgroundRefresh() throws Exception {
getRepository().syncSubjects(); getRepository().getSyncRepo().syncSubjects();
getRepository().syncGrades(); getRepository().getSyncRepo().syncGrades();
} }
@Override @Override
@ -89,7 +89,7 @@ public class GradesPresenter extends BasePresenter<GradesContract.View>
loadingTask.setOnFirstLoadingListener(this); loadingTask.setOnFirstLoadingListener(this);
loadingTask.execute(); loadingTask.execute();
int numberOfNewGrades = getRepository().getNewGrades().size(); int numberOfNewGrades = getRepository().getDbRepo().getNewGrades().size();
if (numberOfNewGrades <= 0) { if (numberOfNewGrades <= 0) {
getView().onRefreshSuccessNoGrade(); getView().onRefreshSuccessNoGrade();
@ -97,15 +97,15 @@ public class GradesPresenter extends BasePresenter<GradesContract.View>
getView().onRefreshSuccess(numberOfNewGrades); getView().onRefreshSuccess(numberOfNewGrades);
} }
} else { } else {
getView().onError(getRepository().getErrorLoginMessage(exception)); getView().onError(getRepository().getResRepo().getErrorLoginMessage(exception));
} }
getView().hideRefreshingBar(); getView().hideRefreshingBar();
} }
@Override @Override
public void onDoInBackgroundLoading() { public void onDoInBackgroundLoading() {
List<Subject> subjectList = getRepository().getSubjectList(); List<Subject> subjectList = getRepository().getDbRepo().getSubjectList();
boolean isShowSummary = getRepository().isShowGradesSummary(); boolean isShowSummary = getRepository().getSharedRepo().isShowGradesSummary();
headerItems = new ArrayList<>(); headerItems = new ArrayList<>();

View File

@ -88,18 +88,18 @@ public class TimetableTabPresenter extends BasePresenter<TimetableTabContract.Vi
getView().onRefreshSuccess(); getView().onRefreshSuccess();
} else { } else {
getView().onError(getRepository().getErrorLoginMessage(exception)); getView().onError(getRepository().getResRepo().getErrorLoginMessage(exception));
} }
getView().hideRefreshingBar(); getView().hideRefreshingBar();
} }
@Override @Override
public void onDoInBackgroundLoading() throws Exception { public void onDoInBackgroundLoading() throws Exception {
Week week = getRepository().getWeek(date); Week week = getRepository().getDbRepo().getWeek(date);
if (week == null || !week.getTimetableSynced()) { if (week == null || !week.getTimetableSynced()) {
syncData(); syncData();
week = getRepository().getWeek(date); week = getRepository().getDbRepo().getWeek(date);
} }
List<Day> dayList = week.getDayList(); List<Day> dayList = week.getDayList();
@ -159,7 +159,7 @@ public class TimetableTabPresenter extends BasePresenter<TimetableTabContract.Vi
} }
private void syncData() throws Exception { private void syncData() throws Exception {
getRepository().syncTimetable(date); getRepository().getSyncRepo().syncTimetable(0, date);
} }
private void cancelAsyncTasks() { private void cancelAsyncTasks() {

View File

@ -20,15 +20,15 @@ public class SplashPresenter extends BasePresenter<SplashContract.View>
super.onStart(activity); super.onStart(activity);
getView().cancelNotifications(); getView().cancelNotifications();
if (getRepository().isServicesEnable()) { if (getRepository().getSharedRepo().isServicesEnable()) {
getView().startSyncService(getRepository().getServicesInterval(), getView().startSyncService(getRepository().getSharedRepo().getServicesInterval(),
getRepository().isMobileDisable()); getRepository().getSharedRepo().isMobileDisable());
} }
if (getRepository().getCurrentUserId() == 0) { if (getRepository().getSharedRepo().isUserLoggedIn()) {
getView().openLoginActivity();
} else {
getView().openMainActivity(); getView().openMainActivity();
} else {
getView().openLoginActivity();
} }
} }
} }

View File

@ -50,10 +50,10 @@ public class TimetableWidgetFactory implements RemoteViewsService.RemoteViewsFac
inject(); inject();
lessonList = new ArrayList<>(); lessonList = new ArrayList<>();
if (repository.getCurrentUserId() != 0) { if (repository.getSharedRepo().isUserLoggedIn()) {
Week week = repository.getWeek(TimeUtils.getDateOfCurrentMonday(true)); Week week = repository.getDbRepo().getWeek(TimeUtils.getDateOfCurrentMonday(true));
int valueOfDay = TimeUtils.getTodayOrNextDayValue(repository.getTimetableWidgetState()); int valueOfDay = TimeUtils.getTodayOrNextDayValue(repository.getSharedRepo().getTimetableWidgetState());
if (valueOfDay != 5 && valueOfDay != 6 && week != null) { if (valueOfDay != 5 && valueOfDay != 6 && week != null) {
week.resetDayList(); week.resetDayList();

View File

@ -49,7 +49,7 @@ public class TimetableWidgetProvider extends AppWidgetProvider {
TimetableWidgetProvider.class.getName()); TimetableWidgetProvider.class.getName());
final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
repository.setTimetableWidgetState(!repository.getTimetableWidgetState()); repository.getSharedRepo().setTimetableWidgetState(!repository.getSharedRepo().getTimetableWidgetState());
onUpdate(context, appWidgetManager, appWidgetIds); onUpdate(context, appWidgetManager, appWidgetIds);
} }
} }
@ -80,7 +80,7 @@ public class TimetableWidgetProvider extends AppWidgetProvider {
views.setRemoteAdapter(appWidgetId, R.id.timetable_widget_list, intent); views.setRemoteAdapter(appWidgetId, R.id.timetable_widget_list, intent);
views.setEmptyView(R.id.timetable_widget_list, R.id.timetable_widget_empty); views.setEmptyView(R.id.timetable_widget_list, R.id.timetable_widget_empty);
boolean nextDay = repository.getTimetableWidgetState(); boolean nextDay = repository.getSharedRepo().getTimetableWidgetState();
String toggleText = context.getString(nextDay ? R.string.widget_timetable_tomorrow String toggleText = context.getString(nextDay ? R.string.widget_timetable_tomorrow
: R.string.widget_timetable_today); : R.string.widget_timetable_today);