Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / viewmodels / SettingsViewModel.java
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;


    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<>();
    }
    public MutableLiveData<String> getAccountColorLiveData(){return accountColorLiveData;}

    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");
                    //成功処理
                } else {
                    System.out.println("fail");
                }
            }

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

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("NetWorkError"+t);
                //もしコンソール表記をするならこの下に処理を記載
            }
        });
    }

    //通信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);
            }
        });
    }

}