commit 109c9d4d702d9eceeb4b6a6fd72241204d8c6d8f
parent 86fd9f24c4c15e131a5ccbf57ad5bb3f041cc034
Author: Nilotpal Gupta <nilotpalgupta0701@gmail.com>
Date: Mon, 1 Dec 2025 17:38:14 +0530
note asset generation fixed
Diffstat:
3 files changed, 73 insertions(+), 30 deletions(-)
diff --git a/lib/data/repos/note_repo.dart b/lib/data/repos/note_repo.dart
@@ -78,4 +78,20 @@ class NoteRepo {
final db = await AppDatabase.db;
await db.delete('notes', where: 'id = ?', whereArgs: [id]);
}
+
+ // Inside NoteRepo
+
+ Future<NoteModel?> getByCropPath(String path) async {
+ final db = await AppDatabase.db;
+ final maps = await db.query(
+ 'notes',
+ where: 'cropFilePath = ?',
+ whereArgs: [path],
+ );
+
+ if (maps.isNotEmpty) {
+ return NoteModel.fromMap(maps.first);
+ }
+ return null;
+ }
}
diff --git a/lib/services/flask_service.dart b/lib/services/flask_service.dart
@@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:io';
import 'package:adobe/data/repos/file_repo.dart';
import 'package:adobe/data/repos/image_repo.dart';
+import 'package:adobe/data/repos/note_repo.dart';
import 'package:adobe/data/repos/project_repo.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
@@ -19,6 +20,12 @@ class FlaskService {
'Content-Type': 'application/json',
};
+ // --- OPTIMIZATION: Instantiate Repos once ---
+ final _imageRepo = ImageRepo();
+ final _noteRepo = NoteRepo();
+ final _projectRepo = ProjectRepo();
+ final _fileRepo = FileRepo();
+
// ===========================================================================
// 1. PIPELINES (Complex workflows)
// ===========================================================================
@@ -97,42 +104,64 @@ class FlaskService {
/// [Background Removal]
Future<String?> generateAsset({required String imagePath}) async {
+ // 1. Prepare and Upload
final String? base64Image = await _encodeFile(imagePath);
if (base64Image == null) return null;
-
- final String? path = await _performImageOperation(
+
+ final String? generatedAssetPath = await _performImageOperation(
endpoint: '/asset',
logPrefix: '✂️ Asset Gen',
body: {'image': base64Image},
filenamePrefix: 'asset',
);
-
- if (path != null) {
- // 1. Find the project ID
+
+ // 2. Resolve Project ID and Save
+ if (generatedAssetPath != null) {
int? projectId;
-
- final imagemodel = await ImageRepo().getByFilePath(imagePath);
- if (imagemodel != null) {
- projectId = imagemodel.projectId;
- } else {
- final filemodel = await FileRepo().getByFilePath(imagePath);
- if (filemodel != null) {
- projectId = filemodel.projectId;
+
+ // --- CHECK 1: Is this a Main Image? ---
+ final imageModel = await _imageRepo.getByFilePath(imagePath);
+ if (imageModel != null) {
+ projectId = imageModel.projectId;
+ }
+
+ // --- CHECK 2: Is this a Note Crop? (NEW) ---
+ if (projectId == null) {
+ // You need a method in NoteRepo to find a note by its crop path
+ final noteModel = await _noteRepo.getByCropPath(imagePath);
+
+ if (noteModel != null) {
+ // Traverse up: Note -> Parent Image -> Project
+ final parentImage = await _imageRepo.getById(noteModel.imageId);
+ if (parentImage != null) {
+ projectId = parentImage.projectId;
+ debugPrint("🔗 Linked Asset to Project via Note: ${noteModel.id}");
+ }
}
}
-
- // 2. Update the Project Repo AND SAVE TO DATABASE
+
+ // --- CHECK 3: Is this a generic File? ---
+ if (projectId == null) {
+ final fileModel = await _fileRepo.getByFilePath(imagePath);
+ if (fileModel != null) {
+ projectId = fileModel.projectId;
+ }
+ }
+
+ // 3. Update the Project
if (projectId != null) {
- final project = await ProjectRepo().getProjectById(projectId);
+ final project = await _projectRepo.getProjectById(projectId);
if (project != null) {
- project.assetsPath.add(path); // Update memory
- await ProjectRepo().updateAssets(projectId, project.assetsPath);
- debugPrint("✅ Asset path saved to Project DB: $path");
+ project.assetsPath.add(generatedAssetPath);
+ await _projectRepo.updateAssets(projectId, project.assetsPath);
+ debugPrint("✅ Asset path saved to Project DB: $generatedAssetPath");
}
+ } else {
+ debugPrint("⚠️ Asset generated but could not link to a Project ID.");
}
}
-
- return path;
+
+ return generatedAssetPath;
}
// ===========================================================================
@@ -184,9 +213,7 @@ class FlaskService {
return _saveImageFromResponse(response, filenamePrefix);
}
- debugPrint(
- "❌ $logPrefix Failed: ${response?.statusCode ?? 'No Connection'}",
- );
+ debugPrint("❌ $logPrefix Failed: ${response?.statusCode ?? 'No Connection'}");
return null;
}
diff --git a/lib/ui/pages/project_board_page.dart b/lib/ui/pages/project_board_page.dart
@@ -72,12 +72,12 @@ class _ProjectBoardPageState extends State<ProjectBoardPage> {
await Future.wait(
images.map((img) async {
final Set<String> distinctCategories = {...img.tags};
- final notes = await _noteRepo.getNotesForImage(img.id);
- for (var note in notes) {
- if (note.category.isNotEmpty) {
- distinctCategories.add(note.category);
- }
- }
+ // final notes = await _noteRepo.getNotesForImage(img.id);
+ // for (var note in notes) {
+ // if (note.category.isNotEmpty) {
+ // distinctCategories.add(note.category);
+ // }
+ // }
if (distinctCategories.isEmpty) {
tempMap.putIfAbsent('Uncategorized', () => []).add(img);
} else {