Skip to content

Commit

Permalink
Merge pull request #2017 from moneymanagerex/orm
Browse files Browse the repository at this point in the history
ORM: simplify SupportSQLiteOpenHelper and BriteDatabase provideDatabase
  • Loading branch information
guanlisheng authored Dec 28, 2024
2 parents b8a65ab + 721daba commit e512942
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
19 changes: 6 additions & 13 deletions app/src/main/java/com/money/manager/ex/core/ioc/DbModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import androidx.sqlite.db.SupportSQLiteOpenHelper;

// import com.money.manager.ex.sqlite3mc.SupportFactory;
import net.sqlcipher.database.SupportFactory;
import com.money.manager.ex.MmexApplication;
import com.money.manager.ex.database.MmxOpenHelper;
import com.squareup.sqlbrite3.BriteDatabase;
Expand Down Expand Up @@ -50,23 +48,18 @@ MmxOpenHelper provideOpenHelper(MmexApplication app) {
return app.openHelperAtomicReference.get();
}

@Provides SqlBrite provideSqlBrite() {
@Provides
SqlBrite provideSqlBrite() {
return new SqlBrite.Builder().logger(new SqlBrite.Logger() {
@Override public void log(String message) {
Timber.tag("Database").v(message);
}
}).build();
}

@Provides BriteDatabase provideDatabase(SqlBrite sqlBrite, MmxOpenHelper helper) {
SupportSQLiteOpenHelper.Factory factory = new SupportFactory(helper.getPassword().getBytes());
SupportSQLiteOpenHelper.Configuration configuration =
SupportSQLiteOpenHelper.Configuration.builder(helper.getContext())
.name(helper.getDbPath())
.callback(helper)
.build();
BriteDatabase db = sqlBrite.wrapDatabaseHelper(factory.create(configuration), Schedulers.io());
db.setLoggingEnabled(true);
return db;
@Provides
BriteDatabase provideDatabase(SqlBrite sqlBrite, MmxOpenHelper helper) {
SupportSQLiteOpenHelper supportHelper = helper.provideSupportSQLiteOpenHelper();
return sqlBrite.wrapDatabaseHelper(supportHelper, Schedulers.io());
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/money/manager/ex/database/MmxOpenHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ protected void finalize() throws Throwable {
super.finalize();
}

public SupportSQLiteOpenHelper provideSupportSQLiteOpenHelper() {
SupportSQLiteOpenHelper.Factory factory = new SupportFactory(this.mPassword.getBytes());
SupportSQLiteOpenHelper.Configuration configuration =
SupportSQLiteOpenHelper.Configuration.builder(mContext)
.name(this.dbPath)
.callback(this)
.build();

return factory.create(configuration);
}

public static void createDatabaseBackupOnUpgrade(String currentDbFile, long oldVersion) throws IOException {
File in = new File(currentDbFile);
String backupFileNameWithExtension = in.getName();
Expand Down

0 comments on commit e512942

Please sign in to comment.