Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / viewmodels / AccountViewModel.java
package com.example.nemophila.viewmodels;

import com.example.nemophila.entities.Account;
import com.example.nemophila.entities.Post;
import com.example.nemophila.resources.AccountsRest;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.Collection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class AccountViewModel extends ViewModel {
    // フィールド
    private final Retrofit retrofit;
    private final AccountsRest accountsRest;
    // ライブデータ
    private final MutableLiveData<Account> accountLiveData;
    private final MutableLiveData<Collection<Post>> accountPostsLiveData;

    // コンストラクタ
    public AccountViewModel() {
        this.retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/nemophila/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        this.accountsRest = retrofit.create(AccountsRest.class);
        this.accountLiveData = new MutableLiveData<>();
        this.accountPostsLiveData = new MutableLiveData<>();
    }

    // ライブデータの取得(ゲッター)
    public MutableLiveData<Account> getAccountLiveData() {
        return accountLiveData;
    }
    public MutableLiveData<Collection<Post>> getAccountPostsLiveData() { return accountPostsLiveData; }

    // 対象のアカウント情報の取得
    public void getAccount(String uid) {
        Call<Account> call = accountsRest.getAccount(uid);

        call.enqueue(new Callback<Account>() {
            @Override
            public void onResponse(Call<Account> call, Response<Account> response) {
                if (response.isSuccessful()) {
                    accountLiveData.setValue(response.body());
                } else {
                    System.out.println("response error");
                }
            }
            @Override
            public void onFailure(Call<Account> call, Throwable t) {
                System.out.println("correspondence error");
            }
        });
    }

    // 対象のアカウント情報の削除
    public void deleteAccount(String uid) {
        Call<Void> call = accountsRest.deleteAccount(uid);

        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                if (response.isSuccessful()) {
                    System.out.println("successful");
                } else {
                    System.out.println("response error");
                }
            }
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                System.out.println("correspondence error");
            }
        });
    }

    // 対象のアカウントパスワードの変更
    public void changePw(String uid, String oldPw, String newPw, String token) {
        Call<Void> call = accountsRest.changePw(uid, oldPw, newPw, token);

        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                if (response.isSuccessful()) {
                    System.out.println("successful");
                } else {
                    System.out.println("response error");
                }
            }
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                System.out.println("correspondence error");
            }
        });
    }

    // 対象のアカウント名の変更
    public void changeName(String uid, String name, String token) {
        Call<Void> call = accountsRest.changeName(uid, name, token);

        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                if (response.isSuccessful()) {
                    System.out.println("successful");
                } else {
                    System.out.println("response error");
                }
            }
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                System.out.println("correspondence error");
            }
        });
    }

    // 対象のアカウントがした投稿の全取得
    public void getAccountPosts(String uid) {
        Call<Collection<Post>> call = accountsRest.getAccountPosts(uid);

        call.enqueue(new Callback<Collection<Post>>() {
            @Override
            public void onResponse(Call<Collection<Post>> call, Response<Collection<Post>> response) {
                if (response.isSuccessful()) {
                    accountPostsLiveData.setValue(response.body());
                } else {
                    System.out.println("response error");
                }
            }
            @Override
            public void onFailure(Call<Collection<Post>> call, Throwable t) {
                System.out.println("correspondence error");
            }
        });
    }

    // 対象のアカウントがした投稿の削除
    public void deleteAccountPost(String sid, String uid, String pid, String token) {
        Call<Void> call = accountsRest.deletePost(sid, uid, pid, token);

        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                if (response.isSuccessful()) {
                    System.out.println("successful");
                } else {
                    System.out.println("response error");
                }
            }
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                System.out.println("correspondence error");
            }
        });
    }
}