Skip to content

Commit

Permalink
feat: added the functionality to display the clipart and delete teh c…
Browse files Browse the repository at this point in the history
…lipart.
  • Loading branch information
Jhalakupadhyay committed Oct 20, 2024
1 parent e910c4b commit bd7dbd6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 28 deletions.
32 changes: 20 additions & 12 deletions lib/bademagic_module/utils/file_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,26 @@ class FileHelper {
if (file is File &&
file.path.endsWith('.json') &&
file.path.contains('data_')) {

final String content = await file.readAsString();
if (content.isNotEmpty) {
// Ensure correct type casting
final List<dynamic> decodedData = jsonDecode(content);
final List<List<dynamic>> imageData =
decodedData.cast<List<dynamic>>();
List<List<int>> intImageData =
imageData.map((list) => list.cast<int>()).toList();
Uint8List imageBytes =
await imageUtils.convert2DListToUint8List(intImageData);
imageCacheProvider.clipartsCache[file.path.split('/').last] = imageBytes;
try {
// Read the file as bytes
Uint8List fileBytes = await file.readAsBytes();
// Decode the bytes to a string using utf-8 encoding
String content = utf8.decode(fileBytes);

if (content.isNotEmpty) {
// Ensure correct type casting
final List<dynamic> decodedData = jsonDecode(content);
final List<List<dynamic>> imageData =
decodedData.cast<List<dynamic>>();
List<List<int>> intImageData =
imageData.map((list) => list.cast<int>()).toList();
Uint8List imageBytes =
await imageUtils.convert2DListToUint8List(intImageData);
imageCacheProvider.clipartsCache[file.path.split('/').last] =
imageBytes;
}
} catch (e) {
logger.i('Error reading or decoding the file: $e');
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions lib/providers/imageprovider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class InlineImageProvider extends ChangeNotifier {

//list of vectors
List<String> vectors = [];

//cache for storing cliparts
Map<String,Uint8List?> clipartsCache = {};
Map<String, Uint8List?> clipartsCache = {};

//uses the AssetManifest class to load the list of assets
Future<void> initVectors() async {
vectors.clear();
try {
final manifestContent = await rootBundle.loadString('AssetManifest.json');
final Map<String, dynamic> manifestMap = json.decode(manifestContent);
Expand All @@ -35,8 +36,6 @@ class InlineImageProvider extends ChangeNotifier {
}
}



//to test the delete operation in TextField
//used for compairing the length of the current textfield and the prevous
//if the length of the current controller length is greater than the previous (add operation)
Expand All @@ -60,6 +59,12 @@ class InlineImageProvider extends ChangeNotifier {
//The cache generation time acts as a delay in the splash screen
Map<Object, Uint8List?> imageCache = {};

void removeFromCache(String key) {
imageCache.remove(key);
logger.d('Removed from cache: ${imageCache.containsKey(key)}');
notifyListeners();
}

//function that generates the image cache
//it fills the map with the Unit8List(byte Array) of the images
Future<void> generateImageCache() async {
Expand Down
2 changes: 1 addition & 1 deletion lib/view/draw_badge_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class _DrawBadgeState extends State<DrawBadge> {
.substring(0, widget.filename!.length - 5),
hexString,
);
fileHelper.generateClipartCache();
fileHelper.generateClipartCache();
},
child: const Column(
children: [
Expand Down
19 changes: 9 additions & 10 deletions lib/view/homescreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,15 @@ class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
Visibility(
visible: isPrefixIconClicked,
child: Container(
height: 99.h,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.r),
color: Colors.grey.shade200,
),
margin: EdgeInsets.symmetric(horizontal: 15.w),
padding: EdgeInsets.symmetric(
vertical: 10.h, horizontal: 10.w),
child: const VectorGridView(),
)),
height: 99.h,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.r),
color: Colors.grey.shade200,
),
margin: EdgeInsets.symmetric(horizontal: 15.w),
padding: EdgeInsets.symmetric(
vertical: 10.h, horizontal: 10.w),
child: VectorGridView())),
TabBar(
indicatorSize: TabBarIndicatorSize.label,
controller: _tabController,
Expand Down
1 change: 1 addition & 0 deletions lib/view/saved_clipart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class _SavedClipartState extends State<SavedClipart> {
setState(() {
logger.i('Clipart $fileName deleted');
});
imageprovider.removeFromCache(fileName);
imageprovider.generateImageCache();
},
), // Use the separate ListView widget here
Expand Down
2 changes: 1 addition & 1 deletion lib/view/widgets/vectorview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ class _VectorGridViewState extends State<VectorGridView> {
itemCount: keys.length,
);
}
}
}

0 comments on commit bd7dbd6

Please sign in to comment.