2024-09-02 ほぼOK

This commit is contained in:
2024-09-02 21:25:19 +09:00
parent dc58dc0584
commit fe46d46ab6
59 changed files with 2006 additions and 677 deletions

View File

@ -13,6 +13,11 @@ class DatabaseHelper {
static Database? _database;
Future<Database> get database async => _database ??= await _initDatabase();
// データベース初期化:
//
// シングルトンパターンを使用してDatabaseHelperのインスタンスを管理しています。
// _initDatabase()メソッドでデータベースを初期化し、必要なテーブルを作成します。
//
Future<Database> _initDatabase() async {
Directory documentDirectory = await getApplicationDocumentsDirectory();
String path = join(documentDirectory.path, 'rog.db');
@ -30,7 +35,10 @@ class DatabaseHelper {
onCreate: _onCreate);
}
// DBを初期化する際に、必要なテーブルを作成します。
//
Future _onCreate(Database db, int version) async {
// destinationテーブル: 目的地の情報を保存(位置、名前、住所、連絡先情報など)。
await db.execute('''
CREATE TABLE destination(
location_id INTEGER PRIMARY KEY,
@ -63,6 +71,7 @@ class DatabaseHelper {
)
''');
// rogainingテーブル: ロゲイニングorienteering的なアクティビティの記録を保存。
await db.execute('''
CREATE TABLE rogaining(
rog_id INTEGER PRIMARY KEY AUTOINCREMENT,
@ -76,6 +85,7 @@ class DatabaseHelper {
)
''');
// rogテーブル: ロゲイニングのチェックポイント情報を保存。
await db.execute('''
CREATE TABLE rog(
id INTEGER PRIMARY KEY AUTOINCREMENT,
@ -138,6 +148,22 @@ class DatabaseHelper {
await db.delete('rog');
}
Future<void> deleteAllRogainingExceptToday() async {
Database db = await instance.database;
// 今日の開始時刻をエポックミリ秒で取得
final now = DateTime.now();
final startOfDay = DateTime(now.year, now.month, now.day).millisecondsSinceEpoch;
// 今日チェックインしたもの以外を削除
await db.delete(
'rog',
where: 'checkintime < ?',
whereArgs: [startOfDay]
);
}
Future<bool> isRogAlreadyAvailable(int id) async {
Database db = await instance.database;
var rog = await db.query('rog', where: "id = $id");
@ -229,6 +255,27 @@ class DatabaseHelper {
await db.delete('destination');
}
Future<void> deleteAllDestinationsExceptTodayCheckins() async {
Database db = await instance.database;
// 今日の開始時刻をエポックからのミリ秒で取得
final now = DateTime.now();
final startOfDay = DateTime(now.year, now.month, now.day).millisecondsSinceEpoch;
// 今日チェックインされ、buy_pointを持つ目的地を除いて全て削除
await db.rawDelete('''
DELETE FROM destination
WHERE location_id NOT IN (
SELECT d.location_id
FROM destination d
JOIN rog r ON d.location_id = r.cp_number
WHERE date(r.checkintime / 1000, 'unixepoch', 'localtime') = date('now', 'localtime')
AND d.buy_point > 0
AND d.checkedin = 1
)
''', [startOfDay]);
}
Future<bool> isAlreadyAvailable(int locationId) async {
Database db = await instance.database;
var dest =