Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / RequestedActivity.java
m-mifune on 12 Oct 2023 9 KB layoutの変更
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.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
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 android.widget.Toast;

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

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RequestedActivity extends AppCompatActivity {

    private final ExecutorService executor = Executors.newSingleThreadExecutor();
    private final Handler handler = new Handler();
    private String IconUrlString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_requested);

        //初期化
        FriendViewModel friendViewModel = new ViewModelProvider(this).get(FriendViewModel.class);
        Nemophila nemophila = (Nemophila) this.getApplication();
        List<RequestedUserModel> requestedDataSet = new ArrayList<>();
        LinearLayoutManager llm = new LinearLayoutManager(this);
        String uid = nemophila.getUid();
        String token = nemophila.getToken();

        Button returnButton = (Button)findViewById(R.id.CancelButton);
        returnButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(getApplication(), MyPageActivity.class);
                startActivity(intent);
            }
        });

        RecyclerView rv = findViewById(R.id.requestedList);
        RequestedAdapter adapter = new RequestedAdapter(requestedDataSet) {
            @Override
            void onOkButtonClick(View view, int position, RequestedUserModel userModel) {
                new AlertDialog.Builder(RequestedActivity.this)
                        .setTitle("確認")
                        .setMessage("フレンド申請を承諾します")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(RequestedActivity.this, userModel.getName(), Toast.LENGTH_SHORT).show();
                                System.out.println("承諾ボタンが押されました: " + userModel.getName());
                                friendViewModel.putFriend(uid, userModel.getId() ,userModel.getName(), token);
                            }
                        })
                        .setNegativeButton("Cancel", null)
                        .create()
                        .show();
            }

            @Override
            void onNoButtonClick(View view, int position, RequestedUserModel userModel) {
                new AlertDialog.Builder(RequestedActivity.this)
                        .setTitle("警告")
                        .setMessage("フレンド申請を拒否します")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(RequestedActivity.this, userModel.getName(), Toast.LENGTH_SHORT).show();
                                System.out.println("拒否ボタンが押されました: " + userModel.getName());
                                friendViewModel.deleteRequested(uid, userModel.getId(), token);
                            }
                        })
                        .setNegativeButton("Cancel", null)
                        .create()
                        .show();
            }
        };

        RecyclerView.ItemDecoration itemDecoration =
                new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);

        rv.addItemDecoration(itemDecoration);
        rv.setHasFixedSize(true);
        rv.setLayoutManager(llm);

        friendViewModel.getRequested(uid);
        friendViewModel.getRequestedLiveData().observe(this, new Observer<Collection<AccountNameJson>>() {
            @Override
            public void onChanged(Collection<AccountNameJson> accountNameJson) {
                requestedDataSet.clear();
                for (AccountNameJson acj: accountNameJson) {
                    RequestedUserModel data = new RequestedUserModel();
                    data.setId(acj.getUid());
                    data.setName(acj.getName());
                    requestedDataSet.add(data);

                    IconUrlString = "http://nitta-lab-www.is.konan-u.ac.jp/nemophila-data/test01.jpg";
                    executor.execute(() -> {
                        try {
                            URL url = new URL(IconUrlString);
                            HttpURLConnection con = (HttpURLConnection) url.openConnection();
                            con.connect();

                            InputStream is = con.getInputStream();

                            Bitmap bitmap = BitmapFactory.decodeStream(is);
                            handler.post(() -> {
                                data.setIcon(bitmap);
                                adapter.notifyItemChanged(adapter.getPosition(data));
                            });
                            is.close();
                            con.disconnect();
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    });
                }
                adapter.setList(requestedDataSet);
                rv.setAdapter(adapter);
            }


        });
    }

    //Adapter
    abstract public class RequestedAdapter extends RecyclerView.Adapter<RequestedViewHolder> {
        private List<RequestedUserModel> list;
        public RequestedAdapter(List<RequestedUserModel> list) {
            this.list = list;
        }
        public void setList(List<RequestedUserModel> list) {
            this.list = list;
        }
        public int getPosition(RequestedActivity.RequestedUserModel d) { return list.indexOf(d); }

        @NonNull
        @Override
        public RequestedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_requested_activity, parent,false);
            RequestedViewHolder vh = new RequestedViewHolder(inflate);

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

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

            return vh;
        }

        @Override
        public void onBindViewHolder(@NonNull RequestedViewHolder holder, int position) {
            holder.nameView.setText(list.get(position).getName());
            holder.iconView.setImageBitmap(list.get(position).getIcon());
        }

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

        abstract void onOkButtonClick(View view, int position, RequestedUserModel userModel);
        abstract void onNoButtonClick(View view, int position, RequestedUserModel userModel);
    }

    //ViewHolder
    public class RequestedViewHolder extends RecyclerView.ViewHolder {
        public TextView nameView;
        public Button okButton;
        public Button noButton;
        public ImageView iconView;

        public RequestedViewHolder(@NonNull View view) {
            super(view);
            nameView = (TextView) view.findViewById(R.id.textView3);
            okButton = view.findViewById(R.id.okButton);
            noButton = view.findViewById(R.id.noButton);
            iconView = view.findViewById(R.id.imageView);
        }
    }
    
    // DataModel
    public static class RequestedUserModel {
        private String name;
        private String id;
        private Bitmap icon;

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

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