Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / FriendActivity.java
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.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

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 FriendActivityAdapter adapter;

    @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を取得
        adapter = new FriendActivityAdapter(friendsDataset);  // adapterを取得
        FriendViewModel friendViewModel = new ViewModelProvider(this).get(FriendViewModel.class);  // ViewModelを取得
        Nemophila nemophila = (Nemophila) this.getApplication();


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

        // 詳細不明(おまじない)
        RecyclerView.ItemDecoration itemDecoration =
                new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        rv.addItemDecoration(itemDecoration);
        rv.setHasFixedSize(true);
        rv.setLayoutManager(llm);

        String myUid = nemophila.getUid();  // 自身のidを取得
        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());
                    friendsDataset.add(data);

                    //RecyclerViewをonChangedが呼ばれるたび表示(アダプターを更新してRecyclerViewにセット)
                    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
    public class FriendActivityAdapter extends RecyclerView.Adapter<FriendsViewHolder> {
        private List<FriendDataModel> list;

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

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

        @NonNull
        @Override
        public FriendsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int ViewType) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_friends_activity, parent,false);
            FriendsViewHolder vh = new FriendsViewHolder(inflate);
            return vh;
        }

        @Override
        public void onBindViewHolder(@NonNull FriendsViewHolder holder, int position) {
            // 後からアイコンの登録をできるようにする
            holder.nameView.setText(list.get(position).getName());
        }

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

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

        public FriendsViewHolder(@NonNull View itemView) {
            super(itemView);
            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;

        public String getName() {
            return name;
        }

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