-
Notifications
You must be signed in to change notification settings - Fork 84
PAINTROID-638 Add a placeholder name when saving #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tarikhoza
wants to merge
1
commit into
Catrobat:develop
Choose a base branch
from
Tarikhoza:PAINTROID-638
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import 'package:shared_preferences/shared_preferences.dart'; | ||
|
|
||
| import 'package:paintroid/core/database/project_database.dart'; | ||
|
|
||
| class NameGenerator { | ||
| static const String _imageCounterKey = 'last_image_number'; | ||
|
|
||
| /// Finds the next available project name in format "project[number]" | ||
| /// by querying the database for existing projects | ||
| static Future<String> getNextProjectName(ProjectDatabase database) async { | ||
| final projects = await database.projectDAO.getProjects(); | ||
| return _findNextAvailableName( | ||
| projects.map((p) => p.name).toList(), | ||
| 'project', | ||
| ); | ||
| } | ||
|
|
||
| /// Finds the next available image name in format "image[number]" | ||
| /// Uses SharedPreferences to track the counter since images are saved | ||
| /// to the photo library where we can't query filenames | ||
| static Future<String> getNextImageName() async { | ||
| final prefs = await SharedPreferences.getInstance(); | ||
| final lastNumber = prefs.getInt(_imageCounterKey) ?? 0; | ||
| final nextNumber = lastNumber + 1; | ||
| await prefs.setInt(_imageCounterKey, nextNumber); | ||
| return 'image$nextNumber'; | ||
| } | ||
|
|
||
| /// Finds the next available name by parsing existing names with the given prefix | ||
| /// Returns prefix + (maxNumber + 1) | ||
| static String _findNextAvailableName(List<String> existingNames, String prefix) { | ||
| final pattern = RegExp('^$prefix(\\d+)\$'); | ||
| int maxNumber = 0; | ||
|
|
||
| for (final name in existingNames) { | ||
| final match = pattern.firstMatch(name); | ||
| if (match != null) { | ||
| final number = int.tryParse(match.group(1)!); | ||
| if (number != null && number > maxNumber) { | ||
| maxNumber = number; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return '$prefix${maxNumber + 1}'; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:mockito/annotations.dart'; | ||
| import 'package:mockito/mockito.dart'; | ||
| import 'package:shared_preferences/shared_preferences.dart'; | ||
|
|
||
| import 'package:paintroid/core/database/project_dao.dart'; | ||
| import 'package:paintroid/core/database/project_database.dart'; | ||
| import 'package:paintroid/core/models/database/project.dart'; | ||
| import 'package:paintroid/core/utils/name_generator.dart'; | ||
|
|
||
| import 'name_generator_test.mocks.dart'; | ||
|
|
||
| @GenerateMocks([ProjectDatabase, ProjectDAO]) | ||
| void main() { | ||
| group('NameGenerator', () { | ||
| late MockProjectDatabase mockDatabase; | ||
| late MockProjectDAO mockProjectDAO; | ||
|
|
||
| setUp(() { | ||
| mockDatabase = MockProjectDatabase(); | ||
| mockProjectDAO = MockProjectDAO(); | ||
| when(mockDatabase.projectDAO).thenReturn(mockProjectDAO); | ||
| }); | ||
|
|
||
| group('getNextProjectName', () { | ||
| test('Should return "project1" when no projects exist', () async { | ||
| when(mockProjectDAO.getProjects()).thenAnswer((_) async => []); | ||
|
|
||
| final result = await NameGenerator.getNextProjectName(mockDatabase); | ||
|
|
||
| expect(result, 'project1'); | ||
| verify(mockProjectDAO.getProjects()).called(1); | ||
| }); | ||
|
|
||
| test('Should return "project2" when "project1" exists', () async { | ||
| final existingProject = Project( | ||
| name: 'project1', | ||
| path: '/path/to/project1', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ); | ||
| when(mockProjectDAO.getProjects()) | ||
| .thenAnswer((_) async => [existingProject]); | ||
|
|
||
| final result = await NameGenerator.getNextProjectName(mockDatabase); | ||
|
|
||
| expect(result, 'project2'); | ||
| verify(mockProjectDAO.getProjects()).called(1); | ||
| }); | ||
|
|
||
| test('Should return "project5" when projects 1-4 exist', () async { | ||
| final projects = [ | ||
| Project( | ||
| name: 'project1', | ||
| path: '/path/to/project1', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| Project( | ||
| name: 'project2', | ||
| path: '/path/to/project2', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| Project( | ||
| name: 'project3', | ||
| path: '/path/to/project3', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| Project( | ||
| name: 'project4', | ||
| path: '/path/to/project4', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| ]; | ||
| when(mockProjectDAO.getProjects()).thenAnswer((_) async => projects); | ||
|
|
||
| final result = await NameGenerator.getNextProjectName(mockDatabase); | ||
|
|
||
| expect(result, 'project5'); | ||
| }); | ||
|
|
||
| test('Should find max number when projects are not sequential', () async { | ||
| final projects = [ | ||
| Project( | ||
| name: 'project1', | ||
| path: '/path/to/project1', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| Project( | ||
| name: 'project5', | ||
| path: '/path/to/project5', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| Project( | ||
| name: 'project3', | ||
| path: '/path/to/project3', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| ]; | ||
| when(mockProjectDAO.getProjects()).thenAnswer((_) async => projects); | ||
|
|
||
| final result = await NameGenerator.getNextProjectName(mockDatabase); | ||
|
|
||
| expect(result, 'project6'); | ||
| }); | ||
|
|
||
| test('Should ignore projects with different naming patterns', () async { | ||
| final projects = [ | ||
| Project( | ||
| name: 'project1', | ||
| path: '/path/to/project1', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| Project( | ||
| name: 'myProject', | ||
| path: '/path/to/myProject', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| Project( | ||
| name: 'project3', | ||
| path: '/path/to/project3', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| Project( | ||
| name: 'test', | ||
| path: '/path/to/test', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| ]; | ||
| when(mockProjectDAO.getProjects()).thenAnswer((_) async => projects); | ||
|
|
||
| final result = await NameGenerator.getNextProjectName(mockDatabase); | ||
|
|
||
| expect(result, 'project4'); | ||
| }); | ||
|
|
||
| test('Should handle large project numbers correctly', () async { | ||
| final projects = [ | ||
| Project( | ||
| name: 'project99', | ||
| path: '/path/to/project99', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| Project( | ||
| name: 'project100', | ||
| path: '/path/to/project100', | ||
| lastModified: DateTime.now(), | ||
| creationDate: DateTime.now(), | ||
| ), | ||
| ]; | ||
| when(mockProjectDAO.getProjects()).thenAnswer((_) async => projects); | ||
|
|
||
| final result = await NameGenerator.getNextProjectName(mockDatabase); | ||
|
|
||
| expect(result, 'project101'); | ||
| }); | ||
| }); | ||
|
|
||
| group('getNextImageName', () { | ||
| setUp(() { | ||
| SharedPreferences.setMockInitialValues({}); | ||
| }); | ||
|
|
||
| test('Should return "image1" on first call', () async { | ||
| final result = await NameGenerator.getNextImageName(); | ||
|
|
||
| expect(result, 'image1'); | ||
| }); | ||
|
|
||
| test('Should increment counter with each call', () async { | ||
| final first = await NameGenerator.getNextImageName(); | ||
| expect(first, 'image1'); | ||
|
|
||
| final second = await NameGenerator.getNextImageName(); | ||
| expect(second, 'image2'); | ||
|
|
||
| final third = await NameGenerator.getNextImageName(); | ||
| expect(third, 'image3'); | ||
| }); | ||
|
|
||
| test('Should persist counter across instances', () async { | ||
| await NameGenerator.getNextImageName(); | ||
| await NameGenerator.getNextImageName(); | ||
|
|
||
| final prefs = await SharedPreferences.getInstance(); | ||
| final savedValue = prefs.getInt('last_image_number'); | ||
|
|
||
| expect(savedValue, 2); | ||
| }); | ||
|
|
||
| test('Should resume from stored counter value', () async { | ||
| SharedPreferences.setMockInitialValues({'last_image_number': 42}); | ||
|
|
||
| final result = await NameGenerator.getNextImageName(); | ||
|
|
||
| expect(result, 'image43'); | ||
| }); | ||
|
|
||
| test('Should handle large numbers correctly', () async { | ||
| SharedPreferences.setMockInitialValues({'last_image_number': 999}); | ||
|
|
||
| final result = await NameGenerator.getNextImageName(); | ||
|
|
||
| expect(result, 'image1000'); | ||
| }); | ||
|
|
||
| test('Should save incremented value to SharedPreferences', () async { | ||
| SharedPreferences.setMockInitialValues({'last_image_number': 5}); | ||
|
|
||
| await NameGenerator.getNextImageName(); | ||
|
|
||
| final prefs = await SharedPreferences.getInstance(); | ||
| final savedValue = prefs.getInt('last_image_number'); | ||
|
|
||
| expect(savedValue, 6); | ||
| }); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plerase remove comments, should be clear enough :)