Skip to content

Commit 0fe3478

Browse files
committed
Event Insert and Delete on a background thread for ListActivity
- database insertion and deletion now on a background thread through asynctask - updating the recycler view is managed through LiveData observable Signed-off-by: Arka Prava Basu <[email protected]>
1 parent 57da23b commit 0fe3478

File tree

6 files changed

+128
-63
lines changed

6 files changed

+128
-63
lines changed

src/main/java/org/havenapp/main/ListActivity.java

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.havenapp.main;
1919

2020
import android.annotation.SuppressLint;
21+
import android.arch.lifecycle.LiveData;
2122
import android.arch.lifecycle.Observer;
2223
import android.content.Intent;
2324
import android.database.sqlite.SQLiteException;
@@ -26,7 +27,6 @@
2627
import android.graphics.drawable.Drawable;
2728
import android.os.Build;
2829
import android.os.Bundle;
29-
import android.os.Handler;
3030
import android.support.annotation.NonNull;
3131
import android.support.design.widget.FloatingActionButton;
3232
import android.support.design.widget.Snackbar;
@@ -48,6 +48,10 @@
4848
import com.mikepenz.aboutlibraries.LibsBuilder;
4949

5050
import org.havenapp.main.database.HavenEventDB;
51+
import org.havenapp.main.database.async.EventDeleteAllAsync;
52+
import org.havenapp.main.database.async.EventDeleteAsync;
53+
import org.havenapp.main.database.async.EventInsertAllAsync;
54+
import org.havenapp.main.database.async.EventInsertAsync;
5155
import org.havenapp.main.model.Event;
5256
import org.havenapp.main.resources.IResourceManager;
5357
import org.havenapp.main.resources.ResourceManager;
@@ -76,8 +80,7 @@ public class ListActivity extends AppCompatActivity {
7680

7781
private int REQUEST_CODE_INTRO = 1001;
7882

79-
80-
private Handler handler = new Handler();
83+
private LiveData<List<Event>> eventListLD;
8184

8285
private Observer<List<Event>> eventListObserver = events -> {
8386
if (events != null) {
@@ -153,14 +156,9 @@ public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
153156
}
154157

155158

156-
fab.setOnClickListener(new View.OnClickListener() {
157-
@Override
158-
public void onClick(View v) {
159-
160-
Intent i = new Intent(ListActivity.this, MonitorActivity.class);
161-
startActivity(i);
162-
163-
}
159+
fab.setOnClickListener(v -> {
160+
Intent i = new Intent(ListActivity.this, MonitorActivity.class);
161+
startActivity(i);
164162
});
165163

166164
if (preferences.isFirstLaunch()) {
@@ -199,8 +197,8 @@ private void setEventListToRecyclerView(@NonNull List<Event> events) {
199197

200198
private void fetchEventList() {
201199
try {
202-
HavenEventDB.getDatabase(this).getEventDAO().getAllEventDesc()
203-
.observe(this, eventListObserver);
200+
eventListLD = HavenEventDB.getDatabase(this).getEventDAO().getAllEventDesc();
201+
eventListLD.observe(this, eventListObserver);
204202
} catch (SQLiteException sqe) {
205203
Log.d(getClass().getName(), "database not yet initiatied", sqe);
206204
}
@@ -218,33 +216,17 @@ private void showNonEmptyState() {
218216

219217
private void deleteEvent (final Event event, final int position)
220218
{
221-
222-
final Runnable runnableDelete = new Runnable ()
223-
{
224-
public void run ()
225-
{
226-
HavenEventDB.getDatabase(ListActivity.this).getEventDAO().delete(event);
227-
}
228-
};
229-
230-
handler.postDelayed(runnableDelete,5000);
219+
new EventDeleteAsync(() -> onEventDeleted(event, position)).execute(event);
231220
events.remove(position);
232221
adapter.notifyItemRemoved(position);
222+
}
233223

234-
HavenEventDB.getDatabase(ListActivity.this).getEventDAO().delete(event);
235-
224+
private void onEventDeleted(Event event, int position) {
236225
Snackbar.make(recyclerView, resourceManager.getString(R.string.event_deleted), Snackbar.LENGTH_SHORT)
237-
.setAction(resourceManager.getString(R.string.undo), new View.OnClickListener() {
238-
@Override
239-
public void onClick(View v) {
240-
handler.removeCallbacks(runnableDelete);
241-
long eventId = HavenEventDB.getDatabase(ListActivity.this)
242-
.getEventDAO().insert(event);
243-
event.setId(eventId);
244-
events.add(position, event);
245-
adapter.notifyItemInserted(position);
246-
}
247-
})
226+
.setAction(resourceManager.getString(R.string.undo),
227+
v -> new EventInsertAsync(eventId -> {
228+
event.setId(eventId);
229+
}).execute(event))
248230
.show();
249231
}
250232

@@ -329,36 +311,21 @@ public boolean onOptionsItemSelected (MenuItem item) {
329311

330312
private void removeAllEvents()
331313
{
332-
final List<Event> removedEvents = new ArrayList<Event>();
333-
final Runnable runnableDelete = new Runnable ()
334-
{
335-
public void run ()
336-
{
337-
for (Event event : removedEvents) {
338-
HavenEventDB.getDatabase(ListActivity.this).getEventDAO().delete(event);
339-
}
340-
}
341-
};
342-
343-
for (int i = 0, size = events.size(); i < size; i++) {
344-
removedEvents.add(events.remove(0));
345-
adapter.notifyItemRemoved(0);
346-
}
347-
348-
handler.postDelayed(runnableDelete, 3000);
314+
final List<Event> removedEvents = new ArrayList<Event>(events);
315+
events.clear();
316+
adapter.notifyDataSetChanged();
317+
new EventDeleteAllAsync(() -> onAllEventsRemoved(removedEvents)).execute(removedEvents);
318+
}
349319

320+
private void onAllEventsRemoved(List<Event> removedEvents) {
350321
Snackbar.make(recyclerView, resourceManager.getString(R.string.events_deleted), Snackbar.LENGTH_SHORT)
351-
.setAction(resourceManager.getString(R.string.undo), v -> {
352-
handler.removeCallbacks(runnableDelete);
353-
354-
for (Event event : removedEvents) {
355-
long eventId = HavenEventDB.getDatabase(ListActivity.this)
356-
.getEventDAO().insert(event);
357-
event.setId(eventId);
358-
events.add(event);
359-
adapter.notifyItemInserted(events.size() - 1);
360-
}
322+
.setAction(resourceManager.getString(R.string.undo),
323+
v -> new EventInsertAllAsync(eventIdList -> {
324+
for (int i = 0; i < removedEvents.size(); i++) {
325+
Event event = removedEvents.get(i);
326+
event.setId(eventIdList.get(i));
361327
}
328+
}).execute(removedEvents)
362329
)
363330
.show();
364331
}

src/main/java/org/havenapp/main/dao/EventDAO.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ interface EventDAO {
1313
@Insert
1414
fun insert(event: Event): Long
1515

16+
@Insert
17+
fun insertAll(eventList: List<Event>): List<Long>
18+
1619
@Delete
1720
fun delete(event: Event)
1821

22+
@Delete
23+
fun deleteAll(eventList: List<Event>)
24+
1925
@Update
2026
fun update(event: Event)
2127

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.havenapp.main.database.async
2+
3+
import android.os.AsyncTask
4+
import org.havenapp.main.HavenApp
5+
import org.havenapp.main.model.Event
6+
7+
/**
8+
* Created by Arka Prava Basu <[email protected]> on 6/9/18.
9+
*/
10+
class EventDeleteAllAsync(private val listener: EventDeleteAllListener)
11+
: AsyncTask<List<Event>, Unit, Unit>() {
12+
override fun doInBackground(vararg params: List<Event>) {
13+
HavenApp.dataBaseInstance.getEventDAO().deleteAll(params.get(0))
14+
}
15+
16+
override fun onPostExecute(result: Unit?) {
17+
listener.onEventsDeleted()
18+
}
19+
20+
interface EventDeleteAllListener {
21+
fun onEventsDeleted()
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.havenapp.main.database.async
2+
3+
import android.os.AsyncTask
4+
import org.havenapp.main.HavenApp
5+
import org.havenapp.main.model.Event
6+
7+
/**
8+
* Created by Arka Prava Basu <[email protected]> on 6/9/18.
9+
*/
10+
class EventDeleteAsync(private val listener: EventDeleteListener)
11+
: AsyncTask<Event, Unit, Unit>() {
12+
override fun doInBackground(vararg params: Event) {
13+
HavenApp.dataBaseInstance.getEventDAO().delete(params.get(0))
14+
}
15+
16+
override fun onPostExecute(result: Unit?) {
17+
listener.onDeleteEvent()
18+
}
19+
20+
interface EventDeleteListener {
21+
fun onDeleteEvent()
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.havenapp.main.database.async
2+
3+
import android.os.AsyncTask
4+
import org.havenapp.main.HavenApp
5+
import org.havenapp.main.model.Event
6+
7+
/**
8+
* Created by Arka Prava Basu <[email protected]> on 6/9/18.
9+
*/
10+
class EventInsertAllAsync(private val listener: EventInsertListener)
11+
: AsyncTask<List<Event>, Unit, List<Long>>() {
12+
override fun doInBackground(vararg params: List<Event>): List<Long> {
13+
return HavenApp.dataBaseInstance.getEventDAO().insertAll(params.get(0))
14+
}
15+
16+
override fun onPostExecute(result: List<Long>) {
17+
listener.onInsertionComplete(result)
18+
}
19+
20+
interface EventInsertListener {
21+
fun onInsertionComplete(eventIdList: List<Long>)
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.havenapp.main.database.async
2+
3+
import android.os.AsyncTask
4+
import org.havenapp.main.HavenApp
5+
import org.havenapp.main.model.Event
6+
7+
/**
8+
* Created by Arka Prava Basu <[email protected]> on 6/9/18.
9+
*/
10+
class EventInsertAsync(val listener: EventInsertListener): AsyncTask<Event, Unit, Long>() {
11+
override fun doInBackground(vararg params: Event): Long {
12+
val event = params.get(0)
13+
return HavenApp.dataBaseInstance.getEventDAO().insert(event)
14+
}
15+
16+
override fun onPostExecute(result: Long) {
17+
listener.onInsertionComplete(result)
18+
}
19+
20+
interface EventInsertListener {
21+
fun onInsertionComplete(eventId: Long)
22+
}
23+
}

0 commit comments

Comments
 (0)