Compare commits

...

2 Commits

Author SHA1 Message Date
nemukemo
e64b2f6c11 roomの作成
daoは追加がいる
2024-06-18 14:07:39 +09:00
nemukemo
aa189688d3 room 2024-06-12 20:25:16 +09:00
9 changed files with 151 additions and 62 deletions

View File

@ -46,3 +46,14 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
} }
//room用の依存関係を追加
dependencies {
def room_version = "2.4.1"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
}
dependencies {
implementation 'com.google.code.gson:gson:2.8.8'
}

View File

@ -0,0 +1,12 @@
package com.example.oplogy;
import androidx.room.Database;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;
@Database(entities = {QuestionnaireForm.class}, version = 1)
@TypeConverters({Converters.class})
public abstract class AppDatabase extends RoomDatabase {
// データベースにアクセスするためのメソッドを提供する
public abstract QuestionnaireFormDao questionnaireFormDao();
}

View File

@ -0,0 +1,39 @@
package com.example.oplogy;
import androidx.room.TypeConverter;
import com.google.firebase.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class Converters {
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.JAPAN);
// タイムスタンプを文字列(yyyy-mm-dd,日時)に変換
@TypeConverter
public static List<String> fromTimestampList(List<Timestamp> timestamps) {
List<String> strings = new ArrayList<>();
for (Timestamp timestamp : timestamps) {
strings.add(format.format(timestamp.toDate()));
}
return strings;
}
@TypeConverter
public static List<Timestamp> toTimestampList(List<String> strings) {
List<Timestamp> timestamps = new ArrayList<>();
for (String string : strings) {
try {
Date date = format.parse(string);
timestamps.add(new Timestamp(date));
} catch (Exception e) {
e.printStackTrace();
}
}
return timestamps;
}
}

View File

