]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/dao/ProfileDAO.java
Room-based profile management
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / dao / ProfileDAO.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.dao;
19
20 import android.os.AsyncTask;
21
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;
30
31 import net.ktnx.mobileledger.db.Profile;
32
33 import java.util.List;
34
35 @Dao
36 public abstract class ProfileDAO extends BaseDAO<Profile> {
37     @Insert(onConflict = OnConflictStrategy.REPLACE)
38     abstract long insertSync(Profile item);
39
40     @Transaction
41     public long insertLastSync(Profile item) {
42         int count = getProfileCountSync();
43         item.setOrderNo(count + 1);
44         return insertSync(item);
45     }
46     public void insertLast(Profile item, OnInsertedReceiver onInsertedReceiver) {
47         AsyncTask.execute(() -> {
48             long id = insertLastSync(item);
49             if (onInsertedReceiver != null)
50                 onInsertedReceiver.onInsert(id);
51         });
52     }
53
54     @Update
55     abstract void updateSync(Profile item);
56
57     @Delete
58     public abstract void deleteSync(Profile item);
59
60     @Query("select * from profiles where id = :profileId")
61     public abstract Profile getByIdSync(long profileId);
62
63     @Query("SELECT * FROM profiles WHERE id=:profileId")
64     public abstract LiveData<Profile> getById(long profileId);
65
66     @Query("SELECT * FROM profiles ORDER BY order_no")
67     public abstract List<Profile> getAllOrderedSync();
68
69     @Query("SELECT * FROM profiles ORDER BY order_no")
70     public abstract LiveData<List<Profile>> getAllOrdered();
71
72     @Query("SELECT * FROM profiles LIMIT 1")
73     public abstract Profile getAnySync();
74
75     @Query("SELECT MAX(order_no) FROM profiles")
76     public abstract int getProfileCountSync();
77     public void updateOrderSync(List<Profile> list) {
78         if (list == null)
79             list = getAllOrderedSync();
80         int order = 1;
81         for (Profile p : list) {
82             p.setOrderNo(order++);
83             updateSync(p);
84         }
85     }
86     public void updateOrder(List<Profile> list, Runnable onDone) {
87         AsyncTask.execute(() -> {
88             updateOrderSync(list);
89             if (onDone != null)
90                 onDone.run();
91
92         });
93     }
94 }