ログポイント追加, 確実に非同期実行されるように?

This commit is contained in:
ろむねこ 2024-07-05 13:19:58 +09:00
parent c25e9837f8
commit 268c4d3bd3
Signed by: Fujimatsu
GPG Key ID: FA1F39A1BA37D168

View File

@ -89,11 +89,17 @@ public class SettingMainFragment extends Fragment {
public void onFailed(String message) { public void onFailed(String message) {
} }
}).thenAccept(parentModel -> { }).thenAcceptAsync(parentModel -> {
username.setText(parentModel.getName() != null ? parentModel.getName() : "親の名前"); try {
userMailAddress.setText(parentModel.getEmail() != null ? parentModel.getEmail() : "親のアドレス"); Thread.sleep(5000); // デバッグのための遅延実際のアプリでは削除
} catch (InterruptedException e) {
e.printStackTrace();
}
requireActivity().runOnUiThread(() -> {
username.setText(parentModel.getName() != null ? parentModel.getName() : "親の名前");
userMailAddress.setText(parentModel.getEmail() != null ? parentModel.getEmail() : "親のアドレス");
});
}); });
} }
/** /**
@ -118,20 +124,18 @@ public class SettingMainFragment extends Fragment {
public void onFailed(String message) { public void onFailed(String message) {
} }
}).thenAccept(childModels -> { }).thenAcceptAsync(childModels -> {
mainAdapter.setChildDataList(childModels); mainAdapter.setChildDataList(childModels);
requireActivity().runOnUiThread(() -> { requireActivity().runOnUiThread(() -> {
mainAdapter.notifyDataSetChanged(); mainAdapter.notifyDataSetChanged();
}); });
}); });
} }
/** /**
* ユーザー情報を更新するラッパー * ユーザー情報を更新するラッパー
*/ */
private void updateInfo() { private CompletableFuture<Void> updateInfo() {
CompletableFuture<Void> updateParent = updateParentInfo(); CompletableFuture<Void> updateParent = updateParentInfo();
CompletableFuture<Void> updateChildList = updateChildInfo(); CompletableFuture<Void> updateChildList = updateChildInfo();
@ -140,14 +144,19 @@ public class SettingMainFragment extends Fragment {
swipeRefreshLayout.setRefreshing(true); swipeRefreshLayout.setRefreshing(true);
logger.debug("アップデート開始"); logger.debug("アップデート開始");
updateParent.thenCombine(updateChildList, (res1, res2) -> null).thenRun(() -> { return updateParent.thenCombineAsync(updateChildList, (res1, res2) -> null)
logger.debug("アップデート完了"); .thenRunAsync(() -> {
swipeRefreshLayout.setRefreshing(false); requireActivity().runOnUiThread(() -> {
}).exceptionally(e -> { logger.debug("アップデート完了");
logger.error("アップデート失敗: " + e.getMessage()); swipeRefreshLayout.setRefreshing(false);
swipeRefreshLayout.setRefreshing(false); });
return null; }).exceptionally(e -> {
}); requireActivity().runOnUiThread(() -> {
logger.error("アップデート失敗: " + e.getMessage());
swipeRefreshLayout.setRefreshing(false);
});
return null;
});
} }
@Override @Override
@ -155,28 +164,42 @@ public class SettingMainFragment extends Fragment {
Bundle savedInstanceState) { Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
logger.debug("Point 1");
View view = inflater.inflate(R.layout.fragment_setting_main, container, false); View view = inflater.inflate(R.layout.fragment_setting_main, container, false);
logger.debug("Point 2");
// ビューの取得 // ビューの取得
username = view.findViewById(R.id.username); username = view.findViewById(R.id.username);
userMailAddress = view.findViewById(R.id.useradress); userMailAddress = view.findViewById(R.id.useradress);
swipeRefreshLayout = view.findViewById(R.id.swipe_refresh_layout); swipeRefreshLayout = view.findViewById(R.id.swipe_refresh_layout);
RecyclerView recyclerView = view.findViewById(R.id.childrecyclerview); RecyclerView recyclerView = view.findViewById(R.id.childrecyclerview);
logger.debug("Point 3");
// RecyclerViewの設定 // RecyclerViewの設定
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext()); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager); recyclerView.setLayoutManager(layoutManager);
mainAdapter = new SettingAdapter(); mainAdapter = new SettingAdapter();
recyclerView.setAdapter(mainAdapter); recyclerView.setAdapter(mainAdapter);
logger.debug("Point 4");
// ユーザー情報の更新(初回) // ユーザー情報の更新(初回)
updateInfo(); updateInfo().thenRun(() -> {
logger.debug("ユーザー情報の更新完了");
});
logger.debug("Point 5");
// スワイプリフレッシュのリスナー // スワイプリフレッシュのリスナー
swipeRefreshLayout.setOnRefreshListener(() -> { swipeRefreshLayout.setOnRefreshListener(() -> {
updateInfo(); // ユーザー情報の更新 updateInfo(); // ユーザー情報の更新
}); });
logger.debug("Point 6");
// ダイアログの設定 // ダイアログの設定
LayoutInflater dialogInflater = requireActivity().getLayoutInflater(); LayoutInflater dialogInflater = requireActivity().getLayoutInflater();
@ -184,6 +207,8 @@ public class SettingMainFragment extends Fragment {
View childListItemView = inflater.inflate(R.layout.list_item_child_name_list, container, false); View childListItemView = inflater.inflate(R.layout.list_item_child_name_list, container, false);
logger.debug("Point 7");
mainAdapter.setLoginButtonCallback(new SettingAdapter.LoginButtonCallback() { mainAdapter.setLoginButtonCallback(new SettingAdapter.LoginButtonCallback() {
@Override @Override
public void onLoginButtonClicked(String childId) { public void onLoginButtonClicked(String childId) {
@ -208,6 +233,8 @@ public class SettingMainFragment extends Fragment {
} }
}); });
logger.debug("Point 8");
// int loginCode = childData.issueLoginCode("543256").join(); // int loginCode = childData.issueLoginCode("543256").join();
// TextView loginCodeTextView = addChildDialogView.findViewById(R.id.loginCode); // TextView loginCodeTextView = addChildDialogView.findViewById(R.id.loginCode);
// new StringBuilder(Integer.toString(loginCode)).insert(3,"-"); // new StringBuilder(Integer.toString(loginCode)).insert(3,"-");
@ -221,12 +248,14 @@ public class SettingMainFragment extends Fragment {
.setNeutralButton("閉じる", null); .setNeutralButton("閉じる", null);
builder.create(); builder.create();
// //
logger.debug("Point 9");
// childListItemView.findViewById(R.id.loginButton).setOnClickListener(v -> { // childListItemView.findViewById(R.id.loginButton).setOnClickListener(v -> {
// builder.show(); // builder.show();
// }); // });
logger.debug("Point 10");
// ダイアログの表示 // ダイアログの表示
@ -260,6 +289,8 @@ public class SettingMainFragment extends Fragment {
} }
}); });
logger.debug("Point 11");
return view; return view;
} }