@ -1,7 +1,13 @@
package com.example.oplogy; package com.example.oplogy;
import static android.content.Context.MODE_PRIVATE;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log; import android.util.Log;
import androidx.room.Room;
import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Task;
import com.google.firebase.Timestamp; import com.google.firebase.Timestamp;
@ -16,46 +22,58 @@ import java.util.Map;
public class FirestoreReception { public class FirestoreReception {
private FirebaseFirestore db; private FirebaseFirestore db;
private Context context;
private AppDatabase appDatabase;
public FirestoreReception() { public FirestoreReception(Context context) {
db = FirebaseFirestore.getInstance(); db = FirebaseFirestore.getInstance();
this.context = context;
this.appDatabase = Room.databaseBuilder(context, AppDatabase.class, "FormsRoom").build();
} }
//ClassIdを引数にデータの作成を行う
public void getDocumentsByClassId(int classId) { public void getDocumentsByClassId(int classId) {
CollectionReference collectionRef = db.collection("QuestionnaireForms"); CollectionReference collectionRef = db.collection("QuestionnaireForms");
SharedPreferences sharedPreferences = context.getSharedPreferences("FirestoreData", MODE_PRIVATE);
// classIdが引数のものを取得する
collectionRef.whereEqualTo("classId", classId).get() collectionRef.whereEqualTo("classId", classId).get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override @Override
public void onComplete(Task<QuerySnapshot> task) { public void onComplete(Task<QuerySnapshot> task) {
if (task.isSuccessful()) { if (task.isSuccessful()) {
// データの取得に成功した場合
for (QueryDocumentSnapshot document : task.getResult()) { for (QueryDocumentSnapshot document : task.getResult()) {
// すでに取得済みのドキュメントは除外
if (sharedPreferences.getBoolean(document.getId(), false)) {
Log.w("FirestoreReception", "すでに取得済みのドキュメントです.");
continue;
}
// firestoreのドキュメントのデータを取得
Map<String, Object> data = document.getData(); Map<String, Object> data = document.getData();
// デバッグ用のログ出力 // データをエンティティクラスのインスタンスに変換
Log.d("FirestoreReception", "Document ID: " + document.getId()); QuestionnaireForm form = new QuestionnaireForm();
Log.d("FirestoreReception", "Data: " + data); form.classId = Integer.parseInt(data.get("classId").toString());
form.patronName = data.get("patronName").toString();
form.address = data.get("address").toString();
form.firstDay = Converters.fromTimestampList((List<Timestamp>) data.get("firstDay")).toString();
form.studentNumber = Integer.parseInt(data.get("studentNumber").toString());
form.childName = data.get("childName").toString();
form.thirdDay = Converters.fromTimestampList((List<Timestamp>) data.get("thirdDay")).toString();
form.secondDay = Converters.fromTimestampList((List<Timestamp>) data.get("secondDay")).toString();
// ここでデータを取得し必要に応じて処理を行います // データベースに保存
// String parentName = (String) data.get("patronName"); new Thread(new Runnable() {
// String childName = (String) data.get("childName"); @Override
// String studentId = (String) data.get("studentNumber"); public void run() {
//// Timestamp address = (Timestamp) data.get("address"); appDatabase.questionnaireFormDao().insertAll(form);
// List<Timestamp> firstDay = (List<Timestamp>) data.get("firstDay"); Log.w("FirestoreReception", "成功.");
// List<Timestamp> secondDay = (List<Timestamp>) data.get("secondDay"); }
// List<Timestamp> thirdDay = (List<Timestamp>) data.get("thirdDay"); }).start();
//
// // 取得したデータを使って必要な処理を行う // ドキュメントのIDを保存
// Log.d("FirestoreReception", "ParentName: " + parentName); // すでに取得済みのドキュメントは再度取得しないようにする
// Log.d("FirestoreReception", "ChildName: " + childName); SharedPreferences.Editor editor = sharedPreferences.edit();
// Log.d("FirestoreReception", "StudentNumber: " + studentId); editor.putBoolean(document.getId(), true);
//// Log.d("FirestoreReception", "Address: " + address.toDate()); editor.apply();
// Log.d("FirestoreReception", "First Day: " + firstDay);
// Log.d("FirestoreReception", "Second Day: " + secondDay);
// Log.d("FirestoreReception", "Third Day: " + thirdDay);
} }
} else { } else {
Log.w("FirestoreReception", "Error getting documents.", task.getException()); Log.w("FirestoreReception", "Error getting documents.", task.getException());

View File

@ -63,10 +63,10 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
submission = findViewById(R.id.submission); submission = findViewById(R.id.submission);
submission.setOnClickListener(this); submission.setOnClickListener(this);
// firestoreの受信関連 // firestoreの受信関連の宣言
db = FirebaseFirestore.getInstance(); db = FirebaseFirestore.getInstance();
firestoreReception = new FirestoreReception(); firestoreReception = new FirestoreReception(this);
// 受信するメソッドの呼び出し
firestoreReception.getDocumentsByClassId(100); firestoreReception.getDocumentsByClassId(100);
@ -85,12 +85,12 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
} }
// セットアップのクリック処理 // セットアップのクリック処理
if(view == setUp){ // if(view == setUp){
imageSetup.setImageResource(R.drawable.ischecked_uuid); // imageSetup.setImageResource(R.drawable.ischecked_uuid);
Intent toSetup = new Intent(MainActivity.this,SetupActivity.class); // Intent toSetup = new Intent(MainActivity.this,SetupActivity.class);
startActivity(toSetup); // startActivity(toSetup);
//
} // }
// ルート作成のクリック処理 // ルート作成のクリック処理
if(view == root){ if(view == root){
imageRoot.setImageResource(R.drawable.pin); imageRoot.setImageResource(R.drawable.pin);

View File

@ -0,0 +1,20 @@
package com.example.oplogy;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity
public class QuestionnaireForm {
//主キー
@PrimaryKey(autoGenerate = true)
public int id;
//その他フィールド
public int classId;
public String patronName;
public String address;
public String firstDay;
public int studentNumber;
public String childName;
public String thirdDay;
public String secondDay;
}

View File

@ -0,0 +1,17 @@
package com.example.oplogy;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import java.util.List;
@Dao
public interface QuestionnaireFormDao {
@Query("SELECT * FROM QuestionnaireForm")
List<QuestionnaireForm> getAll();
// このメソッドはQuestionnaireFormのリストを受け取りそれらをデータベースに挿入します
@Insert
void insertAll(QuestionnaireForm... questionnaireForms);
}

View File

@ -1,14 +0,0 @@
package com.example.oplogy;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class SetUpActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_up);
}
}

View File

@ -1,14 +0,0 @@
package com.example.oplogy;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class SetupActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup);
}
}