diff --git a/app/src/main/java/io/github/chronosx88/influence/contracts/chatlist/ChatListPresenterContract.java b/app/src/main/java/io/github/chronosx88/influence/contracts/chatlist/ChatListPresenterContract.java index 1abbb27..15a8bbd 100644 --- a/app/src/main/java/io/github/chronosx88/influence/contracts/chatlist/ChatListPresenterContract.java +++ b/app/src/main/java/io/github/chronosx88/influence/contracts/chatlist/ChatListPresenterContract.java @@ -1,6 +1,9 @@ package io.github.chronosx88.influence.contracts.chatlist; +import android.view.MenuItem; + public interface ChatListPresenterContract { void updateChatList(); void openChat(String chatID); + void onContextItemSelected(MenuItem item); } diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/ChatListAdapter.java b/app/src/main/java/io/github/chronosx88/influence/helpers/ChatListAdapter.java index ec5f243..90771a5 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/ChatListAdapter.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/ChatListAdapter.java @@ -1,5 +1,6 @@ package io.github.chronosx88.influence.helpers; +import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -15,6 +16,7 @@ import io.github.chronosx88.influence.models.roomEntities.ChatEntity; public class ChatListAdapter extends RecyclerView.Adapter { List chatList = new ArrayList<>(); + public int onClickPosition = -1; @NonNull @Override @@ -29,9 +31,14 @@ public class ChatListAdapter extends RecyclerView.Adapter { + onClickPosition = position; + itemView.showContextMenu(); + return true; + }); + } + + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + menu.add(0, 0, 0, "Remove chat"); } } } diff --git a/app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java b/app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java index 1be1436..32e8f0a 100644 --- a/app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java +++ b/app/src/main/java/io/github/chronosx88/influence/models/daos/ChatDao.java @@ -12,7 +12,7 @@ public interface ChatDao { @Insert void addChat(ChatEntity chatEntity); - @Query("DELETE FROM chats WHERE id = :chatID") + @Query("DELETE FROM chats WHERE chatID = :chatID") void deleteChat(String chatID); @Query("SELECT * FROM chats") diff --git a/app/src/main/java/io/github/chronosx88/influence/presenters/ChatListPresenter.java b/app/src/main/java/io/github/chronosx88/influence/presenters/ChatListPresenter.java index ab9b8e2..93261d2 100644 --- a/app/src/main/java/io/github/chronosx88/influence/presenters/ChatListPresenter.java +++ b/app/src/main/java/io/github/chronosx88/influence/presenters/ChatListPresenter.java @@ -1,5 +1,7 @@ package io.github.chronosx88.influence.presenters; +import android.view.MenuItem; + import com.google.gson.JsonObject; import io.github.chronosx88.influence.contracts.chatlist.ChatListLogicContract; @@ -10,7 +12,6 @@ import io.github.chronosx88.influence.helpers.AppHelper; import io.github.chronosx88.influence.helpers.ChatListAdapter; import io.github.chronosx88.influence.helpers.actions.UIActions; import io.github.chronosx88.influence.logic.ChatListLogic; -import io.github.chronosx88.influence.observable.MainObservable; public class ChatListPresenter implements ChatListPresenterContract, Observer { private ChatListViewContract view; @@ -35,6 +36,18 @@ public class ChatListPresenter implements ChatListPresenterContract, Observer { // TODO } + @Override + public void onContextItemSelected(MenuItem item) { + switch(item.getItemId()) { + case 0: { + if(chatListAdapter.onClickPosition != -1) { + AppHelper.getChatDB().chatDao().deleteChat(chatListAdapter.getItem(chatListAdapter.onClickPosition).chatID); + view.updateChatList(chatListAdapter, logic.loadAllChats()); + } + } + } + } + @Override public void handleEvent(JsonObject object) { switch (object.get("action").getAsInt()) { diff --git a/app/src/main/java/io/github/chronosx88/influence/views/fragments/ChatListFragment.java b/app/src/main/java/io/github/chronosx88/influence/views/fragments/ChatListFragment.java index 2551b75..15b0a6e 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/fragments/ChatListFragment.java +++ b/app/src/main/java/io/github/chronosx88/influence/views/fragments/ChatListFragment.java @@ -2,6 +2,7 @@ package io.github.chronosx88.influence.views.fragments; import android.os.Bundle; import android.view.LayoutInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -22,7 +23,6 @@ import io.github.chronosx88.influence.helpers.AppHelper; import io.github.chronosx88.influence.helpers.ChatListAdapter; import io.github.chronosx88.influence.helpers.actions.UIActions; import io.github.chronosx88.influence.models.roomEntities.ChatEntity; -import io.github.chronosx88.influence.observable.MainObservable; import io.github.chronosx88.influence.presenters.ChatListPresenter; public class ChatListFragment extends Fragment implements ChatListViewContract, Observer { @@ -48,6 +48,7 @@ public class ChatListFragment extends Fragment implements ChatListViewContract, chatList.setLayoutManager(new LinearLayoutManager(getContext())); presenter = new ChatListPresenter(this); presenter.updateChatList(); + registerForContextMenu(chatList); } @Override @@ -72,9 +73,15 @@ public class ChatListFragment extends Fragment implements ChatListViewContract, @Override public void updateChatList(ChatListAdapter adapter, List chats) { - getActivity().runOnUiThread(() -> { + requireActivity().runOnUiThread(() -> { adapter.setChatList(chats); adapter.notifyDataSetChanged(); }); } + + @Override + public boolean onContextItemSelected(@NonNull MenuItem item) { + presenter.onContextItemSelected(item); + return super.onContextItemSelected(item); + } }