diff --git a/app/build.gradle b/app/build.gradle index 4966d9f0..eb57cb92 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -80,6 +80,7 @@ dependencies { implementation 'eu.davidea:flexible-adapter-ui:1.0.0-b1' implementation 'org.apache.commons:commons-collections4:4.1' implementation 'org.greenrobot:greendao:3.2.2' + implementation 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.2' implementation 'com.jakewharton:butterknife:8.8.1' implementation 'joda-time:joda-time:2.9.9' implementation 'com.google.dagger:dagger-android:2.14.1' diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/DbHelper.java b/app/src/main/java/io/github/wulkanowy/data/db/dao/DbHelper.java index 9392bae5..a67e9b7f 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/DbHelper.java +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/DbHelper.java @@ -3,20 +3,26 @@ package io.github.wulkanowy.data.db.dao; import android.content.Context; import android.database.sqlite.SQLiteDatabase; +import com.github.yuweiguocn.library.greendao.MigrationHelper; + import org.greenrobot.greendao.database.Database; import org.greenrobot.greendao.database.StandardDatabase; import javax.inject.Inject; import javax.inject.Singleton; +import io.github.wulkanowy.BuildConfig; +import io.github.wulkanowy.data.db.dao.entities.AccountDao; import io.github.wulkanowy.data.db.dao.entities.DaoMaster; +import io.github.wulkanowy.data.db.dao.entities.GradeDao; +import io.github.wulkanowy.data.db.dao.entities.SubjectDao; import io.github.wulkanowy.data.db.shared.SharedPrefContract; import io.github.wulkanowy.di.annotations.ApplicationContext; import io.github.wulkanowy.di.annotations.DatabaseInfo; import io.github.wulkanowy.utils.LogUtils; @Singleton -public class DbHelper extends DaoMaster.DevOpenHelper { +public class DbHelper extends DaoMaster.OpenHelper { private SharedPrefContract sharedPref; @@ -28,19 +34,28 @@ public class DbHelper extends DaoMaster.DevOpenHelper { } @Override + @SuppressWarnings("unchecked") public void onUpgrade(Database db, int oldVersion, int newVersion) { - cleanUserData(db, oldVersion, newVersion); + MigrationHelper.DEBUG = BuildConfig.DEBUG; + MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() { + @Override + public void onCreateAllTables(Database db, boolean ifNotExists) { + DaoMaster.createAllTables(db, ifNotExists); + } + @Override + public void onDropAllTables(Database db, boolean ifExists) { + DaoMaster.dropAllTables(db, ifExists); + } + }, AccountDao.class, SubjectDao.class, GradeDao.class); } @Override public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { - cleanUserData(new StandardDatabase(db), oldVersion, newVersion); - } - - private void cleanUserData(Database database, int oldVersion, int newVersion) { - LogUtils.info("Cleaning user data oldVersion=" + oldVersion + " newVersion=" + newVersion); + Database database = new StandardDatabase(db); DaoMaster.dropAllTables(database, true); onCreate(database); sharedPref.setCurrentUserId(0); + + LogUtils.info("Cleaning user data oldVersion=" + oldVersion + " newVersion=" + newVersion); } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/main/attendance/AttendanceHeaderItem.java b/app/src/main/java/io/github/wulkanowy/ui/main/attendance/AttendanceHeaderItem.java index 0270f7c1..b103a007 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/main/attendance/AttendanceHeaderItem.java +++ b/app/src/main/java/io/github/wulkanowy/ui/main/attendance/AttendanceHeaderItem.java @@ -18,7 +18,6 @@ import eu.davidea.flexibleadapter.FlexibleAdapter; import eu.davidea.flexibleadapter.items.AbstractExpandableHeaderItem; import eu.davidea.viewholders.ExpandableViewHolder; import io.github.wulkanowy.R; -import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson; import io.github.wulkanowy.data.db.dao.entities.Day; public class AttendanceHeaderItem diff --git a/build.gradle b/build.gradle index 13b22854..999f91de 100644 --- a/build.gradle +++ b/build.gradle @@ -36,6 +36,7 @@ allprojects { jcenter() mavenCentral() google() + maven { url "https://jitpack.io" } } }