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

Merged
murakumo merged 1 commits from murakumo_ダイアログ祭り into master 2024-07-05 00:52:13 +00:00
Showing only changes of commit 914e49bccb - Show all commits

View File

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