package com.example.citrusclient.viewmodels; import com.example.citrusclient.Citrus; import com.example.citrusclient.rest.AccountsRest; import java.net.HttpCookie; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; public class SettingsViewModel extends ViewModel { private final Retrofit retrofit; private final AccountsRest accountsRest; private final MutableLiveData<String> accountColorLiveData; //通信レスポンスの画面表示用のライブデーター //PW変更時の通信レスポンス結果格納用 private final MutableLiveData<Integer> response_changePw_LiveData; //アカウント削除時の通信レスポンス結果格納用 private final MutableLiveData<Integer> response_deleteAccount_LiveData; //アカウントID変更はやるのか不明なので一旦保留 public SettingsViewModel(){ 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); this.accountColorLiveData = new MutableLiveData<>(); this.response_changePw_LiveData = new MutableLiveData<>(); this.response_deleteAccount_LiveData = new MutableLiveData<>(); } public MutableLiveData<String> getAccountColorLiveData(){return accountColorLiveData;} public MutableLiveData<Integer> getResponse_changePw(){return response_changePw_LiveData;} public MutableLiveData<Integer>getResponse_deleteAccount(){return response_deleteAccount_LiveData;} public void loadAccountColor(String accountId){ Call<String> call= accountsRest.getAccountColor(accountId); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { String strAccountColor = response.body(); if (response.isSuccessful()){ System.out.println("success"); accountColorLiveData.setValue(strAccountColor); }else{ System.out.println("fail"); } } @Override public void onFailure(Call<String> call, Throwable t) { System.out.println("NetworkError"+t); } }); } //なんちゃって通信ver1 //PW変更時のサーバ通信プログラム public void changePW(String accountId, String old_password, String new_password,String token){ Call<String> call = accountsRest.changePW(accountId, old_password, new_password, token); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { if (response.isSuccessful()) { System.out.println("success"); response_changePw_LiveData.setValue(204); //成功処理 } else { System.out.println("fail"); response_changePw_LiveData.setValue(404); } } @Override public void onFailure(Call<String> call, Throwable t) { System.out.println("NetWorkError"+t); response_changePw_LiveData.setValue(404); } }); } //通信ver2 //アカウント削除 public void deleteId(String accountId, String token, String password){ Call<String> call =accountsRest.deleteId(accountId,token,password); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { if(response.isSuccessful()){ System.out.println("success"); //成功処理 response_deleteAccount_LiveData.setValue(204); }else{ System.out.println("fail"); //もしコンソール表記をするならこの下に処理を記載 response_deleteAccount_LiveData.setValue(404); } } @Override public void onFailure(Call<String> call, Throwable t) { System.out.println("NetWorkError"+t); //もしコンソール表記をするならこの下に処理を記載 response_deleteAccount_LiveData.setValue(404); } }); } //通信Ver3 //色変更 public void changeColor(String accountId, String accountColor, String token){ Call<String> call =accountsRest.changeColor(accountId, accountColor, token); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { if(response.isSuccessful()){ System.out.println("success"); }else{ System.out.println("fail"); } } @Override public void onFailure(Call<String> call, Throwable t) { System.out.println("NetWorkError"+t); } }); } //通信Ver4 //アカウント名変更 public void changeAccount(String accountId, String accountColor, String token){ Call<String> call =accountsRest.changeColor(accountId, accountColor, token); call.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { if(response.isSuccessful()){ System.out.println("success"); }else{ System.out.println("fail"); } } @Override public void onFailure(Call<String> call, Throwable t) { System.out.println("NetWorkError"+t); } }); } }