package com.example.nemophila.viewmodels; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import com.example.nemophila.entities.Post; import com.example.nemophila.resources.AccountsRest; import com.example.nemophila.resources.FriendsRest; import java.util.Collection; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; public class FriendViewModel extends ViewModel { // フィールド private final Retrofit retrofit; private final FriendsRest friendsRest; // ライブデータ private final MutableLiveData<String> friendsLiveData; private final MutableLiveData<String> requestedLiveData; private final MutableLiveData<Collection<Post>> requestingLiveData; // コンストラクタ public FriendViewModel() { this.retrofit = new Retrofit.Builder() .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/nemophila/") .addConverterFactory(JacksonConverterFactory.create()) .build(); this.friendsRest = retrofit.create(FriendsRest.class); this.friendsLiveData = new MutableLiveData<>(); this.requestedLiveData = new MutableLiveData<>(); this.requestingLiveData = new MutableLiveData<>(); } // ライブデータの取得(ゲッター) public MutableLiveData<String> getFriendsLiveData() { return friendsLiveData; } public MutableLiveData<String> getRequestedLiveData() { return requestedLiveData; } public MutableLiveData<Collection<Post>> getRequestingLiveData() { return requestingLiveData; } public void searchFriend(String uid,String fid, String token) { Call<Void> call = friendsRest.putFriend(uid,fid,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("ResponseError"); } } @Override public void onFailure(Call<Void> call, Throwable t) { System.out.println(" NetworkError" + t); } }); } }