diff --git a/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java b/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java index 9ec5406..e092f40 100644 --- a/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java +++ b/app/src/main/java/com/example/citrusclient/views/SignUpActivity.java @@ -1,19 +1,35 @@ package com.example.citrusclient.views; +import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; +import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.jackson.JacksonConverterFactory; +import com.example.citrusclient.Citrus; import com.example.citrusclient.R; +import com.example.citrusclient.rest.AccountsRest; + +import org.w3c.dom.Text; public class SignUpActivity extends AppCompatActivity { + private Retrofit retrofit; + private AccountsRest accountsRest; + private Citrus citrus; + + //画面起動時 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -28,6 +44,26 @@ }); + //citrusと連携 + citrus = (Citrus) this.getApplication(); + + //端末に情報が登録されているとき + if ((citrus.getToken() != null)&&(citrus.getAccountId() != null)){ + System.out.println("アカウント登録済み"); + //MainActivityに遷移 + Intent intent = new Intent(SignUpActivity.this,MainActivity.class); + startActivity(intent); + } + + //通信の初期化 + this.retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + this.accountsRest = retrofit.create(AccountsRest.class); + + + findViewById(R.id.button_signup).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -37,11 +73,49 @@ EditText editpw = (EditText) findViewById(R.id.put_pw); String pw = editpw.getText().toString().trim(); - if(!id.isEmpty() && !pw.isEmpty() && !id.trim().isEmpty() && !pw.trim().isEmpty() ){ - //通信 + //入力欄が空欄の時 + if(id.isEmpty() && pw.isEmpty() && id.trim().isEmpty() && pw.trim().isEmpty()){ + System.out.println("不適切入力"); + ((TextView)findViewById(R.id.textView_respons)).setText("適切に入力してください"); + + }else if(!id.isEmpty() && !pw.isEmpty() && !id.trim().isEmpty() && !pw.trim().isEmpty() ){ + //入力欄が全て適切に入力された場合の処理 + Call call = accountsRest.signup(id,pw); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + System.out.println("通信成功"); + //端末にtoken.id登録 + String token = response.body(); + citrus.setToken(token); + citrus.setAccountId(id); + + //画面遷移 + Intent intent = new Intent(SignUpActivity.this,MainActivity.class); + startActivity(intent); + + }else{ + //通信可能(ただしエラーが発生した場合) + //404がPW入力されてないときだけど、前で定義しているからいらないよね..? + System.out.println("通信可能"); + if(response.code()==409){ + ((TextView)findViewById(R.id.textView_respons)).setText(("入力したアカウントIDは既に使用されています")); + } + } + } + //通信失敗 + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("通信失敗"); + System.out.println(t); + } + }); } } }); + + } } \ No newline at end of file