Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / MyPageActivity.java
package com.example.nemophila;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
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.TextView;

import com.example.nemophila.entities.Post;
import com.example.nemophila.viewmodels.AccountViewModel;

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

public class MyPageActivity extends AppCompatActivity {
    //MyPageActivity画面の作成
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mypage);

        Nemophila nemophila = (Nemophila) getApplication();
        String uid = nemophila.getUid();
        String name = nemophila.getName();
        TextView myName = (TextView) findViewById(R.id.name);
        myName.setText(name);
        TextView id = (TextView) findViewById(R.id.id);
        id.setText(uid);

        // 「閉じる」ボタンを押したときにメイン画面へ遷移する
        Button cancelButton = (Button) findViewById(R.id.cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MyPageActivity.this, MapsActivity.class);
                startActivity(intent);
            }
        });

        //「フレンドリスト」ボタンを押したときにフレンドリスト画面へ遷移する
        Button friendButton = (Button) findViewById(R.id.friend);
        friendButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MyPageActivity.this, FriendActivity.class);
                startActivity(intent);

            }
        });

        // 「フレンド追加」ボタンを押したときにフレンド追加画面へ遷移する
        Button RequestedButton = (Button) findViewById(R.id.requested);
        RequestedButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MyPageActivity.this, RequestedActivity.class);
                startActivity(intent);
            }
        });

        // 「マイページ編集」ボタンを押したときにマイページ編集画面へ遷移する
        Button changeMyPageButton = (Button) findViewById(R.id.changeMyPage);
        changeMyPageButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MyPageActivity.this, MyPageEditorActivity.class);
                startActivity(intent);
            }

        });

        //getNameLiveDataとの通信により今のユーザーネームを表示する
        AccountViewModel accountViewModel = new ViewModelProvider(this).get(AccountViewModel.class);
        accountViewModel.getNameLiveData().observe(this, new Observer <String>() {
            @Override
            public void onChanged(String name) {
                TextView myName = (TextView) findViewById(R.id.name);
                myName.setText(name);
                nemophila.setName(name);
            }
        });

        /* ここからあんまわかっていない */
        RecyclerView rv = (RecyclerView) findViewById(R.id.postsList);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        List<MyPageActDataModel> MyPageActDataset = new ArrayList<>();
        MyPageActAdapter adapter = new MyPageActAdapter(MyPageActDataset);

        rv.setHasFixedSize(true);
        rv.setLayoutManager(llm);
        rv.setAdapter(adapter);

        accountViewModel.getAccountPosts(uid);
        accountViewModel.getAccountPostsLiveData().observe(this, new Observer <Collection<Post>>() {
            @Override
            public void onChanged(Collection<Post> Posts) {
                //MyPageActDatasetを一旦クリアし、LiveDataから受け取ったデータを一つずつセット
                MyPageActDataset.clear();
                for (Post post: Posts){
                    MyPageActDataModel data = new MyPageActDataModel();
                    data.setDate(post.getDate());
                    //rateはintで受け取った評価に対応した星の数のStringに変換してセット
                    int intRate = post.getRate();
                    String strRate = "";
                    for (int i = 0; i < 5; i++){
                        if(intRate > i ){
                            strRate += "★";
                        }else{
                            strRate += "☆";
                        }
                    }
                    data.setRate(strRate);
                    data.setGenre(post.getGenre());
                    data.setComment(post.getComment());
                    MyPageActDataset.add(data);
                }

                //RecyclerViewをonChangedが呼ばれるたび表示(アダプターを更新してRecyclerViewにセット)
                adapter.setList(MyPageActDataset);
                rv.setAdapter(adapter);
            }
        });
    }

    /* ここからMyPageActivityの内部クラス */

    // Adapter
    public class MyPageActAdapter extends RecyclerView.Adapter<MyPageActViewHolder> {

        private List<MyPageActDataModel> list;

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

        public MyPageActAdapter(List<MyPageActDataModel> list) {
            this.list = list;
        }

        @Override
        public MyPageActViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_my_page_activity_posts, parent,false);
            MyPageActViewHolder vh = new MyPageActViewHolder(inflate);
            return vh;
        }

        @Override
        public void onBindViewHolder(MyPageActViewHolder holder, int position) {
            holder.dateView.setText(list.get(position).getDate());
            holder.rateView.setText(list.get(position).getRate());
            holder.genreView.setText(list.get(position).getGenre());
            holder.commentView.setText(list.get(position).getComment());
        }

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

    // ViewHolder
    public class MyPageActViewHolder extends RecyclerView.ViewHolder {
        public TextView dateView;
        public TextView rateView;
        public TextView genreView;
        public TextView commentView;

        public MyPageActViewHolder(View itemView) {
            super(itemView);
            dateView = (TextView) itemView.findViewById(R.id.date);
            rateView = (TextView) itemView.findViewById(R.id.rate);
            genreView = (TextView) itemView.findViewById(R.id.genre);
            commentView = (TextView) itemView.findViewById(R.id.comment);
        }
    }

    // DataModel
    public class MyPageActDataModel {
        private String date;
        private String rate;
        private String genre;
        private String comment;

        public String getDate() {
            return date;
        }
        public void setDate(String date) {
            this.date = date;
        }

        public String getRate() {
            return rate;
        }
        public void setRate(String rate) {
            this.rate = rate;
        }

        public String getGenre() {
            return genre;
        }
        public void setGenre(String genre) {
            this.genre = genre;
        }

        public String getComment() {
            return comment;
        }
        public void setComment(String comment) {
            this.comment = comment;
        }
    }
}