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.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; 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.util.ArrayList; import java.util.Collection; import java.util.List; public class RequestedActivity extends AppCompatActivity { @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.getFriends(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); } 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; } @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()); } @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 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); } } // DataModel public static class RequestedUserModel { 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;} } }