1
0

Optimize session handling (#63)

* [APP] Change way the Vulcan is configured (#65)
This commit is contained in:
Mikołaj Pich
2018-03-11 19:16:20 +01:00
committed by Rafał Borcz
parent a0313827ce
commit 3aca34340d
57 changed files with 521 additions and 454 deletions

View File

@ -7,10 +7,7 @@ import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.login.VulcanOfflineException;
import io.github.wulkanowy.api.VulcanException;
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;
@ -21,8 +18,8 @@ import io.github.wulkanowy.data.db.dao.entities.WeekDao;
import io.github.wulkanowy.data.db.resources.ResourcesContract;
import io.github.wulkanowy.data.db.shared.SharedPrefContract;
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.login.LoginSyncContract;
import io.github.wulkanowy.data.sync.timetable.TimetableSyncContract;
import io.github.wulkanowy.di.annotations.SyncGrades;
import io.github.wulkanowy.di.annotations.SyncSubjects;
@ -37,7 +34,7 @@ public class Repository implements RepositoryContract {
private final DaoSession daoSession;
private final LoginSyncContract loginSync;
private final AccountSyncContract accountSync;
private final AttendanceSyncContract attendanceSync;
@ -51,7 +48,7 @@ public class Repository implements RepositoryContract {
Repository(SharedPrefContract sharedPref,
ResourcesContract resources,
DaoSession daoSession,
LoginSyncContract loginSync,
AccountSyncContract accountSync,
AttendanceSyncContract attendanceSync,
TimetableSyncContract timetableSync,
@SyncGrades SyncContract gradeSync,
@ -59,7 +56,7 @@ public class Repository implements RepositoryContract {
this.sharedPref = sharedPref;
this.resources = resources;
this.daoSession = daoSession;
this.loginSync = loginSync;
this.accountSync = accountSync;
this.attendanceSync = attendanceSync;
this.timetableSync = timetableSync;
this.gradeSync = gradeSync;
@ -92,50 +89,48 @@ public class Repository implements RepositoryContract {
}
@Override
public void loginUser(String email, String password, String symbol)
throws NotLoggedInErrorException, AccountPermissionException, IOException,
CryptoException, VulcanOfflineException, BadCredentialsException {
loginSync.loginUser(email, password, symbol);
public void registerUser(String email, String password, String symbol) throws VulcanException,
IOException, CryptoException {
accountSync.registerUser(email, password, symbol);
}
@Override
public void loginCurrentUser() throws NotLoggedInErrorException, AccountPermissionException,
IOException, CryptoException, VulcanOfflineException, BadCredentialsException {
loginSync.loginCurrentUser();
public void initLastUser() throws VulcanException, IOException, CryptoException {
accountSync.initLastUser();
}
@Override
public void syncGrades() throws NotLoggedInErrorException, IOException, ParseException {
public void syncGrades() throws VulcanException, IOException, ParseException {
gradeSync.sync();
}
@Override
public void syncSubjects() throws NotLoggedInErrorException, IOException, ParseException {
public void syncSubjects() throws VulcanException, IOException, ParseException {
subjectSync.sync();
}
@Override
public void syncAttendance() throws NotLoggedInErrorException, ParseException, IOException {
public void syncAttendance() throws ParseException, IOException, VulcanException {
attendanceSync.syncAttendance();
}
@Override
public void syncAttendance(String date) throws NotLoggedInErrorException, ParseException, IOException {
public void syncAttendance(String date) throws ParseException, IOException, VulcanException {
attendanceSync.syncAttendance(date);
}
@Override
public void syncTimetable() throws NotLoggedInErrorException, IOException, ParseException {
public void syncTimetable() throws VulcanException, IOException, ParseException {
timetableSync.syncTimetable();
}
@Override
public void syncTimetable(String date) throws NotLoggedInErrorException, IOException, ParseException {
public void syncTimetable(String date) throws VulcanException, IOException, ParseException {
timetableSync.syncTimetable(date);
}
@Override
public void syncAll() throws NotLoggedInErrorException, IOException, ParseException {
public void syncAll() throws VulcanException, IOException, ParseException {
syncSubjects();
syncGrades();
syncAttendance();

View File

@ -6,26 +6,26 @@ import java.util.List;
import javax.inject.Singleton;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.VulcanException;
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.Week;
import io.github.wulkanowy.data.db.resources.ResourcesContract;
import io.github.wulkanowy.data.sync.account.AccountSyncContract;
import io.github.wulkanowy.data.sync.attendance.AttendanceSyncContract;
import io.github.wulkanowy.data.sync.login.LoginSyncContract;
import io.github.wulkanowy.data.sync.timetable.TimetableSyncContract;
@Singleton
public interface RepositoryContract extends ResourcesContract, LoginSyncContract,
public interface RepositoryContract extends ResourcesContract, AccountSyncContract,
AttendanceSyncContract, TimetableSyncContract {
long getCurrentUserId();
void syncGrades() throws NotLoggedInErrorException, IOException, ParseException;
void syncGrades() throws VulcanException, IOException, ParseException;
void syncSubjects() throws NotLoggedInErrorException, IOException, ParseException;
void syncSubjects() throws VulcanException, IOException, ParseException;
void syncAll() throws NotLoggedInErrorException, IOException, ParseException;
void syncAll() throws VulcanException, IOException, ParseException;
Account getCurrentUser();

View File

@ -11,8 +11,8 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import io.github.wulkanowy.R;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.login.VulcanOfflineException;
import io.github.wulkanowy.api.NotLoggedInErrorException;
import io.github.wulkanowy.api.VulcanOfflineException;
import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson;
import io.github.wulkanowy.di.annotations.ApplicationContext;
import io.github.wulkanowy.utils.AppConstant;

View File

@ -3,9 +3,9 @@ package io.github.wulkanowy.data.sync;
import java.io.IOException;
import java.text.ParseException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.VulcanException;
public interface SyncContract {
void sync() throws NotLoggedInErrorException, IOException, ParseException;
void sync() throws VulcanException, IOException, ParseException;
}

View File

@ -1,4 +1,4 @@
package io.github.wulkanowy.data.sync.login;
package io.github.wulkanowy.data.sync.account;
import android.content.Context;
@ -8,10 +8,7 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.login.VulcanOfflineException;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.data.db.dao.entities.Account;
import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.shared.SharedPrefContract;
@ -21,7 +18,7 @@ import io.github.wulkanowy.utils.security.CryptoException;
import io.github.wulkanowy.utils.security.Scrambler;
@Singleton
public class LoginSync implements LoginSyncContract {
public class AccountSync implements AccountSyncContract {
private final DaoSession daoSession;
@ -32,8 +29,8 @@ public class LoginSync implements LoginSyncContract {
private final Context context;
@Inject
LoginSync(DaoSession daoSession, SharedPrefContract sharedPref,
Vulcan vulcan, @ApplicationContext Context context) {
AccountSync(DaoSession daoSession, SharedPrefContract sharedPref,
Vulcan vulcan, @ApplicationContext Context context) {
this.daoSession = daoSession;
this.sharedPref = sharedPref;
this.vulcan = vulcan;
@ -41,13 +38,12 @@ public class LoginSync implements LoginSyncContract {
}
@Override
public void loginUser(String email, String password, String symbol)
throws NotLoggedInErrorException, AccountPermissionException, IOException,
CryptoException, VulcanOfflineException, BadCredentialsException {
public void registerUser(String email, String password, String symbol)
throws VulcanException, IOException, CryptoException {
LogUtils.debug("Login new user email=" + email);
LogUtils.debug("Register new user email=" + email);
vulcan.login(email, password, symbol);
vulcan.setCredentials(email, password, symbol, null);
Account account = new Account()
.setName(vulcan.getBasicInformation().getPersonalData().getFirstAndLastName())
@ -56,24 +52,25 @@ public class LoginSync implements LoginSyncContract {
.setSymbol(vulcan.getSymbol())
.setSnpId(vulcan.getStudentAndParent().getId());
sharedPref.setCurrentUserId(daoSession.getAccountDao().insert(account));
daoSession.getAccountDao().insert(account);
sharedPref.setCurrentUserId(account.getId());
}
@Override
public void loginCurrentUser() throws NotLoggedInErrorException, AccountPermissionException,
IOException, CryptoException, VulcanOfflineException, BadCredentialsException {
public void initLastUser() throws VulcanException, IOException, CryptoException {
long userId = sharedPref.getCurrentUserId();
if (userId == 0) {
throw new IOException("Can't find logged user");
throw new IOException("Can't find saved user");
}
LogUtils.debug("Login current user id=" + userId);
LogUtils.debug("Initialization current user id=" + userId);
Account account = daoSession.getAccountDao().load(userId);
vulcan.login(account.getEmail(),
vulcan.setCredentials(account.getEmail(),
Scrambler.decrypt(account.getEmail(), account.getPassword()),
account.getSymbol(),
account.getSnpId());

View File

@ -0,0 +1,16 @@
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

@ -9,8 +9,8 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.api.generic.Lesson;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson;
import io.github.wulkanowy.data.db.dao.entities.AttendanceLessonDao;
import io.github.wulkanowy.data.db.dao.entities.DaoSession;
@ -42,12 +42,12 @@ public class AttendanceSync implements AttendanceSyncContract {
}
@Override
public void syncAttendance() throws IOException, NotLoggedInErrorException, ParseException {
public void syncAttendance() throws IOException, ParseException, VulcanException {
syncAttendance(null);
}
@Override
public void syncAttendance(String date) throws IOException, NotLoggedInErrorException, ParseException {
public void syncAttendance(String date) throws IOException, ParseException, VulcanException {
this.userId = sharedPref.getCurrentUserId();
io.github.wulkanowy.api.generic.Week<io.github.wulkanowy.api.generic.Day> weekApi = getWeekFromApi(getNormalizedDate(date));
@ -59,7 +59,7 @@ public class AttendanceSync implements AttendanceSyncContract {
daoSession.getAttendanceLessonDao().saveInTx(lessonList);
LogUtils.debug("Synchronization lessons (amount = " + lessonList.size() + ")");
LogUtils.debug("Synchronization attendance lessons (amount = " + lessonList.size() + ")");
}
private String getNormalizedDate(String date) throws ParseException {
@ -67,7 +67,7 @@ public class AttendanceSync implements AttendanceSyncContract {
}
private io.github.wulkanowy.api.generic.Week<io.github.wulkanowy.api.generic.Day> getWeekFromApi(String date)
throws IOException, NotLoggedInErrorException, ParseException {
throws IOException, ParseException, VulcanException {
return vulcan.getAttendanceTable().getWeekTable(date);
}

View File

@ -3,11 +3,11 @@ package io.github.wulkanowy.data.sync.attendance;
import java.io.IOException;
import java.text.ParseException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.VulcanException;
public interface AttendanceSyncContract {
void syncAttendance(String date) throws NotLoggedInErrorException, IOException, ParseException;
void syncAttendance(String date) throws IOException, ParseException, VulcanException;
void syncAttendance() throws NotLoggedInErrorException, IOException, ParseException;
void syncAttendance() throws IOException, ParseException, VulcanException;
}

View File

@ -9,7 +9,7 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.data.db.dao.entities.Account;
import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.dao.entities.Grade;
@ -29,6 +29,8 @@ public class GradeSync implements SyncContract {
private final SharedPrefContract sharedPref;
private Long userId;
@Inject
GradeSync(DaoSession daoSession, SharedPrefContract sharedPref, Vulcan vulcan) {
this.daoSession = daoSession;
@ -37,36 +39,53 @@ public class GradeSync implements SyncContract {
}
@Override
public void sync() throws IOException, NotLoggedInErrorException, ParseException {
public void sync() throws IOException, VulcanException, ParseException {
long userId = sharedPref.getCurrentUserId();
userId = sharedPref.getCurrentUserId();
Account account = daoSession.getAccountDao().load(userId);
account.resetGradeList();
account.resetSubjectList();
resetAccountRelations(account);
List<Grade> gradesFromNet = DataObjectConverter
.gradesToGradeEntities(vulcan.getGradesList().getAll());
List<Grade> gradesFromDb = account.getGradeList();
List<Grade> updatedGrades = EntitiesCompare.compareGradeList(gradesFromNet, gradesFromDb);
daoSession.getGradeDao().deleteInTx(gradesFromDb);
List<Grade> lastList = new ArrayList<>();
for (Grade grade : updatedGrades) {
grade.setUserId(userId);
grade.setSubjectId(daoSession.getSubjectDao().queryBuilder()
.where(SubjectDao.Properties.Name.eq(grade.getSubject()),
SubjectDao.Properties.UserId.eq(userId))
.build()
.uniqueOrThrow().getId());
lastList.add(grade);
}
List<Grade> lastList = getUpdatedList(getComparedList(account));
daoSession.getGradeDao().deleteInTx(account.getGradeList());
daoSession.getGradeDao().insertInTx(lastList);
LogUtils.debug("Synchronization grades (amount = " + lastList.size() + ")");
}
private void resetAccountRelations(Account account) {
account.resetSubjectList();
account.resetGradeList();
}
private List<Grade> getUpdatedList(List<Grade> comparedList) {
List<Grade> updatedList = new ArrayList<>();
for (Grade grade : comparedList) {
grade.setUserId(userId);
grade.setSubjectId(getSubjectId(grade.getSubject()));
updatedList.add(grade);
}
return updatedList;
}
private List<Grade> getComparedList(Account account) throws IOException, VulcanException,
ParseException {
List<Grade> gradesFromNet = DataObjectConverter
.gradesToGradeEntities(vulcan.getGradesList().getAll());
List<Grade> gradesFromDb = account.getGradeList();
return EntitiesCompare.compareGradeList(gradesFromNet, gradesFromDb);
}
private Long getSubjectId(String subjectName) {
return daoSession.getSubjectDao().queryBuilder()
.where(SubjectDao.Properties.Name.eq(subjectName),
SubjectDao.Properties.UserId.eq(userId))
.build()
.uniqueOrThrow()
.getId();
}
}

View File

@ -1,19 +0,0 @@
package io.github.wulkanowy.data.sync.login;
import java.io.IOException;
import io.github.wulkanowy.api.login.AccountPermissionException;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.login.VulcanOfflineException;
import io.github.wulkanowy.utils.security.CryptoException;
public interface LoginSyncContract {
void loginUser(String email, String password, String symbol)
throws NotLoggedInErrorException, AccountPermissionException, IOException,
CryptoException, VulcanOfflineException, BadCredentialsException;
void loginCurrentUser() throws NotLoggedInErrorException, AccountPermissionException, IOException,
CryptoException, VulcanOfflineException, BadCredentialsException;
}

View File

@ -9,10 +9,10 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.data.db.dao.entities.Account;
import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.dao.entities.Subject;
import io.github.wulkanowy.data.db.dao.entities.SubjectDao;
import io.github.wulkanowy.data.db.shared.SharedPrefContract;
import io.github.wulkanowy.data.sync.SyncContract;
import io.github.wulkanowy.utils.DataObjectConverter;
@ -21,41 +21,51 @@ import io.github.wulkanowy.utils.LogUtils;
@Singleton
public class SubjectSync implements SyncContract {
private final SubjectDao subjectDao;
private final DaoSession daoSession;
private final Vulcan vulcan;
private final SharedPrefContract sharedPref;
private Long userId;
@Inject
SubjectSync(DaoSession daoSession, SharedPrefContract sharedPref, Vulcan vulcan) {
this.subjectDao = daoSession.getSubjectDao();
this.daoSession = daoSession;
this.sharedPref = sharedPref;
this.vulcan = vulcan;
}
@Override
public void sync() throws NotLoggedInErrorException, IOException, ParseException {
public void sync() throws VulcanException, IOException, ParseException {
long userId = sharedPref.getCurrentUserId();
userId = sharedPref.getCurrentUserId();
List<Subject> subjectsFromNet = DataObjectConverter
.subjectsToSubjectEntities(vulcan.getSubjectsList().getAll());
List<Subject> lastList = getUpdatedList(getSubjectsFromNet());
subjectDao.deleteInTx(subjectDao.queryBuilder()
.where(SubjectDao.Properties.UserId.eq(userId))
.build()
.list());
List<Subject> lastList = new ArrayList<>();
for (Subject subject : subjectsFromNet) {
subject.setUserId(userId);
lastList.add(subject);
}
subjectDao.insertInTx(lastList);
daoSession.getSubjectDao().deleteInTx(getSubjectsFromDb());
daoSession.getSubjectDao().insertInTx(lastList);
LogUtils.debug("Synchronization subjects (amount = " + lastList.size() + ")");
}
private List<Subject> getSubjectsFromNet() throws VulcanException, IOException {
return DataObjectConverter.subjectsToSubjectEntities(vulcan.getSubjectsList().getAll());
}
private List<Subject> getSubjectsFromDb() {
Account account = daoSession.getAccountDao().load(userId);
account.resetSubjectList();
return account.getSubjectList();
}
private List<Subject> getUpdatedList(List<Subject> subjectsFromNet) {
List<Subject> updatedList = new ArrayList<>();
for (Subject subject : subjectsFromNet) {
subject.setUserId(userId);
updatedList.add(subject);
}
return updatedList;
}
}

View File

@ -9,8 +9,8 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import io.github.wulkanowy.api.Vulcan;
import io.github.wulkanowy.api.VulcanException;
import io.github.wulkanowy.api.generic.Lesson;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.data.db.dao.entities.DaoSession;
import io.github.wulkanowy.data.db.dao.entities.Day;
import io.github.wulkanowy.data.db.dao.entities.DayDao;
@ -42,12 +42,12 @@ public class TimetableSync implements TimetableSyncContract {
}
@Override
public void syncTimetable() throws NotLoggedInErrorException, IOException, ParseException {
public void syncTimetable() throws IOException, ParseException, VulcanException {
syncTimetable(null);
}
@Override
public void syncTimetable(String date) throws NotLoggedInErrorException, IOException, ParseException {
public void syncTimetable(String date) throws IOException, ParseException, VulcanException {
this.userId = sharedPref.getCurrentUserId();
io.github.wulkanowy.api.generic.Week<io.github.wulkanowy.api.generic.Day> weekApi = getWeekFromApi(getNormalizedDate(date));
@ -59,7 +59,7 @@ public class TimetableSync implements TimetableSyncContract {
daoSession.getTimetableLessonDao().saveInTx(lessonList);
LogUtils.debug("Synchronization lessons (amount = " + lessonList.size() + ")");
LogUtils.debug("Synchronization timetable lessons (amount = " + lessonList.size() + ")");
}
private String getNormalizedDate(String date) throws ParseException {
@ -67,7 +67,7 @@ public class TimetableSync implements TimetableSyncContract {
}
private io.github.wulkanowy.api.generic.Week<io.github.wulkanowy.api.generic.Day> getWeekFromApi(String date)
throws IOException, NotLoggedInErrorException, ParseException {
throws IOException, VulcanException, ParseException {
return vulcan.getTimetable().getWeekTable(date);
}

View File

@ -3,11 +3,11 @@ package io.github.wulkanowy.data.sync.timetable;
import java.io.IOException;
import java.text.ParseException;
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
import io.github.wulkanowy.api.VulcanException;
public interface TimetableSyncContract {
void syncTimetable(String date) throws NotLoggedInErrorException, IOException, ParseException;
void syncTimetable(String date) throws VulcanException, IOException, ParseException;
void syncTimetable() throws NotLoggedInErrorException, IOException, ParseException;
void syncTimetable() throws VulcanException, IOException, ParseException;
}

View File

@ -21,11 +21,11 @@ import io.github.wulkanowy.data.db.resources.ResourcesContract;
import io.github.wulkanowy.data.db.shared.SharedPref;
import io.github.wulkanowy.data.db.shared.SharedPrefContract;
import io.github.wulkanowy.data.sync.SyncContract;
import io.github.wulkanowy.data.sync.account.AccountSync;
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.login.LoginSync;
import io.github.wulkanowy.data.sync.login.LoginSyncContract;
import io.github.wulkanowy.data.sync.subjects.SubjectSync;
import io.github.wulkanowy.data.sync.timetable.TimetableSync;
import io.github.wulkanowy.data.sync.timetable.TimetableSyncContract;
@ -100,8 +100,8 @@ public class ApplicationModule {
@Singleton
@Provides
LoginSyncContract provideLoginSync(LoginSync loginSync) {
return loginSync;
AccountSyncContract provideLoginSync(AccountSync accountSync) {
return accountSync;
}
@SyncGrades

View File

@ -62,7 +62,7 @@ public class SyncJob extends SimpleJobService {
@Override
public int onRunJob(JobParameters job) {
try {
repository.loginCurrentUser();
repository.initLastUser();
repository.syncAll();
gradeList = repository.getNewGrades();

View File

@ -42,6 +42,8 @@ public interface LoginContract {
void onStartAsync();
void onDoInBackground(int stepNumber) throws Exception;
void onLoginProgress(int step);
void onEndAsync(boolean success, Exception exception);

View File

@ -17,6 +17,12 @@ public class LoginPresenter extends BasePresenter<LoginContract.View>
private LoginTask loginAsync;
private String email;
private String password;
private String symbol;
@Inject
LoginPresenter(RepositoryContract repository) {
super(repository);
@ -26,17 +32,17 @@ public class LoginPresenter extends BasePresenter<LoginContract.View>
public void attemptLogin(String email, String password, String symbol) {
getView().resetViewErrors();
this.email = email;
this.password = password;
this.symbol = getNormalizedSymbol(symbol);
if (!isAllFieldCorrect(password, email)) {
getView().showSoftInput();
return;
}
if (getView().isNetworkConnected()) {
// Dopóki używamy AsyncTask presenter będzie musiał "wiedzieć" o AsyncTaskach
loginAsync = new LoginTask(this,
email,
password,
getNormalizedSymbol(symbol));
loginAsync = new LoginTask(this);
loginAsync.execute();
} else {
@ -53,6 +59,18 @@ public class LoginPresenter extends BasePresenter<LoginContract.View>
}
}
@Override
public void onDoInBackground(int stepNumber) throws Exception {
switch (stepNumber) {
case 1:
getRepository().registerUser(email, password, symbol);
break;
case 2:
getRepository().syncAll();
break;
}
}
@Override
public void onLoginProgress(int step) {
if (step == 1) {

View File

@ -4,21 +4,12 @@ import android.os.AsyncTask;
public class LoginTask extends AsyncTask<Void, Integer, Boolean> {
private String email;
private String password;
private String symbol;
private LoginContract.Presenter presenter;
private Exception exception;
LoginTask(LoginContract.Presenter presenter, String email, String password, String symbol) {
LoginTask(LoginContract.Presenter presenter) {
this.presenter = presenter;
this.email = email;
this.password = password;
this.symbol = symbol;
}
@Override
@ -30,10 +21,10 @@ public class LoginTask extends AsyncTask<Void, Integer, Boolean> {
protected Boolean doInBackground(Void... params) {
try {
publishProgress(1);
presenter.getRepository().loginUser(email, password, symbol);
presenter.onDoInBackground(1);
publishProgress(2);
presenter.getRepository().syncAll();
presenter.onDoInBackground(2);
} catch (Exception e) {
exception = e;
return false;

View File

@ -153,7 +153,6 @@ public class AttendanceTabPresenter extends BasePresenter<AttendanceTabContract.
}
private void syncData() throws Exception {
getRepository().loginCurrentUser();
getRepository().syncAttendance(date);
}

View File

@ -71,7 +71,6 @@ public class GradesPresenter extends BasePresenter<GradesContract.View>
@Override
public void onDoInBackgroundRefresh() throws Exception {
getRepository().loginCurrentUser();
getRepository().syncSubjects();
getRepository().syncGrades();
}

View File

@ -157,7 +157,6 @@ public class TimetableTabPresenter extends BasePresenter<TimetableTabContract.Vi
}
private void syncData() throws Exception {
getRepository().loginCurrentUser();
getRepository().syncTimetable(date);
}

View File

@ -6,6 +6,7 @@ import javax.inject.Inject;
import io.github.wulkanowy.data.RepositoryContract;
import io.github.wulkanowy.ui.base.BasePresenter;
import io.github.wulkanowy.utils.LogUtils;
public class SplashPresenter extends BasePresenter<SplashContract.View>
implements SplashContract.Presenter {
@ -23,7 +24,12 @@ public class SplashPresenter extends BasePresenter<SplashContract.View>
if (getRepository().getCurrentUserId() == 0) {
getView().openLoginActivity();
} else {
getView().openMainActivity();
try {
getRepository().initLastUser();
getView().openMainActivity();
} catch (Exception e) {
LogUtils.error("An error occurred when the application was started", e);
}
}
}
}

View File

@ -6,10 +6,10 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import io.github.wulkanowy.api.grades.Grade;
import io.github.wulkanowy.api.grades.Subject;
import io.github.wulkanowy.api.generic.Day;
import io.github.wulkanowy.api.generic.Lesson;
import io.github.wulkanowy.api.grades.Grade;
import io.github.wulkanowy.api.grades.Subject;
import io.github.wulkanowy.data.db.dao.entities.TimetableLesson;
public class DataObjectConverterTest {