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.app.AlertDialog;
import android.content.DialogInterface;
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.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

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) this.getApplication();
        String uid = nemophila.getUid();
        String name = nemophila.getName();
        String token = nemophila.getToken();
        TextView myName = (TextView) findViewById(R.id.name);
        myName.setText(name);
        TextView id = (TextView) findViewById(R.id.id);
        id.setText(uid);

        // 「閉じる」ボタンを押したときにメイン画面へ遷移する
        Button closeButton = (Button) findViewById(R.id.close);
        closeButton.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 requestingButton = (Button) findViewById(R.id.requesting);
        requestingButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MyPageActivity.this, RequestingActivity.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 myPageEditButton = (Button) findViewById(R.id.myPageEdit);
        myPageEditButton.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 = findViewById(R.id.postsList);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        List<MyPageActDataModel> myPageActDataset = new ArrayList<>();
        MyPageActAdapter adapter = new MyPageActAdapter(myPageActDataset) {
            @Override
            void onDeleteButtonClick(View view, int position, MyPageActDataModel dataModel) {
                new AlertDialog.Builder(MyPageActivity.this)
                        .setTitle("警告")
                        .setMessage("この投稿を削除しますか?\n削除すると元に戻せなくなります!")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MyPageActivity.this, "投稿を削除しました。", Toast.LENGTH_SHORT).show();
                                System.out.println("投稿を削除しました。");
                                accountViewModel.deleteAccountPost(uid, dataModel.getPid(), token);
                            }
                        })
                        .setNegativeButton("No", null)
                        .create()
                        .show();
            }
        };

        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());
                    data.setPid(post.getPid());
                    myPageActDataset.add(data);
                }

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

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

    // Adapter
    abstract 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);

            vh.deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = vh.getBindingAdapterPosition();
                    onDeleteButtonClick(view, position, list.get(position));
                }
            });

            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(); }

        abstract void onDeleteButtonClick(View view, int position, MyPageActDataModel dataModel);
    }

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

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

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

        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;
        }

        public String getPid() { return pid; }
        public void setPid(String sid) { this.pid = pid; }
    }
}