From: Damyan Ivanov Date: Wed, 15 Jul 2020 20:38:47 +0000 (+0300) Subject: DbOp*: optional post-execute hook X-Git-Tag: v0.15.0~92 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=a024759d8803c0c8a0a801b0733144a7943a98c5;p=mobile-ledger.git DbOp*: optional post-execute hook --- diff --git a/app/src/main/java/net/ktnx/mobileledger/async/DbOpItem.java b/app/src/main/java/net/ktnx/mobileledger/async/DbOpItem.java index 7ecce275..b031dc64 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/DbOpItem.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/DbOpItem.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2020 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -20,11 +20,16 @@ package net.ktnx.mobileledger.async; class DbOpItem { String sql; Object[] params; - public DbOpItem(String sql, Object[] params) { + Runnable onReady; + public DbOpItem(String sql, Object[] params, Runnable onReady) { this.sql = sql; this.params = params; + this.onReady = onReady; + } + public DbOpItem(String sql, Object[] params) { + this(sql, params, null); } public DbOpItem(String sql) { - this(sql, null); + this(sql, null, null); } } diff --git a/app/src/main/java/net/ktnx/mobileledger/async/DbOpQueue.java b/app/src/main/java/net/ktnx/mobileledger/async/DbOpQueue.java index 2bc541e3..081bbeee 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/DbOpQueue.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/DbOpQueue.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2020 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -26,7 +26,8 @@ public class DbOpQueue { static private final BlockingQueue queue = new LinkedBlockingQueue<>(); static private DbOpRunner runner; synchronized static public void init() { - if (runner != null) return; + if (runner != null) + return; debug("opQueue", "Starting runner thread"); runner = new DbOpRunner(queue); runner.start(); @@ -34,10 +35,11 @@ public class DbOpQueue { static public void done() { runner.interrupt(); } - public static void add(String sql, Object[] params) { + public static void add(String sql, Object[] params) {add(sql, params, null);} + public static void add(String sql, Object[] params, Runnable onReady) { init(); debug("opQueue", "Adding " + sql); - queue.add(new DbOpItem(sql, params)); + queue.add(new DbOpItem(sql, params, onReady)); } static void add(String sql) { queue.add(new DbOpItem(sql)); diff --git a/app/src/main/java/net/ktnx/mobileledger/async/DbOpRunner.java b/app/src/main/java/net/ktnx/mobileledger/async/DbOpRunner.java index 3a81614d..2944efed 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/DbOpRunner.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/DbOpRunner.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2020 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -35,10 +35,14 @@ class DbOpRunner extends Thread { while (!interrupted()) { try { DbOpItem item = queue.take(); - debug("opQrunner", "Got "+item.sql); - SQLiteDatabase db = App.getDatabase(); - debug("opQrunner", "Executing "+item.sql); - db.execSQL(item.sql, item.params); + debug("opQrunner", "Got " + item.sql); + { + SQLiteDatabase db = App.getDatabase(); + debug("opQrunner", "Executing " + item.sql); + db.execSQL(item.sql, item.params); + } + if (item.onReady != null) + item.onReady.run(); } catch (InterruptedException e) { break;