diff --git a/app/src/main/java/com/example/nemophila/RequestedActivity.java b/app/src/main/java/com/example/nemophila/RequestedActivity.java index db2d1d9..996393c 100644 --- a/app/src/main/java/com/example/nemophila/RequestedActivity.java +++ b/app/src/main/java/com/example/nemophila/RequestedActivity.java @@ -4,9 +4,12 @@ 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; @@ -14,6 +17,7 @@ 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; @@ -24,10 +28,11 @@ 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(); @@ -36,9 +41,6 @@ String uid = nemophila.getUid(); String token = nemophila.getToken(); - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_requested); - Button returnButton = (Button)findViewById(R.id.CancelButton); returnButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ @@ -48,11 +50,50 @@ }); RecyclerView rv = findViewById(R.id.requestedList); - RequestedAdapter adapter = new RequestedAdapter(requestedDataSet); + 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(), 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); - rv.setAdapter(adapter); friendViewModel.getRequested(uid); friendViewModel.getRequestedLiveData().observe(this, new Observer>() { @@ -72,7 +113,7 @@ } //Adapter - public class RequestedAdapter extends RecyclerView.Adapter { + abstract public class RequestedAdapter extends RecyclerView.Adapter { private List list; public RequestedAdapter(List list) { this.list = list; @@ -86,6 +127,23 @@ 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; } @@ -98,15 +156,22 @@ 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); } }