syncParent戻り値型変更, 実装

This commit is contained in:
ろむねこ 2024-06-24 10:52:12 +09:00
parent 7382a7a36a
commit 66ba912170
Signed by: Fujimatsu
GPG Key ID: FA1F39A1BA37D168
2 changed files with 47 additions and 4 deletions

View File

@ -1,5 +1,9 @@
package one.nem.kidshift.data;
import java.util.concurrent.CompletableFuture;
import one.nem.kidshift.model.ParentModel;
/**
* データの同期など, ユーザーからの操作に基づかない処理を行う
*/
@ -12,6 +16,6 @@ public interface KSActions {
/**
* 親ユーザー情報同期
*/
void syncParent();
CompletableFuture<ParentModel> syncParent();
}

View File

@ -1,17 +1,30 @@
package one.nem.kidshift.data.impl;
import java.util.concurrent.CompletableFuture;
import javax.inject.Inject;
import one.nem.kidshift.data.KSActions;
import one.nem.kidshift.data.UserSettings;
import one.nem.kidshift.data.retrofit.KidShiftApiService;
import one.nem.kidshift.data.retrofit.model.parent.ParentInfoResponse;
import one.nem.kidshift.model.ParentModel;
import one.nem.kidshift.utils.KSLogger;
import retrofit2.Call;
import retrofit2.Response;
public class KSActionsImpl implements KSActions {
private UserSettings userSettings;
private KidShiftApiService kidShiftApiService;
private KSLogger logger;
@Inject
public KSActionsImpl(UserSettings userSettings) {
public KSActionsImpl(UserSettings userSettings, KidShiftApiService kidShiftApiService, KSLogger logger) {
this.userSettings = userSettings;
this.kidShiftApiService = kidShiftApiService;
this.logger = logger;
logger.setTag("KSActions");
}
@Override
@ -25,7 +38,33 @@ public class KSActionsImpl implements KSActions {
}
@Override
public void syncParent() {
public CompletableFuture<ParentModel> syncParent() {
logger.info("syncParent called and started");
return CompletableFuture.supplyAsync(() -> {
logger.info("fetching...");
Call<ParentInfoResponse> call = kidShiftApiService.getParentInfo();
try {
Response<ParentInfoResponse> response = call.execute();
if (!response.isSuccessful()) {
logger.error("Error fetching parent info: " + response.errorBody().string());
throw new RuntimeException("Error fetching parent info: " + response.errorBody().string());
}
ParentInfoResponse responseBody = response.body();
ParentModel parent = new ParentModel();
// TODO: 詰め替えをどこかにまとめる, 他のプロパティも処理する
parent.setInternalId(responseBody.getId());
parent.setEmail(responseBody.getEmail());
parent.setDisplayName(responseBody.getEmail()); // Workaround
logger.info("Parent fetched with status: " + response.code());
logger.debug("Parent: " + parent);
// Save to cache
userSettings.getCache().setParent(parent);
logger.info("Parent saved to cache");
return parent;
} catch (Exception e) {
logger.error("Error fetching parent info");
throw new RuntimeException(e);
}
});
}
}