Skip to content

Commit 552bdd1

Browse files
committed
add ListNotInRemoteChatPostsViewModel #5
1 parent ed51081 commit 552bdd1

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

app/src/main/java/io/github/veeshostak/aichat/database/dao/ChatPostDao.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.veeshostak.aichat.database.dao;
22

3+
import android.arch.lifecycle.LiveData;
34
import android.arch.persistence.room.Dao;
45
import android.arch.persistence.room.Delete;
56
import android.arch.persistence.room.Insert;
@@ -22,7 +23,7 @@
2223
public interface ChatPostDao {
2324

2425
@Insert(onConflict = OnConflictStrategy.ABORT)
25-
void insertChatMessages(ChatPost... chatPosts); // array of chatPosts (0 or more)
26+
void insertChatPosts(ChatPost... chatPosts); // array of chatPosts (0 or more)
2627

2728
@Update
2829
void updateChatPosts(ChatPost... chatPosts);
@@ -34,15 +35,15 @@ public interface ChatPostDao {
3435
// Each @Query method is verified at compile time. (Room also verifies the return value of the query)
3536

3637
@Query("SELECT * FROM chat_posts")
37-
List<ChatPost> getAllChatPosts();
38+
LiveData<List<ChatPost>> getAllChatPosts();
3839

3940
@Query("SELECT * FROM chat_posts WHERE pushed_to_remote_db = 0")
40-
List<ChatPost> getAllChatPostsNotInRemoteDb();
41+
LiveData<List<ChatPost>> getAllChatPostsNotInRemoteDb();
4142

4243
@Query("DELETE FROM chat_posts")
43-
int deleteAllChatPosts(); // return # of deleted rows
44+
void deleteAllChatPosts();
4445

45-
@Query("SELECT * FROM chat_posts WHERE user_query = :userQuery LIMIT 1")
46-
ChatPost getChatPostWithUserQuery(String userQuery);
46+
// @Query("SELECT * FROM chat_posts WHERE user_query = :userQuery LIMIT 1")
47+
// ChatPost getChatPostWithUserQuery(String userQuery);
4748

4849
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.github.veeshostak.aichat.viewmodels;
2+
3+
import android.app.Application;
4+
import android.arch.lifecycle.AndroidViewModel;
5+
import android.arch.lifecycle.LiveData;
6+
import android.os.AsyncTask;
7+
8+
import java.util.List;
9+
10+
import io.github.veeshostak.aichat.database.AppDatabase;
11+
import io.github.veeshostak.aichat.database.entity.ChatPost;
12+
13+
/**
14+
* Created by vladshostak on 12/26/17.
15+
*/
16+
17+
public class ListNotInRemoteChatPostsViewModel extends AndroidViewModel {
18+
19+
private AppDatabase appDatabase;
20+
21+
private LiveData<List<ChatPost>> notInRemoteChatPostsList;
22+
23+
public ListNotInRemoteChatPostsViewModel(Application application) {
24+
super(application);
25+
// get instance of our db
26+
appDatabase = AppDatabase.getDatabase(this.getApplication());
27+
// Set ChatPosts. Live Data runs the query asynchronously on a background thread
28+
notInRemoteChatPostsList = appDatabase.chatPostDao().getAllChatPostsNotInRemoteDb();
29+
}
30+
// Live Data runs the query asynchronously on a background thread
31+
public LiveData<List<ChatPost>>getAllChatPostsNotInRemoteDb() {
32+
return appDatabase.chatPostDao().getAllChatPostsNotInRemoteDb();
33+
}
34+
35+
// Get chatPosts. Live Data runs the query asynchronously on a background thread
36+
public LiveData<List<ChatPost>> getData() {
37+
if (notInRemoteChatPostsList == null) {
38+
notInRemoteChatPostsList = appDatabase.chatPostDao().getAllChatPostsNotInRemoteDb();
39+
}
40+
return notInRemoteChatPostsList;
41+
}
42+
43+
44+
// delete doesnt use liveData, execute in another thread. (Live Data runs the query asynchronously on a background thread when needed)
45+
public void deleteItem(ChatPost toDeleteChatPost) {
46+
new ListNotInRemoteChatPostsViewModel.DeleteAsyncTask(appDatabase).execute(toDeleteChatPost);
47+
}
48+
private static class DeleteAsyncTask extends AsyncTask<ChatPost, Void, Void> {
49+
private AppDatabase appDb;
50+
DeleteAsyncTask(AppDatabase appDatabase) {
51+
appDb = appDatabase;
52+
}
53+
@Override
54+
protected Void doInBackground(final ChatPost... params) {
55+
appDb.chatPostDao().deleteChatPosts(params[0]);
56+
return null;
57+
}
58+
}
59+
60+
61+
}

0 commit comments

Comments
 (0)