/*
- * Copyright © 2021 Damyan Ivanov.
+ * Copyright © 2022 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
package net.ktnx.mobileledger.dao;
-import android.os.AsyncTask;
-import android.os.Handler;
-import android.os.Looper;
-
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData;
import net.ktnx.mobileledger.db.TemplateAccount;
import net.ktnx.mobileledger.db.TemplateHeader;
import net.ktnx.mobileledger.db.TemplateWithAccounts;
+import net.ktnx.mobileledger.utils.Misc;
import java.util.List;
public abstract long insertSync(TemplateHeader item);
public void insertAsync(@NonNull TemplateHeader item, @Nullable Runnable callback) {
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
insertSync(item);
- if (callback != null) {
- new Handler(Looper.getMainLooper()).post(callback);
- }
+ if (callback != null)
+ Misc.onMainThread(callback);
});
}
public abstract void deleteSync(TemplateHeader item);
public void deleteAsync(@NonNull TemplateHeader item, @NonNull Runnable callback) {
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
deleteSync(item);
- new Handler(Looper.getMainLooper()).post(callback);
+ Misc.onMainThread(callback);
});
}
- @Query("SELECT * FROM templates ORDER BY UPPER(name)")
+ @Query("DELETE FROM templates")
+ public abstract void deleteAllSync();
+
+ @Query("SELECT * FROM templates ORDER BY is_fallback, UPPER(name)")
public abstract LiveData<List<TemplateHeader>> getTemplates();
@Query("SELECT * FROM templates WHERE id = :id")
public abstract LiveData<TemplateHeader> getTemplate(Long id);
+ @Query("SELECT * FROM templates WHERE id = :id")
+ public abstract TemplateHeader getTemplateSync(Long id);
+
public void getTemplateAsync(@NonNull Long id,
@NonNull AsyncResultCallback<TemplateHeader> callback) {
LiveData<TemplateHeader> resultReceiver = getTemplate(id);
@Transaction
@Query("SELECT * FROM templates WHERE id = :id")
- public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(Long id);
+ public abstract LiveData<TemplateWithAccounts> getTemplateWithAccounts(@NonNull Long id);
+
+ @Transaction
+ @Query("SELECT * FROM templates WHERE id = :id")
+ public abstract TemplateWithAccounts getTemplateWithAccountsSync(@NonNull Long id);
+
+ @Transaction
+ @Query("SELECT * FROM templates WHERE uuid = :uuid")
+ public abstract TemplateWithAccounts getTemplateWithAccountsByUuidSync(String uuid);
+
+ @Transaction
+ @Query("SELECT * FROM templates")
+ public abstract List<TemplateWithAccounts> getAllTemplatesWithAccountsSync();
@Transaction
public void insertSync(TemplateWithAccounts templateWithAccounts) {
}
}
- public void getTemplateWitAccountsAsync(@NonNull Long id, @NonNull
+ public void getTemplateWithAccountsAsync(@NonNull Long id, @NonNull
AsyncResultCallback<TemplateWithAccounts> callback) {
LiveData<TemplateWithAccounts> resultReceiver = getTemplateWithAccounts(id);
resultReceiver.observeForever(new Observer<TemplateWithAccounts>() {
});
}
public void insertAsync(@NonNull TemplateWithAccounts item, @Nullable Runnable callback) {
- AsyncTask.execute(() -> {
+ BaseDAO.runAsync(() -> {
insertSync(item);
- if (callback != null) {
- new Handler(Looper.getMainLooper()).post(callback);
+ if (callback != null)
+ Misc.onMainThread(callback);
+ });
+ }
+ public void duplicateTemplateWithAccounts(@NonNull Long id, @Nullable
+ AsyncResultCallback<TemplateWithAccounts> callback) {
+ BaseDAO.runAsync(() -> {
+ TemplateWithAccounts src = getTemplateWithAccountsSync(id);
+ TemplateWithAccounts dup = src.createDuplicate();
+ dup.header.setName(dup.header.getName());
+ dup.header.setId(insertSync(dup.header));
+ TemplateAccountDAO accDao = DB.get()
+ .getTemplateAccountDAO();
+ for (TemplateAccount dupAcc : dup.accounts) {
+ dupAcc.setTemplateId(dup.header.getId());
+ dupAcc.setId(accDao.insertSync(dupAcc));
}
+ if (callback != null)
+ Misc.onMainThread(() -> callback.onResult(dup));
});
}