Skip to content

Commit

Permalink
ONE TRUE ENTITY MANAGER
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaned1as committed Mar 8, 2016
1 parent f80a75c commit fb5e6e9
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -38,8 +38,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 251
versionName "2.5.1"
versionCode 253
versionName "2.5.3"
}

try {
Expand Down
1 change: 1 addition & 0 deletions src/main/AndroidManifest.xml
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="com.android.vending.BILLING" />

<application
android:name=".DiaryApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/DiaryThemeRed" android:allowBackup="true">
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/adonai/diary_browser/AuthorizationForm.java
Expand Up @@ -40,7 +40,6 @@ public class AuthorizationForm extends AppCompatActivity implements OnClickListe

private ViewSwitcher mSwitcher;
private SharedPreferences mPreferences;
private PersistManager mEntityManager;

// авторизация
private Button mLogin, mRequestRegistration;
Expand All @@ -56,7 +55,6 @@ public void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setHomeButtonEnabled(true);

mPreferences = getApplicationContext().getSharedPreferences(Utils.mPrefsFile, MODE_PRIVATE);
mEntityManager = DbProvider.getTempHelper(this);

mSwitcher = (ViewSwitcher) findViewById(R.id.login_register_switcher);

Expand All @@ -74,7 +72,6 @@ public void onCreate(Bundle savedInstanceState) {
@Override
protected void onDestroy() {
super.onDestroy();
DbProvider.releaseTempHelper();
}

@Override
Expand All @@ -87,7 +84,7 @@ protected void onStart() {
}

private void loadAuthFromDb() {
RuntimeExceptionDao<CredentialsItem, String> credDao = mEntityManager.getCredentialsDao();
RuntimeExceptionDao<CredentialsItem, String> credDao = DbProvider.getHelper().getCredentialsDao();
List<CredentialsItem> saved = credDao.queryForAll();
ArrayAdapter<CredentialsItem> credAdapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, saved);
mUsername.setAdapter(credAdapter);
Expand Down Expand Up @@ -146,7 +143,7 @@ private void migrateAuthToDb() {
}

// загружаем данные в БД
final RuntimeExceptionDao<CredentialsItem, String> credDao = mEntityManager.getCredentialsDao();
final RuntimeExceptionDao<CredentialsItem, String> credDao = DbProvider.getHelper().getCredentialsDao();
credDao.callBatchTasks(new Callable<Void>() {
public Void call() throws Exception {
for (CredentialsItem account : oldCreds.values()) {
Expand Down Expand Up @@ -204,7 +201,7 @@ public void onClick(View v) {

// убираем автологин у всех аккаунтов
// по идее, будет всего один
RuntimeExceptionDao<CredentialsItem, String> credDao = mEntityManager.getCredentialsDao();
RuntimeExceptionDao<CredentialsItem, String> credDao = DbProvider.getHelper().getCredentialsDao();
for (CredentialsItem item : credDao.queryForEq("autologin", true)) {
item.setAutologin(false);
credDao.update(item);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/adonai/diary_browser/DiaryActivity.java
Expand Up @@ -96,7 +96,6 @@ public void onPanelClosed(View view) {
@Override
protected void onCreate(Bundle savedInstanceState) {
Utils.setupTheme(this);
DbProvider.setHelper(this);
super.onCreate(savedInstanceState);

mSharedPrefs = getApplicationContext().getSharedPreferences(Utils.mPrefsFile, MODE_PRIVATE);
Expand Down Expand Up @@ -321,7 +320,6 @@ public void handleFontChange(String currSize) {

@Override
protected void onDestroy() {
DbProvider.releaseHelper();
if (mCanBuy)
mHelper.dispose();
super.onDestroy();
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/adonai/diary_browser/DiaryApplication.java
@@ -0,0 +1,23 @@
package adonai.diary_browser;

import android.app.Application;

import adonai.diary_browser.database.DbProvider;

/**
* Для инициализации ресурсов, общих для всего приложения
*/
public class DiaryApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
DbProvider.setHelper(this);
}

@Override
public void onTerminate() {
super.onTerminate();
DbProvider.releaseHelper();
}
}
4 changes: 1 addition & 3 deletions src/main/java/adonai/diary_browser/LaunchActivity.java
Expand Up @@ -21,11 +21,9 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(getApplicationContext(), Utils.mPrefsFile, MODE_PRIVATE, R.xml.preferences, true);

PersistManager tHelper = DbProvider.getTempHelper(this);
RuntimeExceptionDao<CredentialsItem, String> credDao = tHelper.getCredentialsDao();
RuntimeExceptionDao<CredentialsItem, String> credDao = DbProvider.getHelper().getCredentialsDao();
List<CredentialsItem> autologinViable = credDao.queryForEq("autologin", true);
startActivity(new Intent(this, autologinViable.isEmpty() ? AuthorizationForm.class : DiaryListActivity.class));
DbProvider.releaseTempHelper();

finish();
}
Expand Down

0 comments on commit fb5e6e9

Please sign in to comment.