Merge pull request 'murakumo_idが被らない用ロジック実装版' (#23) from murakumo_idが被らない用ロジック実装版 into master

Reviewed-on: #23
This commit is contained in:
murakumo 2024-07-02 05:05:17 +00:00
commit 85f736c913
6 changed files with 55 additions and 35 deletions

View File

@ -3,7 +3,7 @@ package com.example.oplogy;
import androidx.room.Database; import androidx.room.Database;
import androidx.room.RoomDatabase; import androidx.room.RoomDatabase;
@Database(entities = {SetUpTable.class}, version = 2, exportSchema = false) @Database(entities = {SetUpTable.class}, version = 3, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase { public abstract class AppDatabase extends RoomDatabase {
// データベースにアクセスするためのメソッドを提供する // データベースにアクセスするためのメソッドを提供する
public abstract SetUpTableDao setUpTableDao(); public abstract SetUpTableDao setUpTableDao();

View File

@ -5,12 +5,12 @@ import java.util.List;
public class CreateUUID { public class CreateUUID {
public static int generateUUID(List<String> classIdList ){ public static int generateUUID(List<Integer> classIdList ){
while (true){ while (true){
String uuid = String.valueOf((int)(Math.random() * 1000000)); int uuid = (int) (Math.random() * 100000);
boolean isDuplicate = false; boolean isDuplicate = false;
for(String classId : classIdList){ for(int classId : classIdList){
if(classId.equals(uuid)){ if(classId==uuid){
//重複があればフラグを立てループを抜ける //重複があればフラグを立てループを抜ける
isDuplicate = true; isDuplicate = true;
break; break;
@ -19,9 +19,11 @@ public class CreateUUID {
//重複がなければ生成したUUIDを返す //重複がなければ生成したUUIDを返す
if (!isDuplicate) { if (!isDuplicate) {
//firestoreに挿入処理 //firestoreに挿入処理
InsertClassIdforFirebase insertClassIdforFirebase = new InsertClassIdforFirebase();
insertClassIdforFirebase.insertClassId(uuid);
//テスト用 //テスト用
uuid="100"; uuid=100;
return Integer.parseInt(uuid); return uuid;
} }
} }
} }

View File

@ -17,14 +17,14 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public class FirestoreReception_classIdDatabase { public class FirestoreReception_classIdDatabase {
private FirebaseFirestore db; private FirebaseFirestore db;
private List<String> classIdList= new ArrayList<>(); private List<Integer> classIdList= new ArrayList<>();
public FirestoreReception_classIdDatabase() { public FirestoreReception_classIdDatabase() {
db = FirebaseFirestore.getInstance(); db = FirebaseFirestore.getInstance();
} }
public List<String> getAllDocumentsFromClassIdDatabase() { public List<Integer> getAllDocumentsFromClassIdDatabase() {
db.collection("classId_Database") db.collection("classId_Database")
.get() .get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@ -34,8 +34,7 @@ public class FirestoreReception_classIdDatabase {
for (QueryDocumentSnapshot document : task.getResult()) { for (QueryDocumentSnapshot document : task.getResult()) {
Log.d("結果", document.getId() + " => " + document.getData()); Log.d("結果", document.getId() + " => " + document.getData());
//データをListに追加 //データをListに追加
classIdList.add((String) document.get("classId")); classIdList.add(((Long) document.get("classId")).intValue()); }
}
} else { } else {
Log.d("結果", "Error getting documents: ", task.getException()); Log.d("結果", "Error getting documents: ", task.getException());
} }
@ -45,7 +44,7 @@ public class FirestoreReception_classIdDatabase {
return classIdList; return classIdList;
} }
public List<String> getClassIdList() { public List<Integer> getClassIdList() {
return classIdList; return classIdList;
} }
} }

View File

@ -0,0 +1,19 @@
package com.example.oplogy;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
public class InsertClassIdforFirebase {
public void insertClassId(int classId) {
FirebaseFirestore db = FirebaseFirestore.getInstance();
Map<String, Object> data = new HashMap<>();
data.put("classId", classId); // classId is inserted as a number
db.collection("classId_Database").add(data)
.addOnSuccessListener(documentReference -> System.out.println("DocumentSnapshot added with ID: " + documentReference.getId()))
.addOnFailureListener(e -> System.err.println("Error adding document: " + e));
}
}

View File

@ -48,7 +48,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private FirestoreReception_classIdDatabase firestoreReception_classIdDatabase; private FirestoreReception_classIdDatabase firestoreReception_classIdDatabase;
//取得するためのクラスID //取得するためのクラスID
private int classId=100000; private int classId;
@Override @Override
@ -84,6 +84,22 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
db = FirebaseFirestore.getInstance(); db = FirebaseFirestore.getInstance();
firestoreReception = new FirestoreReception(); firestoreReception = new FirestoreReception();
//TODO:classIdの初期値を取得
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
try{
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable")
.build();
SetUpTableDao setUpTableDao = db.setUpTableDao();
classId = setUpTableDao.getClassId();
firestoreReception.getDocumentsByClassId(classId);
}catch (Exception e){
//無視して続行
e.printStackTrace();
}
});
} }
@ -141,7 +157,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
//UUIDを表示するかのダイアログ //UUIDを表示するかのダイアログ
private void showUUIDYesNoDialog() { private void showUUIDYesNoDialog() {
firestoreReception_classIdDatabase = new FirestoreReception_classIdDatabase(); firestoreReception_classIdDatabase = new FirestoreReception_classIdDatabase();
List<String> classIdList = firestoreReception_classIdDatabase.getAllDocumentsFromClassIdDatabase(); List<Integer> classIdList = firestoreReception_classIdDatabase.getAllDocumentsFromClassIdDatabase();
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
@ -176,7 +192,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
// タスク1: ローカルDBから生徒数を取得してtotalStudentと比較 // タスク1: ローカルDBから生徒数を取得してtotalStudentと比較
executor.execute(() -> { executor.execute(() -> {
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build(); AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable")
.fallbackToDestructiveMigration()
.build();
SetUpTableDao setUpTableDao = db.setUpTableDao(); SetUpTableDao setUpTableDao = db.setUpTableDao();
Log.d("MainActivity", "db" + setUpTableDao.getAll()); Log.d("MainActivity", "db" + setUpTableDao.getAll());
@ -334,19 +352,4 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
alertDialog.dismiss(); alertDialog.dismiss();
} }
} }
@Override
protected void onResume(){
super.onResume();
//roomからclassIdを取得
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable").build();
SetUpTableDao setUpTableDao = db.setUpTableDao();
classId = setUpTableDao.getClassId();
});
if (classId != 100000 ) {
firestoreReception.getDocumentsByClassId(classId);
}
}
} }

View File

@ -127,11 +127,8 @@ public class SetUpActivity extends FragmentActivity
executor.execute(() -> { executor.execute(() -> {
//roomのインスタンスを作成 //roomのインスタンスを作成
AppDatabase db = Room.databaseBuilder( AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "SetUpTable")
getApplicationContext(), .fallbackToDestructiveMigration()
AppDatabase.class,
"SetUpTable"
)
.build(); .build();
SetUpTableDao setUpTableDao = db.setUpTableDao(); SetUpTableDao setUpTableDao = db.setUpTableDao();
// Roomの操作を行う // Roomの操作を行う