Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / viewmodels / SettingsViewModel.java
package com.example.citrusclient.viewmodels;

import com.example.citrusclient.rest.AccountsRest;

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> error_changePw_LiveData;
    //アカウント削除時の通信レスポンス結果格納用
    private final MutableLiveData<Integer> error_deleteAccount_LiveData;
    //アカウントID変更
    private final MutableLiveData<Integer> error_changeAccount_LiveData;



    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.error_changePw_LiveData = new MutableLiveData<>();
        this.error_deleteAccount_LiveData = new MutableLiveData<>();
        this.error_changeAccount_LiveData = new MutableLiveData<>();
    }
    public MutableLiveData<String> getAccountColorLiveData(){return accountColorLiveData;}
    public MutableLiveData<Integer> getError_changePw_LiveData(){return error_changePw_LiveData;}
    public MutableLiveData<Integer> getError_deleteAccount_LiveData(){return error_deleteAccount_LiveData;}
    public MutableLiveData<Integer> getError_changeAccount_LiveData(){return error_changeAccount_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");
                    error_changePw_LiveData.setValue(204);

                    //成功処理
                } else {
                    System.out.println("fail");
                    error_changePw_LiveData.setValue(404);
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("NetWorkError"+t);
                error_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");
                    //成功処理
                    error_deleteAccount_LiveData.setValue(204);
                }else{
                    System.out.println("fail");
                    //もしコンソール表記をするならこの下に処理を記載
                    error_deleteAccount_LiveData.setValue(404);
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("NetWorkError"+t);
                //もしコンソール表記をするならこの下に処理を記載
                error_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 new_account_id, String old_password, String token){
        Call<String> call =accountsRest.changeAccount(accountId, new_account_id, old_password, token);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful()){
                    System.out.println("success");
                    error_changeAccount_LiveData.setValue(204);
                }else{
                    System.out.println("fail");
                    error_changeAccount_LiveData.setValue(404);
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("NetWorkError"+t);
                error_changeAccount_LiveData.setValue(404);
            }
        });
    }


}