image_repo.dart (3854B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | import 'dart:convert'; import 'package:flutter/widgets.dart'; import '../database.dart'; import '../models/image_model.dart'; class ImageRepo { Future<void> addImage(ImageModel image) async { final db = await AppDatabase.db; await db.insert('images', image.toMap()); } Future<List<ImageModel>> getImages(int projectId) async { final db = await AppDatabase.db; final res = await db.query( 'images', where: 'project_id = ?', whereArgs: [projectId], orderBy: 'created_at DESC', ); return res.map((e) => ImageModel.fromMap(e)).toList(); } Future<ImageModel?> getById(String id) async { final db = await AppDatabase.db; final res = await db.query('images', where: 'id = ?', whereArgs: [id]); if (res.isNotEmpty) return ImageModel.fromMap(res.first); return null; } Future<ImageModel?> getByFilePath(String path) async { final db = await AppDatabase.db; final res = await db.query( 'images', where: 'file_path = ?', whereArgs: [path], limit: 1, ); if (res.isNotEmpty) return ImageModel.fromMap(res.first); return null; } Future<void> updateProject(String id, int projectId) async { final db = await AppDatabase.db; await db.update( 'images', {'project_id': projectId}, where: 'id = ?', whereArgs: [id], ); } Future<void> updateName(String id, String newName) async { final db = await AppDatabase.db; await db.update( 'images', {'name': newName}, where: 'id = ?', whereArgs: [id], ); } Future<List<ImageModel>> getPendingImages() async { final db = await AppDatabase.db; final res = await db.query( 'images', where: 'status = ? OR status = ?', whereArgs: ['pending', 'analyzing'], ); return res.map((e) => ImageModel.fromMap(e)).toList(); } Future<void> updateStatus(String id, String status) async { final db = await AppDatabase.db; await db.update( 'images', {'status': status}, where: 'id = ?', whereArgs: [id], ); } Future<List<String>> getTagsForImage(dynamic id) async { final db = await AppDatabase.db; final res = await db.query( 'images', columns: ['tags'], where: 'id = ?', whereArgs: [id], ); if (res.isNotEmpty && res.first['tags'] != null) { try { // Decode the JSON string stored in the DB back to a List<String> final tagsJson = res.first['tags'] as String; if (tagsJson.isEmpty) return []; final List<dynamic> decoded = jsonDecode(tagsJson); return decoded.map((e) => e.toString()).toList(); } catch (e) { debugPrint("Error decoding tags: $e"); return []; } } return []; } Future<void> updateAnalysis(String id, String analysisData) async { final db = await AppDatabase.db; await db.update( 'images', {'analysis_data': analysisData}, where: 'id = ?', whereArgs: [id], ); } Future<void> updateTags(String id, List<String> tags) async { final db = await AppDatabase.db; await db.update( 'images', {'tags': jsonEncode(tags)}, where: 'id = ?', whereArgs: [id], ); } Future<void> deleteImage(String id) async { final db = await AppDatabase.db; await db.delete('images', where: 'id = ?', whereArgs: [id]); } // Helper for Project Deletion Service Future<List<String>> getAllFilePathsForProjectIds( List<int> projectIds, ) async { if (projectIds.isEmpty) return []; final db = await AppDatabase.db; final idList = projectIds.join(','); final res = await db.rawQuery( 'SELECT file_path FROM images WHERE project_id IN ($idList)', ); return res.map((e) => e['file_path'] as String).toList(); } } |