2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.dao;
20 import android.os.AsyncTask;
22 import androidx.lifecycle.LiveData;
23 import androidx.room.Dao;
24 import androidx.room.Delete;
25 import androidx.room.Insert;
26 import androidx.room.OnConflictStrategy;
27 import androidx.room.Query;
28 import androidx.room.Transaction;
29 import androidx.room.Update;
31 import net.ktnx.mobileledger.db.Profile;
33 import java.util.List;
36 public abstract class ProfileDAO extends BaseDAO<Profile> {
37 @Insert(onConflict = OnConflictStrategy.REPLACE)
38 abstract long insertSync(Profile item);
41 public long insertLastSync(Profile item) {
42 int count = getProfileCountSync();
43 item.setOrderNo(count + 1);
44 return insertSync(item);
46 public void insertLast(Profile item, OnInsertedReceiver onInsertedReceiver) {
47 AsyncTask.execute(() -> {
48 long id = insertLastSync(item);
49 if (onInsertedReceiver != null)
50 onInsertedReceiver.onInsert(id);
55 abstract void updateSync(Profile item);
58 public abstract void deleteSync(Profile item);
60 @Query("select * from profiles where id = :profileId")
61 public abstract Profile getByIdSync(long profileId);
63 @Query("SELECT * FROM profiles WHERE id=:profileId")
64 public abstract LiveData<Profile> getById(long profileId);
66 @Query("SELECT * FROM profiles ORDER BY order_no")
67 public abstract List<Profile> getAllOrderedSync();
69 @Query("SELECT * FROM profiles ORDER BY order_no")
70 public abstract LiveData<List<Profile>> getAllOrdered();
72 @Query("SELECT * FROM profiles LIMIT 1")
73 public abstract Profile getAnySync();
75 @Query("SELECT MAX(order_no) FROM profiles")
76 public abstract int getProfileCountSync();
77 public void updateOrderSync(List<Profile> list) {
79 list = getAllOrderedSync();
81 for (Profile p : list) {
82 p.setOrderNo(order++);
86 public void updateOrder(List<Profile> list, Runnable onDone) {
87 AsyncTask.execute(() -> {
88 updateOrderSync(list);