Made context menu for chat item (now able to delete chats)

This commit is contained in:
ChronosX88 2019-03-23 16:42:22 +04:00
parent e6f68720e8
commit 96800b2628
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
5 changed files with 49 additions and 5 deletions

View File

@ -1,6 +1,9 @@
package io.github.chronosx88.influence.contracts.chatlist; package io.github.chronosx88.influence.contracts.chatlist;
import android.view.MenuItem;
public interface ChatListPresenterContract { public interface ChatListPresenterContract {
void updateChatList(); void updateChatList();
void openChat(String chatID); void openChat(String chatID);
void onContextItemSelected(MenuItem item);
} }

View File

@ -1,5 +1,6 @@
package io.github.chronosx88.influence.helpers; package io.github.chronosx88.influence.helpers;
import android.view.ContextMenu;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -15,6 +16,7 @@ import io.github.chronosx88.influence.models.roomEntities.ChatEntity;
public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ChatListViewHolder> { public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ChatListViewHolder> {
List<ChatEntity> chatList = new ArrayList<>(); List<ChatEntity> chatList = new ArrayList<>();
public int onClickPosition = -1;
@NonNull @NonNull
@Override @Override
@ -29,9 +31,14 @@ public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ChatLi
chatList.addAll(entities); chatList.addAll(entities);
} }
public ChatEntity getItem(int position) {
return chatList.get(position);
}
@Override @Override
public void onBindViewHolder(@NonNull ChatListViewHolder holder, int position) { public void onBindViewHolder(@NonNull ChatListViewHolder holder, int position) {
holder.chatName.setText(chatList.get(position).getName()); holder.chatName.setText(chatList.get(position).getName());
holder.onLongClick(position);
} }
@Override @Override
@ -39,12 +46,26 @@ public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ChatLi
return chatList.size(); return chatList.size();
} }
class ChatListViewHolder extends RecyclerView.ViewHolder { class ChatListViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener {
TextView chatName; TextView chatName;
public ChatListViewHolder(View itemView) { public ChatListViewHolder(View itemView) {
super(itemView); super(itemView);
chatName = itemView.findViewById(R.id.chat_name); 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");
} }
} }
} }

View File

@ -12,7 +12,7 @@ public interface ChatDao {
@Insert @Insert
void addChat(ChatEntity chatEntity); void addChat(ChatEntity chatEntity);
@Query("DELETE FROM chats WHERE id = :chatID") @Query("DELETE FROM chats WHERE chatID = :chatID")
void deleteChat(String chatID); void deleteChat(String chatID);
@Query("SELECT * FROM chats") @Query("SELECT * FROM chats")

View File

@ -1,5 +1,7 @@
package io.github.chronosx88.influence.presenters; package io.github.chronosx88.influence.presenters;
import android.view.MenuItem;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import io.github.chronosx88.influence.contracts.chatlist.ChatListLogicContract; 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.ChatListAdapter;
import io.github.chronosx88.influence.helpers.actions.UIActions; import io.github.chronosx88.influence.helpers.actions.UIActions;
import io.github.chronosx88.influence.logic.ChatListLogic; import io.github.chronosx88.influence.logic.ChatListLogic;
import io.github.chronosx88.influence.observable.MainObservable;
public class ChatListPresenter implements ChatListPresenterContract, Observer { public class ChatListPresenter implements ChatListPresenterContract, Observer {
private ChatListViewContract view; private ChatListViewContract view;
@ -35,6 +36,18 @@ public class ChatListPresenter implements ChatListPresenterContract, Observer {
// TODO // 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 @Override
public void handleEvent(JsonObject object) { public void handleEvent(JsonObject object) {
switch (object.get("action").getAsInt()) { switch (object.get("action").getAsInt()) {

View File

@ -2,6 +2,7 @@ package io.github.chronosx88.influence.views.fragments;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; 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.ChatListAdapter;
import io.github.chronosx88.influence.helpers.actions.UIActions; import io.github.chronosx88.influence.helpers.actions.UIActions;
import io.github.chronosx88.influence.models.roomEntities.ChatEntity; import io.github.chronosx88.influence.models.roomEntities.ChatEntity;
import io.github.chronosx88.influence.observable.MainObservable;
import io.github.chronosx88.influence.presenters.ChatListPresenter; import io.github.chronosx88.influence.presenters.ChatListPresenter;
public class ChatListFragment extends Fragment implements ChatListViewContract, Observer { public class ChatListFragment extends Fragment implements ChatListViewContract, Observer {
@ -48,6 +48,7 @@ public class ChatListFragment extends Fragment implements ChatListViewContract,
chatList.setLayoutManager(new LinearLayoutManager(getContext())); chatList.setLayoutManager(new LinearLayoutManager(getContext()));
presenter = new ChatListPresenter(this); presenter = new ChatListPresenter(this);
presenter.updateChatList(); presenter.updateChatList();
registerForContextMenu(chatList);
} }
@Override @Override
@ -72,9 +73,15 @@ public class ChatListFragment extends Fragment implements ChatListViewContract,
@Override @Override
public void updateChatList(ChatListAdapter adapter, List<ChatEntity> chats) { public void updateChatList(ChatListAdapter adapter, List<ChatEntity> chats) {
getActivity().runOnUiThread(() -> { requireActivity().runOnUiThread(() -> {
adapter.setChatList(chats); adapter.setChatList(chats);
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
}); });
} }
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
presenter.onContextItemSelected(item);
return super.onContextItemSelected(item);
}
} }