From 914e49bccb17d02d788edfcf211fa03d02038a36 Mon Sep 17 00:00:00 2001 From: nemukemo Date: Thu, 4 Jul 2024 18:40:52 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=83=AB=E3=83=BC=E3=83=88=E4=BD=9C?= =?UTF-8?q?=E6=88=90=E6=99=82=E3=81=AE=E3=83=80=E3=82=A4=E3=82=A2=E3=83=AD?= =?UTF-8?q?=E3=82=B0=E9=96=A2=E9=80=A3=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/oplogy/MainActivity.java | 126 ++++++++---------- 1 file changed, 58 insertions(+), 68 deletions(-) diff --git a/app/src/main/java/com/example/oplogy/MainActivity.java b/app/src/main/java/com/example/oplogy/MainActivity.java index 241b490..91988a5 100644 --- a/app/src/main/java/com/example/oplogy/MainActivity.java +++ b/app/src/main/java/com/example/oplogy/MainActivity.java @@ -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 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()); + + //final宣言することによって、スレッドセーフになる(ラムダ式内で使えるようにする) + final List finalMyDataList = myDataList; CreateRoot createRoot = new CreateRoot(MainActivity.this); - Boolean notDuplicates = createRoot.receiveData(myDataList,getApplicationContext()); - latch.countDown(); + Boolean notDuplicates = createRoot.receiveData(finalMyDataList, getApplicationContext()); - if (notDuplicates) { - Log.d("MainActivity", "スケジュール作成成功"); - } else { - showErrorDialog(latch, myDataList); - } - }); - - new Thread(() -> { - List myDataList = firestoreReception.getMyDataList(); - try { - latch.await(); // Both tasks must call countDown() before this returns - runOnUiThread(() -> { + runOnUiThread(() -> { + if (notDuplicates) { + Log.d("MainActivity", "スケジュール作成成功"); Intent toRoot = new Intent(MainActivity.this, Maps.class); startActivity(toRoot); - }); - } catch (InterruptedException e) { - e.printStackTrace(); - } - }).start(); + } else { + //保護者の重複による警告ダイアログ + showErrorDialog(finalMyDataList); + } + }); - executor.shutdown(); + // 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 myDataList) { + private void showErrorDialog(List myDataList) { List 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) { - dialog.dismiss(); - } + .setPositiveButton("OK", (dialog, which) -> { + dialog.dismiss(); }) .show(); } + + private AppDatabase getDatabaseInstance() { + return Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build(); + } + + + //提出状況の取得 private ArrayList getSubmissionStudents() { ArrayList 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から生徒番号のリストを取得 From 4bf097c04dbc86958f945262972ba408819bdc2c Mon Sep 17 00:00:00 2001 From: nemukemo Date: Fri, 5 Jul 2024 11:30:52 +0900 Subject: [PATCH 2/2] =?UTF-8?q?ID=E4=BD=9C=E6=88=90=E3=81=AE=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E4=BD=9C=E6=88=90=E3=80=81=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E3=81=AB=E5=88=87=E3=82=8A=E6=9B=BF=E3=81=88=E3=81=A6=E3=80=81?= =?UTF-8?q?=E3=81=9D=E3=82=8C=E3=81=AB=E5=90=88=E3=82=8F=E3=81=9B=E3=81=A6?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=82=92=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/oplogy/MainActivity.java | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/example/oplogy/MainActivity.java b/app/src/main/java/com/example/oplogy/MainActivity.java index 91988a5..51615f4 100644 --- a/app/src/main/java/com/example/oplogy/MainActivity.java +++ b/app/src/main/java/com/example/oplogy/MainActivity.java @@ -157,7 +157,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe } } - //UUIDを表示するかのダイアログ + //IDに関する処理 private void showUUIDYesNoDialog() { firestoreReception_classIdDatabase = new FirestoreReception_classIdDatabase(); List classIdList = firestoreReception_classIdDatabase.getAllDocumentsFromClassIdDatabase(); @@ -165,19 +165,30 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("クラスID"); - builder.setMessage("あなたのクラスIDを表示しますか?"); + builder.setMessage("あなたのクラスIDを表示/もしくは新規で作成しますか?"); - builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { + builder.setPositiveButton("作成", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { classId = CreateUUID.generateUUID(classIdList); - Toast.makeText(MainActivity.this, "クラスID: " + classId, Toast.LENGTH_SHORT).show(); + // 生成されたクラスIDを表示するメソッド + showClassIdDialog("生成されたクラスID",classId); } }); - builder.setNegativeButton("No", new DialogInterface.OnClickListener() { + builder.setNegativeButton("表示", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - Log.d("DialogNO", "DialogでNoが選ばれました"); + //roomを扱うため非同期処理 + ExecutorService executor = Executors.newSingleThreadExecutor(); + executor.execute(() -> { + // 現在のクラスIDを取得 + int currentClassId = getCurrentClassIdFromRoom(); + runOnUiThread(() -> { + // 現在のクラスIDを表示するダイアログ + showClassIdDialog("現在のクラスID",currentClassId); + }); + }); + executor.shutdown(); } }); @@ -185,6 +196,28 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe alertDialog.show(); } + private int getCurrentClassIdFromRoom() { + AppDatabase db = getDatabaseInstance(); + SetUpTableDao setUpTableDao = db.setUpTableDao(); + + // 現在のクラスIDを取得 + return setUpTableDao.getClassId(); + } + //クラスIDを表示するダイアログ + private void showClassIdDialog(String title, int classId) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(title); + builder.setMessage("クラスID: " + classId); + builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } + //ルート作成の非同期処理 private void fetchDataAndCreateRoute() {