package org.ntlab.irisclient;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
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.app.AlertDialog;
import android.widget.EditText;
import org.ntlab.irisclient.entities.RoomJson;
import org.ntlab.irisclient.resources.RoomsRest;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
public class CreateRoomActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_room);
//----------------------------------------------------------------------------------------------------------------------------------
//サーバーとの通信の初期化
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/iris/")
.addConverterFactory(JacksonConverterFactory.create())
.build();
final RoomsRest roomRest = retrofit.create(RoomsRest.class);
//------------------------------------------------------------------------------------------------------------------------------------------
//アクションバーの非表示
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
//------------------------------------------------------------------------------------------------------------------------------------------
//バックボタンを押した場合、前の画面に遷移
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.CreateRoomButton);
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText text = (EditText) findViewById(R.id.nicknameEditText);
String nickname = text.getText().toString();
//ニックネームが入力されていない場合エラーメッセージを表示する
if (text.getText().toString().isEmpty()) {
text.setError("ニックネームを入力されていません");
//ニックネームが入力されていれば次の画面へ
} else {
//サーバーとの通信のために呼び出す
Call<RoomJson> call = roomRest.makeRooms(nickname);
call.enqueue(new Callback<RoomJson>() {
//onResponseで成功
public void onResponse(Call<RoomJson> call, Response<RoomJson> response) {
if (response.isSuccessful()) {
//iris.setPreferenceName(nickname);
//次の画面へ遷移
Intent intent = new Intent(CreateRoomActivity.this,OwnerRoomActivity.class);
startActivity(intent);
}
}
//onFailureで失敗
public void onFailure(Call<RoomJson> call, Throwable t) {
}
});
}
}
});
}
}