docking #36

Merged
Utahshi merged 4 commits from docking into master 2024-07-05 03:04:51 +00:00
Showing only changes of commit 06fdb9a312 - Show all commits

View File

@ -52,7 +52,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
//取得するためのクラスID
private int classId;
private String address;
private Object finalMyDataList;
@Override
@ -94,8 +93,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
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);
@ -109,6 +107,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
}
// クリック処理
@Override
public void onClick(View view) {
@ -161,7 +160,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
}
}
//UUIDを表示するかのダイアログ
//IDに関する処理
private void showUUIDYesNoDialog() {
firestoreReception_classIdDatabase = new FirestoreReception_classIdDatabase();
List<Integer> classIdList = firestoreReception_classIdDatabase.getAllDocumentsFromClassIdDatabase();
@ -169,19 +168,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();
}
});
@ -189,81 +199,103 @@ 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() {
//非同期処理の開始
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());
//final宣言することによってスレッドセーフになる(ラムダ式内で使えるようにする)
final List<MyDataClass> finalMyDataList = myDataList;
CreateSchedule createSchedule = new CreateSchedule(MainActivity.this);
Boolean notDuplicates = createSchedule.receiveData(myDataList, getApplicationContext());
latch.countDown();
runOnUiThread(() -> {
if (notDuplicates) {
Log.d("MainActivity", "スケジュール作成成功");
saveMyDataList(myDataList);
Intent toRoot = new Intent(MainActivity.this, Maps.class);
startActivity(toRoot);
} else {
showErrorDialog(latch, myDataList);
//保護者の重複による警告ダイアログ
showErrorDialog(finalMyDataList);
}
});
}
//ルート作成のダイアログ
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();
// createRouteの最後にexecutorをシャットダウン
executor.shutdown();
});
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
// MyDataListを共有プリファレンスに保存するメソッド
private void saveMyDataList(List<MyDataClass> myDataList) {
// 共有プリファレンスのインスタンスを取得
SharedPreferences sharedPreferences = getSharedPreferences("MyDataList", MODE_PRIVATE);
@ -278,11 +310,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
editor.apply();
}
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出席番号: ");
@ -295,15 +327,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<>();
@ -314,7 +350,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から生徒番号のリストを取得