Skip to content
This repository has been archived by the owner on Aug 15, 2021. It is now read-only.

Commit

Permalink
2020-03-06 Version 1.0.5: Refactored project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
fartem committed Mar 6, 2020
1 parent 374f77a commit a133581
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 80 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName '1.0.4'
versionName '1.0.5'

testInstrumentationRunner 'com.smlnskgmail.jaman.ormlitedatabackup.runner.AndroidJacocoTestRunner'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.OrmLiteHelperFactory;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.OrmLiteLocalBackupPath;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.tasks.CreateOrmLiteLocalBackupTask;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.tasks.create.CreateOrmLiteLocalBackupTask;

import org.junit.Before;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@

import com.jakewharton.processphoenix.ProcessPhoenix;
import com.smlnskgmail.jaman.adaptiverecyclerview.AdaptiveRecyclerView;
import com.smlnskgmail.jaman.ormlitedatabackup.components.activities.BaseActivity;
import com.smlnskgmail.jaman.ormlitedatabackup.components.snackbars.LongSnackbar;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.event.actions.CreateEventBottomSheet;
import com.smlnskgmail.jaman.ormlitedatabackup.components.BaseActivity;
import com.smlnskgmail.jaman.ormlitedatabackup.components.LongSnackbar;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.event.actions.create.CreateEventBottomSheet;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.event.actions.create.EventCreateTarget;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.event.list.EventsAdapter;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.OrmLiteDatabaseParameters;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.OrmLiteHelperFactory;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.OrmLiteLocalBackupPath;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.tasks.CreateOrmLiteLocalBackupTask;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.tasks.RestoreOrmLiteLocalBackupTask;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.tasks.create.CreateOrmLiteLocalBackupTask;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.tasks.restore.RestoreOrmLiteLocalBackupTask;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.entities.Event;

import java.util.ArrayList;
Expand All @@ -31,7 +32,7 @@

import jahirfiquitiva.libs.fabsmenu.FABsMenu;

