creek

The AI Image Editor of 2030

lib/data/repos/image_repo.dart (3854B)


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