Skip to content

Commit

Permalink
Initial implementation of a progress dialog while db init/migration
Browse files Browse the repository at this point in the history
- needs testing

Signed-off-by: Arka Prava Basu <[email protected]>
  • Loading branch information
Arka Prava Basu authored and archie94 committed Oct 20, 2018
1 parent da4a9d7 commit 7f78254
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
34 changes: 34 additions & 0 deletions src/main/java/org/havenapp/main/ListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
package org.havenapp.main;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteException;
import android.graphics.Color;
import android.graphics.PorterDuff;
Expand All @@ -31,6 +35,7 @@
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
Expand Down Expand Up @@ -66,6 +71,10 @@
import java.util.List;
import java.util.StringTokenizer;

import static org.havenapp.main.database.DbConstantsKt.DB_INIT_END;
import static org.havenapp.main.database.DbConstantsKt.DB_INIT_START;
import static org.havenapp.main.database.DbConstantsKt.DB_INIT_STATUS;

public class ListActivity extends AppCompatActivity {

private RecyclerView recyclerView;
Expand Down Expand Up @@ -97,6 +106,23 @@ public class ListActivity extends AppCompatActivity {
}
};

private BroadcastReceiver dbBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getIntExtra(DB_INIT_STATUS, 0) == DB_INIT_START) {
progressDialog = new ProgressDialog(ListActivity.this);
progressDialog.setTitle(getString(R.string.please_wait));
progressDialog.setMessage(getString(R.string.migrating_data));
progressDialog.show();
} else if (intent.getIntExtra(DB_INIT_STATUS, 0) == DB_INIT_END) {
if (progressDialog != null)
progressDialog.dismiss();
}
}
};

private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -109,6 +135,8 @@ protected void onCreate(Bundle savedInstanceState) {
fab = findViewById(R.id.fab);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
LocalBroadcastManager.getInstance(this).registerReceiver(dbBroadcastReceiver,
new IntentFilter(DB_INIT_STATUS));

LinearLayoutManager llm = new LinearLayoutManager(this);
recyclerView.setLayoutManager(llm);
Expand Down Expand Up @@ -309,6 +337,12 @@ public boolean onOptionsItemSelected (MenuItem item) {
return true;
}

@Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(dbBroadcastReceiver);
}

private void removeAllEvents()
{
final List<Event> removedEvents = new ArrayList<Event>(events);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/havenapp/main/database/DbConstants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.havenapp.main.database

/**
* Created by Arka Prava Basu <[email protected]> on 19/10/18.
*/

const val DB_INIT_START = 0x1
const val DB_INIT_END = 0x2

const val DB_INIT_STATUS = "db_init_status"
24 changes: 20 additions & 4 deletions src/main/java/org/havenapp/main/database/HavenEventDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.arch.persistence.room.Room
import android.arch.persistence.room.RoomDatabase
import android.arch.persistence.room.TypeConverters
import android.content.Context
import android.content.Intent
import android.support.v4.content.LocalBroadcastManager
import org.havenapp.main.dao.EventDAO
import org.havenapp.main.dao.EventTriggerDAO
import org.havenapp.main.database.converter.HavenEventDBConverters
Expand All @@ -17,26 +19,40 @@ import org.havenapp.main.model.EventTrigger
*/
@Database(entities = [(Event::class), (EventTrigger::class)], version = 4)
@TypeConverters(HavenEventDBConverters::class)
abstract class HavenEventDB: RoomDatabase() {
abstract class HavenEventDB : RoomDatabase() {

abstract fun getEventDAO(): EventDAO

abstract fun getEventTriggerDAO() : EventTriggerDAO
abstract fun getEventTriggerDAO(): EventTriggerDAO

companion object {
private var INSTANCE : HavenEventDB? = null
private var INSTANCE: HavenEventDB? = null

@JvmStatic
fun getDatabase(context: Context) : HavenEventDB {
fun getDatabase(context: Context): HavenEventDB {

if (INSTANCE == null) {
synchronized(HavenEventDB::class) {
if (INSTANCE == null) {

// notify interested components that db initialization is starting
var dbIntent = Intent()
dbIntent.putExtra(DB_INIT_STATUS, DB_INIT_START)
dbIntent.action = DB_INIT_STATUS
LocalBroadcastManager.getInstance(context).sendBroadcast(dbIntent)

INSTANCE = Room.databaseBuilder(context.applicationContext,
HavenEventDB::class.java, "haven.db")
.allowMainThreadQueries() // todo remove this
.addMigrations(RoomMigration())
.build()

// notify interested components that db initialization has succeeded
dbIntent = Intent()
dbIntent.putExtra(DB_INIT_STATUS, DB_INIT_END)
dbIntent.action = DB_INIT_STATUS
LocalBroadcastManager.getInstance(context).sendBroadcast(dbIntent)

}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,7 @@
<string name="config_storage_path">Storage Folder Path</string>
<string name="config_storage_page_hint">Where captured media is stored</string>
<string name="event_trigger_deleted">Event Trigger deleted</string>
<string name="please_wait">Please wait</string>
<string name="migrating_data">Looking for your old data</string>

</resources>

0 comments on commit 7f78254

Please sign in to comment.