public class MainActivity extends BaseActivity implements CreateEventBottomSheet.EventCreateTarget {
public class MainActivity extends BaseActivity implements EventCreateTarget {

private static final int REQUEST_CODE_STORAGE = 101;

Expand All @@ -56,7 +57,6 @@ private void initViews() {
setClickToFABTitle(R.id.create_event, view -> {
hideFab();
CreateEventBottomSheet createEventBottomSheet = new CreateEventBottomSheet();
createEventBottomSheet.setupCreateEventTarget(MainActivity.this);
createEventBottomSheet.show(
getSupportFragmentManager(),
createEventBottomSheet.getClass().getName()
Expand Down Expand Up @@ -96,11 +96,14 @@ private void initViews() {
}

private String localDatabasePath() {
OrmLiteDatabaseParameters ormLiteDatabaseParameters = OrmLiteHelperFactory.databaseParameters();
return new OrmLiteLocalBackupPath(ormLiteDatabaseParameters).pathAsString();
OrmLiteDatabaseParameters parameters = OrmLiteHelperFactory.databaseParameters();
return new OrmLiteLocalBackupPath(parameters).pathAsString();
}

private void setClickToFABTitle(int resId, View.OnClickListener clickListener) {
private void setClickToFABTitle(
int resId,
View.OnClickListener clickListener
) {
findViewById(resId).setOnClickListener(clickListener);
}

Expand All @@ -116,7 +119,10 @@ private boolean isStoragePermissionGranted() {

@RequiresApi(api = Build.VERSION_CODES.M)
private void requestStoragePermission() {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_STORAGE);
requestPermissions(
new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_STORAGE
);
}

@RequiresApi(api = Build.VERSION_CODES.M)
Expand All @@ -126,7 +132,11 @@ public void onRequestPermissionsResult(
@NonNull String[] permissions,
@NonNull int[] grantResults
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
super.onRequestPermissionsResult(
requestCode,
permissions,
grantResults
);
if (requestCode == REQUEST_CODE_STORAGE) {
int snackbarMessageResId;
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smlnskgmail.jaman.ormlitedatabackup.components.activities;
package com.smlnskgmail.jaman.ormlitedatabackup.components;

import android.os.Bundle;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smlnskgmail.jaman.ormlitedatabackup.components.bottomsheets;
package com.smlnskgmail.jaman.ormlitedatabackup.components;

import android.os.Bundle;
import android.view.LayoutInflater;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smlnskgmail.jaman.ormlitedatabackup.components.snackbars;
package com.smlnskgmail.jaman.ormlitedatabackup.components;

import android.view.View;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smlnskgmail.jaman.ormlitedatabackup.logic.event.actions;
package com.smlnskgmail.jaman.ormlitedatabackup.logic.event.actions.create;

import android.view.View;
import android.widget.EditText;
Expand All @@ -7,7 +7,7 @@
import androidx.annotation.NonNull;

import com.smlnskgmail.jaman.ormlitedatabackup.R;
import com.smlnskgmail.jaman.ormlitedatabackup.components.bottomsheets.BaseBottomSheet;
import com.smlnskgmail.jaman.ormlitedatabackup.components.BaseBottomSheet;
import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.entities.Event;

import java.text.DateFormat;
Expand All @@ -16,12 +16,6 @@

public class CreateEventBottomSheet extends BaseBottomSheet {

private EventCreateTarget eventCreateTarget;

public void setupCreateEventTarget(@NonNull EventCreateTarget eventCreateTarget) {
this.eventCreateTarget = eventCreateTarget;
}

@Override
public void init(View view) {
Date now = Calendar.getInstance().getTime();
Expand All @@ -38,6 +32,7 @@ public void init(View view) {

view.findViewById(R.id.add_new_event_close).setOnClickListener(button -> dismiss());
view.findViewById(R.id.add_new_event).setOnClickListener(button -> {
EventCreateTarget eventCreateTarget = (EventCreateTarget) getActivity();
eventCreateTarget.eventAdded(
new Event(
textFrom(view.findViewById(R.id.new_event_title)),
Expand All @@ -49,7 +44,10 @@ public void init(View view) {
});
}

private void applyTextTo(@NonNull TextView textView, @NonNull String text) {
private void applyTextTo(
@NonNull TextView textView,
@NonNull String text
) {
textView.setText(text);
}

Expand All @@ -59,7 +57,10 @@ private String textFrom(@NonNull EditText editText) {
}

@NonNull
private String formatDate(@NonNull Date date, int type) {
private String formatDate(
@NonNull Date date,
int type
) {
return DateFormat.getDateInstance(type).format(date);
}

Expand All @@ -68,10 +69,4 @@ public int layoutResId() {
return R.layout.bottom_sheet_create_event;
}

public interface EventCreateTarget {

void eventAdded(@NonNull Event event);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.smlnskgmail.jaman.ormlitedatabackup.logic.event.actions.create;

import androidx.annotation.NonNull;

import com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.entities.Event;

public interface EventCreateTarget {

void eventAdded(@NonNull Event event);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,44 @@
@SuppressLint("StaticFieldLeak")
public class OrmLiteHelperFactory {

private static OrmLiteDatabaseHelper ormLiteDatabaseHelper;
private static OrmLiteDatabaseHelper databaseHelper;

private static OrmLiteDatabaseParameters ormLiteDatabaseParameters;
private static OrmLiteDatabaseParameters databaseParameters;

private OrmLiteHelperFactory(
@NonNull OrmLiteDatabaseHelper ormLiteDatabaseHelper
@NonNull OrmLiteDatabaseHelper databaseHelper
) {
OrmLiteHelperFactory.ormLiteDatabaseHelper = ormLiteDatabaseHelper;
OrmLiteHelperFactory.databaseHelper = databaseHelper;
}

public static void setHelper(@NonNull Context context) {
if (ormLiteDatabaseHelper != null) {
if (databaseHelper != null) {
releaseHelper();
}
ormLiteDatabaseHelper = OpenHelperManager.getHelper(
databaseHelper = OpenHelperManager.getHelper(
context,
OrmLiteDatabaseHelper.class
);
}

public static OrmLiteDatabaseHelper databaseHelper() {
return ormLiteDatabaseHelper;
return databaseHelper;
}

public static void releaseHelper() {
OpenHelperManager.releaseHelper();
ormLiteDatabaseHelper = null;
databaseHelper = null;
}

public static OrmLiteDatabaseParameters databaseParameters() {
if (ormLiteDatabaseParameters == null) {
String databaseName = ormLiteDatabaseHelper.databaseName();
ormLiteDatabaseParameters = new OrmLiteDatabaseParameters(
ormLiteDatabaseHelper.context(),
if (databaseParameters == null) {
String databaseName = databaseHelper.databaseName();
databaseParameters = new OrmLiteDatabaseParameters(
databaseHelper.context(),
databaseName
);
}
return ormLiteDatabaseParameters;
return databaseParameters;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public boolean isValidDB() {
databasePath,
null,
SQLiteDatabase.OPEN_READONLY
); OrmLiteSqliteOpenHelper openHelper = new OrmLiteDatabaseHelper(context, databasePath)) {
); OrmLiteSqliteOpenHelper helper = new OrmLiteDatabaseHelper(context, databasePath)) {
int version = sqLiteDatabase.getVersion();
for (Class clazz : OrmLiteDatabaseHelper.DB_CLASSED) {
Dao dao = openHelper.getDao(clazz);
Dao dao = helper.getDao(clazz);
dao.countOf();
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

public class OrmLiteLocalBackupPath {

private final OrmLiteDatabaseParameters ormLiteDatabaseParameters;
private final OrmLiteDatabaseParameters parameters;

public OrmLiteLocalBackupPath(@NonNull OrmLiteDatabaseParameters ormLiteDatabaseParameters) {
this.ormLiteDatabaseParameters = ormLiteDatabaseParameters;
public OrmLiteLocalBackupPath(@NonNull OrmLiteDatabaseParameters parameters) {
this.parameters = parameters;
}

public String pathAsString() {
String databaseName = ormLiteDatabaseParameters.databaseName();
String databaseName = parameters.databaseName();
String backupFileName = databaseName.substring(0, databaseName.indexOf(".")) + ".backup";
return Environment.getExternalStorageDirectory() + "/OrmLiteDataBackup/" + backupFileName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.tasks.create;

public interface CreateLocalBackupTarget {

void localBackupCreated(boolean result);

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.tasks;
package com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.tasks.create;

import android.annotation.SuppressLint;
import android.content.Context;
Expand All @@ -17,22 +17,22 @@ public class CreateOrmLiteLocalBackupTask extends AsyncTask<Void, Void, Boolean>

@SuppressLint("StaticFieldLeak")
private final Context context;
private final CreateLocalBackupTarget createLocalBackupTarget;
private final CreateLocalBackupTarget backupTarget;

public CreateOrmLiteLocalBackupTask(
@NonNull Context context,
@NonNull CreateLocalBackupTarget createLocalBackupTarget
@NonNull CreateLocalBackupTarget backupTarget
) {
this.context = context;
this.createLocalBackupTarget = createLocalBackupTarget;
this.backupTarget = backupTarget;
}

@Override
protected Boolean doInBackground(Void... voids) {
checkpoint();
OrmLiteDatabaseParameters ormLiteDatabaseParameters = OrmLiteHelperFactory.databaseParameters();
String from = ormLiteDatabaseParameters.databasePath();
String to = new OrmLiteLocalBackupPath(ormLiteDatabaseParameters).pathAsString();
OrmLiteDatabaseParameters parameters = OrmLiteHelperFactory.databaseParameters();
String from = parameters.databasePath();
String to = new OrmLiteLocalBackupPath(parameters).pathAsString();
return new FileCopy(
context,
from,
Expand All @@ -50,13 +50,7 @@ private void checkpoint() {

@Override
protected void onPostExecute(Boolean result) {
createLocalBackupTarget.localBackupCreated(result);
}

public interface CreateLocalBackupTarget {

void localBackupCreated(boolean result);

backupTarget.localBackupCreated(result);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.smlnskgmail.jaman.ormlitedatabackup.logic.ormlite.backup.tasks.restore;

public interface RestoreLocalBackupTarget {

void localBackupRestored(boolean success);

}
Loading

0 comments on commit a133581

Please sign in to comment.