mirror of
https://github.com/ChronosX88/Influence-P2P.git
synced 2024-11-25 08:22:18 +00:00
Made context menu for chat item (now able to delete chats)
This commit is contained in:
parent
e6f68720e8
commit
96800b2628
@ -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);
|
||||
}
|
||||
|
@ -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<ChatListAdapter.ChatListViewHolder> {
|
||||
List<ChatEntity> chatList = new ArrayList<>();
|
||||
public int onClickPosition = -1;
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
@ -29,9 +31,14 @@ public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ChatLi
|
||||
chatList.addAll(entities);
|
||||
}
|
||||
|
||||
public ChatEntity getItem(int position) {
|
||||
return chatList.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ChatListViewHolder holder, int position) {
|
||||
holder.chatName.setText(chatList.get(position).getName());
|
||||
holder.onLongClick(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -39,12 +46,26 @@ public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ChatLi
|
||||
return chatList.size();
|
||||
}
|
||||
|
||||
class ChatListViewHolder extends RecyclerView.ViewHolder {
|
||||
class ChatListViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
|
||||
TextView chatName;
|
||||
|
||||
public ChatListViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
chatName = itemView.findViewById(R.id.chat_name);
|
||||
itemView.setOnCreateContextMenuListener(this);
|
||||
}
|
||||
|
||||
public void onLongClick(int position) {
|
||||
itemView.setOnLongClickListener((v) -> {
|
||||
onClickPosition = position;
|
||||
itemView.showContextMenu();
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
|
||||
menu.add(0, 0, 0, "Remove chat");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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")
|
||||
|
@ -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()) {
|
||||
|
@ -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<ChatEntity> chats) {
|
||||
getActivity().runOnUiThread(() -> {
|
||||
requireActivity().runOnUiThread(() -> {
|
||||
adapter.setChatList(chats);
|
||||
adapter.notifyDataSetChanged();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onContextItemSelected(@NonNull MenuItem item) {
|
||||
presenter.onContextItemSelected(item);
|
||||
return super.onContextItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user