Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / FriendActivity.java
k-fujii on 20 Jul 2023 8 KB FriendActivityの修正
package com.example.nemophila;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.nemophila.entities.AccountNameJson;
import com.example.nemophila.viewmodels.FriendViewModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class FriendActivity extends AppCompatActivity {
    private Nemophila nemophila;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend);

        // 初期化
        List<FriendDataModel> friendsDataset = new ArrayList<>();  // 1行分の情報を持つDataModelを格納するリスト
        RecyclerView rv = findViewById(R.id.friendsList);  // recyclerViewを取得
        FriendViewModel friendViewModel = new ViewModelProvider(this).get(FriendViewModel.class);  // ViewModelを取得
        nemophila = (Nemophila) this.getApplication();


        //一行ずつを縦に(LinearLayout)表示するLayoutManagerを宣言
        LinearLayoutManager llm = new LinearLayoutManager(this);

        // データ1つ1つの区切りが分かるように線を引く
        RecyclerView.ItemDecoration itemDecoration =
                new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        rv.addItemDecoration(itemDecoration);
        rv.setHasFixedSize(true);
        rv.setLayoutManager(llm);

        String myUid = nemophila.getUid();  // 自身のidを取得
        String myToken = nemophila.getToken(); // 自身のtokenを取得
        // getFriendsの通信が呼び出されて、フレンドの一覧をfriendsDatasetに格納し、Adapterにセットする
        FriendActivityAdapter adapter = new FriendActivityAdapter(friendsDataset) {
            @Override
            void onDeleteClick(View view, int position, FriendDataModel friend) {
                new AlertDialog.Builder(FriendActivity.this)
                        .setTitle("フレンドから" + friend.getName() + "を削除しますか?")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                friendViewModel.deleteFriend(myUid, friend.getId(), myToken);
                            }
                        })
                        .setNegativeButton("キャンセル", null)
                        .create()
                        .show();
            }

            @Override
            void onFavoriteClick(View view, int position, FriendDataModel friend, ImageView favorite) {
                if (favorite.getVisibility() == View.INVISIBLE) {
                    favorite.setVisibility(View.VISIBLE);
                    nemophila.setFavoriteFriends(friend.getId());
                } else {
                    favorite.setVisibility(View.INVISIBLE);
                    nemophila.removeFavoriteFriends(friend.getId());
                    System.out.println(nemophila.getFavoriteFriends());
                }
            }
        };
        friendViewModel.getFriends(myUid);  // 自身のフレンドの一覧を取得するメソッドを呼び出す

        friendViewModel.getFriendsLiveData().observe(this, new Observer<Collection<AccountNameJson>>() {
            @Override
            public void onChanged(Collection<AccountNameJson> friends) {
                // 以前のデータが残っていると不都合なので、空にしておく
                friendsDataset.clear();

                for (AccountNameJson friend : friends) {
                    FriendDataModel data = new FriendDataModel();
                    data.setName(friend.getName());
                    data.setId(friend.getUid());
                    friendsDataset.add(data);
                }

                adapter.setList(friendsDataset);
                rv.setAdapter(adapter);
            }
        });

        // マイページ画面に画面遷移する
        Button backButton = findViewById(R.id.backMyPage);
        backButton.setOnClickListener(v -> {
            Intent intent = new Intent(getApplication(), MyPageActivity.class);
            startActivity(intent);
        });
        
    }

    // ----------- RecyclerView用の内部クラス ------------//
    // Adapter
    abstract public class FriendActivityAdapter extends RecyclerView.Adapter<FriendsViewHolder> {
        private List<FriendDataModel> friendData;

        public FriendActivityAdapter(List<FriendDataModel> friendData) {
            this.friendData = friendData;
        }

        public void setList(List<FriendDataModel> friendData) {
            this.friendData = friendData;
        }

        @NonNull
        @Override
        public FriendsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int ViewType) {
            // layoutXMLからViewを生成
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.friend_list_item, parent,false);
            // ViewHolderを生成
            FriendsViewHolder vh = new FriendsViewHolder(inflate);

            // 削除ボタンを押したときのイベントを登録
            vh.deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = vh.getBindingAdapterPosition();
                    // 処理はonDeleteClick()に丸投げ
                    onDeleteClick(v, position, friendData.get(position));
                }
            });

            // お気に入りに追加したときのイベントを登録
            vh.favoriteOutline.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = vh.getBindingAdapterPosition();
                    // 処理はonFavoriteClick()に丸投げ
                    onFavoriteClick(v, position, friendData.get(position), vh.favorite);
                }
            });

            return vh;
        }

        @Override
        public void onBindViewHolder(@NonNull FriendsViewHolder holder, int position) {
            // 後からアイコンの登録をできるようにする
            holder.nameView.setText(friendData.get(position).getName());
            if (nemophila.getFavoriteFriends().contains(friendData.get(position).getId())) {
                holder.favorite.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public int getItemCount() {
            return friendData.size();
        }

        abstract void onDeleteClick(View view, int position, FriendDataModel friend);
        abstract void onFavoriteClick(View view, int position, FriendDataModel friend, ImageView favorite);
    }

    // ViewHolder
    public class FriendsViewHolder extends RecyclerView.ViewHolder {
        public ImageView favorite;
        public ImageButton favoriteOutline;
        public ImageView iconView;
        public TextView nameView;
        public Button deleteButton;

        public FriendsViewHolder(@NonNull View itemView) {
            super(itemView);
            favorite = itemView.findViewById(R.id.favorite);
            favoriteOutline = itemView.findViewById(R.id.favorite_outline);
            iconView = itemView.findViewById(R.id.friendIcon);
            nameView = itemView.findViewById(R.id.friendName);
            deleteButton = itemView.findViewById(R.id.friendDelete);
        }
    }

    // DataModel
    public static class FriendDataModel {
        private String name;
        private String id;

        public String getName() {
            return name;
        }
        public String getId() {
            return id;
        }

        public void setName(String name) {
            this.name = name;
        }
        public void setId(String id) { this.id = id;}
    }
}