Clean data user on bad user credentials (#138)

This commit is contained in:
Rafał Borcz 2018-06-13 03:07:34 +02:00 committed by Mikołaj Pich
parent 7b7be1eef1
commit 072c504d2b
7 changed files with 36 additions and 0 deletions

View File

@ -47,4 +47,10 @@ public class Repository implements RepositoryContract {
public SyncContract getSyncRepo() { public SyncContract getSyncRepo() {
return synchronization; return synchronization;
} }
@Override
public void cleanAllData() {
sharedPref.cleanSharedPref();
database.recreateDatabase();
}
} }

View File

@ -17,4 +17,6 @@ public interface RepositoryContract {
DbContract getDbRepo(); DbContract getDbRepo();
SyncContract getSyncRepo(); SyncContract getSyncRepo();
void cleanAllData();
} }

View File

@ -30,4 +30,6 @@ public interface DbContract {
long getCurrentSemesterId(); long getCurrentSemesterId();
int getCurrentSemesterName(); int getCurrentSemesterName();
void recreateDatabase();
} }

View File

@ -1,9 +1,12 @@
package io.github.wulkanowy.data.db.dao; package io.github.wulkanowy.data.db.dao;
import org.greenrobot.greendao.database.Database;
import java.util.List; import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
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.dao.entities.DiaryDao; 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.Grade;
@ -109,4 +112,12 @@ public class DbRepository implements DbContract {
SemesterDao.Properties.Current.eq(true) SemesterDao.Properties.Current.eq(true)
).unique(); ).unique();
} }
@Override
public void recreateDatabase() {
Database database = daoSession.getDatabase();
DaoMaster.dropAllTables(database, true);
DaoMaster.createAllTables(database, true);
}
} }

View File

@ -28,4 +28,6 @@ public interface SharedPrefContract {
boolean isServicesEnable(); boolean isServicesEnable();
boolean isNotifyEnable(); boolean isNotifyEnable();
void cleanSharedPref();
} }

View File

@ -88,4 +88,10 @@ public class SharedPrefRepository implements SharedPrefContract {
public boolean isMobileDisable() { public boolean isMobileDisable() {
return settingsSharedPref.getBoolean(SettingsFragment.SHARED_KEY_SERVICES_MOBILE_DISABLED, false); return settingsSharedPref.getBoolean(SettingsFragment.SHARED_KEY_SERVICES_MOBILE_DISABLED, false);
} }
@Override
public void cleanSharedPref() {
appSharedPref.edit().clear().apply();
settingsSharedPref.edit().clear().apply();
}
} }

View File

@ -21,6 +21,7 @@ import javax.inject.Inject;
import dagger.android.AndroidInjection; import dagger.android.AndroidInjection;
import io.github.wulkanowy.R; import io.github.wulkanowy.R;
import io.github.wulkanowy.api.login.BadCredentialsException;
import io.github.wulkanowy.data.RepositoryContract; import io.github.wulkanowy.data.RepositoryContract;
import io.github.wulkanowy.data.db.dao.entities.Grade; import io.github.wulkanowy.data.db.dao.entities.Grade;
import io.github.wulkanowy.data.sync.NotRegisteredUserException; import io.github.wulkanowy.data.sync.NotRegisteredUserException;
@ -82,6 +83,12 @@ public class SyncJob extends SimpleJobService {
logError(e); logError(e);
stop(getApplicationContext()); stop(getApplicationContext());
return JobService.RESULT_FAIL_NORETRY;
} catch (BadCredentialsException e) {
logError(e);
repository.cleanAllData();
stop(getApplicationContext());
return JobService.RESULT_FAIL_NORETRY; return JobService.RESULT_FAIL_NORETRY;
} catch (Exception e) { } catch (Exception e) {
logError(e); logError(e);