Newer
Older
IrisClient / app / src / main / java / org / ntlab / irisclient / EnterRoomActivity.java
package org.ntlab.irisclient;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.google.android.material.snackbar.Snackbar;

import org.ntlab.irisclient.entities.MemberJson;
import org.ntlab.irisclient.entities.RoomJson;
import org.ntlab.irisclient.resources.RoomsRest;

import java.util.List;


public class EnterRoomActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter_room);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.hide();
        }

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/iris/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        final RoomsRest roomsRest = retrofit.create(RoomsRest.class);

        ImageButton imageButton = (ImageButton) findViewById(R.id.backButton); // view経由でimageButtonを探す
        imageButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(v.getContext(), MainActivity.class);
                startActivity(i);
            }
        });

        Button nextButton = findViewById(R.id.EntryRoomButton);
        nextButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                EditText nicknameForm = findViewById(R.id.nicknameEditText);
                EditText roomIdForm = findViewById(R.id.roomIdEditText);

                String name = nicknameForm.getText().toString();
                String rid = roomIdForm.getText().toString();

                //ニックネームまたは部屋番号が入力されていない場合エラーメッセージを表示する
                if (name.isEmpty() || rid.isEmpty()) {
                    nicknameForm.setError("ニックネームを入力されていません");
                    roomIdForm.setError("部屋番号を入力されていません");
                } else {
                    Call<RoomJson> call = roomsRest.postMembers(rid,name);
                    call.enqueue(new Callback<RoomJson>() {
                        @Override
                        public void onResponse(Call<RoomJson> call, Response<RoomJson> response) {
                            if(response.isSuccessful()){
                                Intent intent = new Intent(EnterRoomActivity.this,MemberRoomActivity.class);
                                intent.putExtra("nickname", name);
                                startActivity(intent);
                            }else {
                                Snackbar.make(v, "部屋に同名のプレイヤーがいるか、満席です。", Snackbar.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<RoomJson> call, Throwable t){

                        }
                    });
                }
            }
        });

    }

}