Skip to content

Commit

Permalink
better approach to notify savedChatsAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
DevEmperor committed Feb 4, 2024
1 parent f0f4105 commit 018562a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.devemperor.wristassist.activities;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
Expand All @@ -27,6 +26,8 @@ public class SavedChatsActivity extends Activity {
ChatHistoryDatabaseHelper chatHistoryDatabaseHelper;
SavedChatsAdapter savedChatsAdapter;

int currentEditPosition = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -41,6 +42,7 @@ protected void onCreate(Bundle savedInstanceState) {
List<ChatHistoryModel> chats = chatHistoryDatabaseHelper.getAllChats();

savedChatsAdapter = new SavedChatsAdapter(chats, (chatPosition, longClick) -> {
currentEditPosition = chatPosition;
Intent intent;
if (!longClick) {
intent = new Intent(this, ChatActivity.class);
Expand All @@ -66,14 +68,13 @@ protected void onCreate(Bundle savedInstanceState) {
});
}

@SuppressLint("NotifyDataSetChanged")
@Override
protected void onResume() {
super.onResume();
savedChatsAdapter.getData().clear();
savedChatsAdapter.getData().addAll(chatHistoryDatabaseHelper.getAllChats());
savedChatsAdapter.notifyDataSetChanged();

findViewById(R.id.no_saved_chats).setVisibility(savedChatsAdapter.getData().isEmpty() ? android.view.View.VISIBLE : android.view.View.GONE);
if (savedChatsAdapter.getItemCount() != chatHistoryDatabaseHelper.getCount()) {
savedChatsAdapter.getData().remove(currentEditPosition);
savedChatsAdapter.notifyItemRemoved(currentEditPosition);
findViewById(R.id.no_saved_chats).setVisibility(savedChatsAdapter.getData().isEmpty() ? android.view.View.VISIBLE : android.view.View.GONE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,14 @@ public List<ChatHistoryModel> getAllChats() {
db.close();
return chatHistoryModels;
}

public long getCount() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM CHAT_HISTORY_TABLE", null);
cursor.moveToFirst();
long count = cursor.getLong(0);
cursor.close();
db.close();
return count;
}
}

0 comments on commit 018562a

Please sign in to comment.