ルート作成時のダイアログ関連の修正 #34

Merged
murakumo merged 1 commits from murakumo_ダイアログ祭り into master 2024-07-05 00:52:13 +00:00

View File

@ -87,12 +87,10 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
Log.d("MainActivity","geocodeAddress");
//TODO:classIdの初期値を取得
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
try {
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable")
.build();
AppDatabase db = getDatabaseInstance();
SetUpTableDao setUpTableDao = db.setUpTableDao();
classId = setUpTableDao.getClassId();
firestoreReception.getDocumentsByClassId(classId);
@ -190,95 +188,83 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
//ルート作成の非同期処理
private void fetchDataAndCreateRoute() {
//非同期処理の開始
ExecutorService executor = Executors.newSingleThreadExecutor();
CountDownLatch latch = new CountDownLatch(2);
// タスク1: ローカルDBから生徒数を取得してtotalStudentと比較
executor.execute(() -> {
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build();
SetUpTableDao setUpTableDao = db.setUpTableDao();
Log.d("MainActivity", "db" + setUpTableDao.getAll());
AppDatabase db = getDatabaseInstance(); SetUpTableDao setUpTableDao = db.setUpTableDao();
int totalStudent = setUpTableDao.getTotalStudent();
int myDataListSize = firestoreReception.getMyDataListSize();
//総生徒数と提出済みになっている生徒の数が一致するかの確認
runOnUiThread(() -> {
if (totalStudent != myDataListSize) {
showRouteCreationDialog(latch);
//未提出者がいることの警告ダイアログ
showRouteCreationDialog();
} else {
latch.countDown();
//ルート作成
createRoute(executor);
}
});
});
// タスク2: ルート作成を行う
// `fetchDataAndCreateRoute`メソッド内ではshutdownを呼び出さない
}
private void showRouteCreationDialog() {
new AlertDialog.Builder(MainActivity.this)
.setTitle("警告")
.setMessage("人数が足りてませんがそれでもルート作成を行いますか?")
.setPositiveButton("OK", (dialog, which) -> {
// 新しいExecutorServiceを作成してタスクを実行
ExecutorService dialogExecutor = Executors.newSingleThreadExecutor();
createRoute(dialogExecutor);
dialogExecutor.shutdown();
})
.setNegativeButton("Cancel", (dialog, which) -> {
dialog.dismiss();
})
.show();
}
private void createRoute(ExecutorService executor) {
executor.execute(() -> {
List<MyDataClass> myDataList = null;
while (myDataList == null) {
myDataList = firestoreReception.getMyDataList();
try {
Thread.sleep(3000);
Log.d("MainActivity", "myDataList" + myDataList.size());
} catch (InterruptedException e) {
throw new RuntimeException(e);
Thread.currentThread().interrupt();
return;
}
}
Log.d("MainActivity", "myDataList" + myDataList.size());
CreateRoot createRoot = new CreateRoot(MainActivity.this);
Boolean notDuplicates = createRoot.receiveData(myDataList,getApplicationContext());
latch.countDown();
//final宣言することによってスレッドセーフになる(ラムダ式内で使えるようにする)
final List<MyDataClass> finalMyDataList = myDataList;
CreateRoot createRoot = new CreateRoot(MainActivity.this);
Boolean notDuplicates = createRoot.receiveData(finalMyDataList, getApplicationContext());
runOnUiThread(() -> {
if (notDuplicates) {
Log.d("MainActivity", "スケジュール作成成功");
} else {
showErrorDialog(latch, myDataList);
}
});
new Thread(() -> {
List<MyDataClass> myDataList = firestoreReception.getMyDataList();
try {
latch.await(); // Both tasks must call countDown() before this returns
runOnUiThread(() -> {
Intent toRoot = new Intent(MainActivity.this, Maps.class);
startActivity(toRoot);
} else {
//保護者の重複による警告ダイアログ
showErrorDialog(finalMyDataList);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
// createRouteの最後にexecutorをシャットダウン
executor.shutdown();
});
}
//ルート作成のダイアログ
private void showRouteCreationDialog(CountDownLatch latch) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("警告")
.setMessage("人数が足りてませんがそれでもルート作成を行いますか?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
latch.countDown();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
public void showErrorDialog(CountDownLatch latch, List<MyDataClass> myDataList) {
private void showErrorDialog(List<MyDataClass> myDataList) {
List<Integer> studentNumbers = new ArrayList<>();
for (int i = 0; i < myDataList.size(); i++) {
if (myDataList.get(i).getSchedule() == 0) {
studentNumbers.add(myDataList.get(i).getStudentNumber());
for (MyDataClass data : myDataList) {
if (data.getSchedule() == 0) {
studentNumbers.add(data.getStudentNumber());
}
}
StringBuilder message = new StringBuilder("保護者の重複が重大でルート作成ができません。調整してください。\n出席番号: ");
@ -291,15 +277,19 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
new AlertDialog.Builder(MainActivity.this)
.setTitle("警告")
.setMessage(message.toString())
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
.setPositiveButton("OK", (dialog, which) -> {
dialog.dismiss();
}
})
.show();
}
private AppDatabase getDatabaseInstance() {
return Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build();
}
//提出状況の取得
private ArrayList<SubmissionStudent> getSubmissionStudents() {
ArrayList<SubmissionStudent> submissionStudents = new ArrayList<>();
@ -310,7 +300,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
executor.execute(() -> {
// 1. Roomデータベースから全生徒数を取得
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build();
AppDatabase db = getDatabaseInstance();
SetUpTableDao setUpTableDao = db.setUpTableDao();
int totalStudent = setUpTableDao.getTotalStudent();
// 2. Firestoreから生徒番号のリストを取得