Compare commits

...

2 Commits

Author SHA1 Message Date
Frédérik Benoist 1ce0d03198 feat: image rotation (suite) 2023-03-30 08:50:16 +02:00
Frédérik Benoist d063256dce feat: image rotation 2023-03-30 08:49:14 +02:00
9 changed files with 224 additions and 89 deletions

View File

@ -14,6 +14,7 @@ class Photo {
int id_visite;
int id_photo_typologie;
String image;
String image_name;
DateTime date_photo;
int id_photo_mp4;
int photo_privee;
@ -29,6 +30,7 @@ class Photo {
required this.id_visite,
required this.id_photo_typologie,
required this.image,
required this.image_name,
this.id_photo = 0,
this.id_photo_mp4 = 0,
this.photo_privee = 0,

View File

@ -1,6 +1,7 @@
// ignore_for_file: prefer_const_constructors
import 'dart:ui';
import 'dart:io';
import 'package:mobdr/config/constant.dart';
@ -16,6 +17,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:path_provider/path_provider.dart';
import 'objectbox.dart';
import 'package:wakelock/wakelock.dart';
@ -59,6 +61,8 @@ class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
initDirectories();
// Initialize all bloc provider used on this entire application here
return MultiBlocProvider(
providers: [
@ -117,4 +121,23 @@ class MyApp extends StatelessWidget {
),
);
}
void initDirectories() async {
// Get the application's document directory
final Directory documentsDir = await getApplicationDocumentsDirectory();
// Create a Directory object for the "photos" directory in the documents directory
final Directory photosDir = Directory('${documentsDir.path}/photos');
// Check if the "photos" directory exists
if (await photosDir.exists()) {
print(
'The "photos" directory already exists in the documents directory.');
} else {
// Create the "photos" directory if it does not exist
await photosDir.create();
print(
'The "photos" directory has been created in the documents directory.');
}
}
}

View File

@ -1,12 +1,12 @@
class PhotoModel {
late int id;
late int id_visite;
late int id_photo_typologie;
late String image;
late String image_name;
PhotoModel(
{required this.id,
required this.id_visite,
{required this.id_visite,
required this.id_photo_typologie,
required this.image});
required this.image,
required this.image_name});
}

View File

@ -283,7 +283,7 @@
},
{
"id": "9:6788844671665652158",
"lastPropertyId": "15:1865824860595482227",
"lastPropertyId": "16:539065583624712715",
"name": "Photo",
"properties": [
{
@ -356,6 +356,11 @@
"id": "15:1865824860595482227",
"name": "image",
"type": 9
},
{
"id": "16:539065583624712715",
"name": "image_name",
"type": 9
}
],
"relations": []

View File

@ -68,7 +68,7 @@ class ObjectBox {
concurrentBox.removeAll();
visiteBox.removeAll();
visiteTagBox.removeAll();
photoBox.removeAll();
//photoBox.removeAll();
//photoTypologyBox.removeAll();
// Add some demo data if the box is empty.
@ -343,17 +343,19 @@ class ObjectBox {
/// PHOTO --------------------------------------------------------------------
///
void addPhotos(List<Photo> _listPhotos) {
store.box<Photo>().putMany(_listPhotos);
store.box<Photo>().putManyAsync(_listPhotos);
}
Future<void> addPhoto(int id_visite, int id_photo_typologie, String image) =>
Future<void> addPhoto(int id_visite, int id_photo_typologie, String image,
String image_name) =>
store.runInTransactionAsync(
TxMode.write,
_addPhotoInTx,
Photo(
id_visite: id_visite,
id_photo_typologie: id_photo_typologie,
image: image));
image: image,
image_name: image_name));
static void _addPhotoInTx(Store store, _Photo) {
// Perform ObjectBox operations that take longer than a few milliseconds
@ -384,6 +386,15 @@ class ObjectBox {
.map((query) => query.find());
}
void delPhoto(String _name) {
final query = photoBox.query(Photo_.image_name.equals(_name)).build();
final results = query.find();
if (results.isNotEmpty) {
final photoToDelete = results.first;
photoBox.removeAsync(photoToDelete.id);
}
}
int getVisitPhotoCount(int _id_visite, int _id_Photo_typologie) {
final builder = photoBox
.query(Photo_.id_visite.equals(_id_visite) &

View File

@ -308,7 +308,7 @@ final _entities = <ModelEntity>[
ModelEntity(
id: const IdUid(9, 6788844671665652158),
name: 'Photo',
lastPropertyId: const IdUid(15, 1865824860595482227),
lastPropertyId: const IdUid(16, 539065583624712715),
flags: 0,
properties: <ModelProperty>[
ModelProperty(
@ -380,6 +380,11 @@ final _entities = <ModelEntity>[
id: const IdUid(15, 1865824860595482227),
name: 'image',
type: 9,
flags: 0),
ModelProperty(
id: const IdUid(16, 539065583624712715),
name: 'image_name',
type: 9,
flags: 0)
],
relations: <ModelRelation>[],
@ -760,7 +765,8 @@ ModelDefinition getObjectBoxModel() {
final photo_tag3Offset = fbb.writeString(object.photo_tag3);
final photo_tag4Offset = fbb.writeString(object.photo_tag4);
final imageOffset = fbb.writeString(object.image);
fbb.startTable(16);
final image_nameOffset = fbb.writeString(object.image_name);
fbb.startTable(17);
fbb.addInt64(0, object.id);
fbb.addInt64(1, object.id_photo);
fbb.addInt64(2, object.id_visite);
@ -775,6 +781,7 @@ ModelDefinition getObjectBoxModel() {
fbb.addOffset(12, photo_tag4Offset);
fbb.addInt64(13, object.uploaded);
fbb.addOffset(14, imageOffset);
fbb.addOffset(15, image_nameOffset);
fbb.finish(fbb.endTable());
return object.id;
},
@ -790,6 +797,8 @@ ModelDefinition getObjectBoxModel() {
const fb.Int64Reader().vTableGet(buffer, rootOffset, 10, 0),
image: const fb.StringReader(asciiOptimization: true)
.vTableGet(buffer, rootOffset, 32, ''),
image_name: const fb.StringReader(asciiOptimization: true)
.vTableGet(buffer, rootOffset, 34, ''),
id_photo:
const fb.Int64Reader().vTableGet(buffer, rootOffset, 6, 0),
id_photo_mp4:
@ -802,10 +811,9 @@ ModelDefinition getObjectBoxModel() {
.vTableGet(buffer, rootOffset, 22, ''),
photo_tag2: const fb.StringReader(asciiOptimization: true)
.vTableGet(buffer, rootOffset, 24, ''),
photo_tag3: const fb.StringReader(asciiOptimization: true)
.vTableGet(buffer, rootOffset, 26, ''),
photo_tag4: const fb.StringReader(asciiOptimization: true)
.vTableGet(buffer, rootOffset, 28, ''),
photo_tag3:
const fb.StringReader(asciiOptimization: true).vTableGet(buffer, rootOffset, 26, ''),
photo_tag4: const fb.StringReader(asciiOptimization: true).vTableGet(buffer, rootOffset, 28, ''),
date_photo: DateTime.fromMillisecondsSinceEpoch(const fb.Int64Reader().vTableGet(buffer, rootOffset, 14, 0)),
uploaded: const fb.Int64Reader().vTableGet(buffer, rootOffset, 30, 0));
@ -1072,6 +1080,10 @@ class Photo_ {
/// see [Photo.image]
static final image = QueryStringProperty<Photo>(_entities[7].properties[13]);
/// see [Photo.image_name]
static final image_name =
QueryStringProperty<Photo>(_entities[7].properties[14]);
}
/// [PhotoTypology] entity fields to define ObjectBox queries.

View File

@ -1,19 +1,32 @@
import 'dart:developer' as developer;
import 'dart:async';
import 'dart:io';
import 'package:mobdr/config/constant.dart';
import 'package:mobdr/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:mobdr/model/photo_model.dart';
import 'package:mobdr/ui/reusable/cache_image_network.dart';
import 'package:image/image.dart' as img;
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:flutter/painting.dart' show ImageProvider;
import 'package:mobdr/config/constant.dart';
import 'package:mobdr/main.dart';
import 'package:mobdr/ui/reusable/global_widget.dart';
import 'package:mobdr/ui/home/photo_camera.dart';
import 'package:mobdr/objectbox.dart';
import 'package:mobdr/model/photo_model.dart';
import 'package:mobdr/db/box_photo.dart';
// TODO Il faut supprimer les possibles photos du répertoire cache !
extension FileNameExtension on File {
String getFileName() {
String fileName = path.split('/').last;
return fileName;
}
}
class PhotoListPage extends StatefulWidget {
@override
_PhotoListPageState createState() => _PhotoListPageState();
@ -46,10 +59,10 @@ class _PhotoListPageState extends State<PhotoListPage> {
void _loadData() {
for (Photo myPhoto in objectbox.getPhotos2()) {
_photoData.add(PhotoModel(
id: myPhoto.id,
id_visite: myPhoto.id_visite,
id_photo_typologie: myPhoto.id_photo_typologie,
image: myPhoto.image));
image: myPhoto.image,
image_name: myPhoto.image_name));
}
}
@ -89,9 +102,7 @@ class _PhotoListPageState extends State<PhotoListPage> {
}),
],
),
body: RefreshIndicator(
onRefresh: refreshData,
child: AnimatedList(
body: AnimatedList(
key: _listKey,
initialItemCount: _photoData.length,
physics: AlwaysScrollableScrollPhysics(),
@ -100,7 +111,6 @@ class _PhotoListPageState extends State<PhotoListPage> {
_photoData[index], boxImageSize, animation, index);
},
),
),
floatingActionButton: fabCart(context));
}
@ -132,12 +142,8 @@ class _PhotoListPageState extends State<PhotoListPage> {
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(10)),
child: Image.file(
File(photoData.image),
fit: BoxFit.cover,
height: 100,
width: 150,
),
child: Image.file(File(photoData.image),
fit: BoxFit.cover, height: 100, width: 150),
),
SizedBox(
width: 10,
@ -165,10 +171,7 @@ class _PhotoListPageState extends State<PhotoListPage> {
children: [
Icon(Icons.location_on,
color: SOFT_GREY, size: 12),
Text(
' ' +
photoData.id_photo_typologie
.toString(),
Text(' ' + photoData.image_name,
style: TextStyle(
fontSize: 11, color: SOFT_GREY))
],
@ -210,7 +213,7 @@ class _PhotoListPageState extends State<PhotoListPage> {
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
showPopupDeleteFavorite(index, boxImageSize);
showPopupDeletePhoto(index, boxImageSize);
},
child: Container(
padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
@ -284,8 +287,14 @@ class _PhotoListPageState extends State<PhotoListPage> {
),
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
showPopupDeleteFavorite(index, boxImageSize);
onTap: () async {
await rotateAndReplaceImage(
File(_photoData[index].image), 90);
setState(() {
_listKey = GlobalKey();
});
Fluttertoast.showToast(
msg: 'The image has been rotated');
},
child: Container(
padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
@ -303,8 +312,14 @@ class _PhotoListPageState extends State<PhotoListPage> {
),
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
showPopupDeleteFavorite(index, boxImageSize);
onTap: () async {
await rotateAndReplaceImage(
File(_photoData[index].image), -90);
setState(() {
_listKey = GlobalKey();
});
Fluttertoast.showToast(
msg: 'The image has been rotated');
},
child: Container(
padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
@ -331,14 +346,13 @@ class _PhotoListPageState extends State<PhotoListPage> {
Widget fabCart(context) {
return FloatingActionButton(
onPressed: () {
photoFiles.clear();
Route route = MaterialPageRoute(
builder: (context) => CameraPage(photoFiles: photoFiles));
Navigator.push(context, route).then((val) {
/// if the user has validated photos
if (val == true) {
savePhotos();
} else {
deletePhotos();
}
});
},
@ -376,35 +390,61 @@ class _PhotoListPageState extends State<PhotoListPage> {
);
}
void savePhotos() {
print("Save PHOTOS ----:" + photoFiles.length.toString());
Future<bool> evictImage(String imageURL) async {
final NetworkImage provider = NetworkImage(imageURL);
return await provider.evict();
}
void savePhotos() async {
if (photoFiles.length > 0) {
final List<Photo> _listPhotos = [];
final List<PhotoModel> _listPhotosModel = [];
for (var photo in photoFiles) {
_listPhotos
.add(Photo(id_visite: 0, id_photo_typologie: 0, image: photo.path));
for (var myTmpPhoto in photoFiles) {
/// move jpg file to photo directory
final myPhoto = await moveFileFromTempToPhotosDir(myTmpPhoto);
/// database
_listPhotos.add(Photo(
id_visite: 0,
id_photo_typologie: 0,
image: myPhoto.path,
image_name: myPhoto.path.split('/').last));
/// widget
_listPhotosModel.add(PhotoModel(
id_visite: 0,
id_photo_typologie: 0,
image: myPhoto.path,
image_name: myPhoto.path.split('/').last));
}
/// insert photo(s) in database (async)
objectbox.addPhotos(_listPhotos);
/// refresh widget PhotoListPage
refreshData();
}
}
/// insert photo(s) in widget
_photoData.insertAll(0, _listPhotosModel);
void deletePhotos() {
print("DELETE PHOTO ------------");
// delete files on local storage
photoFiles.forEach((element) {
//deleteFile(File(element.path));
//element.delete();
/// refresh widget
setState(() {
_listKey = GlobalKey();
});
}
}
void showPopupDeleteFavorite(index, boxImageSize) {
Future<void> deleteFile(File file) async {
try {
if (await file.exists()) {
await file.delete();
}
} catch (e) {
// Error in getting access to the file.
// todo toast
print(e.toString());
}
}
void showPopupDeletePhoto(index, boxImageSize) {
// set up the buttons
Widget cancelButton = TextButton(
onPressed: () {
@ -415,6 +455,13 @@ class _PhotoListPageState extends State<PhotoListPage> {
onPressed: () {
int removeIndex = index;
var removedItem = _photoData.removeAt(removeIndex);
// delete file on database
objectbox.delPhoto(removedItem.image_name);
// delete file on local storage
deleteFile(new File(removedItem.image));
// This builder is just so that the animation has something
// to work with before it disappears from view since the original
// has already been deleted.
@ -427,7 +474,7 @@ class _PhotoListPageState extends State<PhotoListPage> {
Navigator.pop(context);
Fluttertoast.showToast(
msg: 'Item has been deleted from your favorite',
msg: 'Photo has been deleted from your visit',
toastLength: Toast.LENGTH_SHORT);
},
child: Text('Yes', style: TextStyle(color: SOFT_BLUE)));
@ -438,10 +485,10 @@ class _PhotoListPageState extends State<PhotoListPage> {
borderRadius: BorderRadius.circular(10),
),
title: Text(
'Delete Favorite',
'Delete Photo',
style: TextStyle(fontSize: 18),
),
content: Text('Are you sure to delete this item from your Favorite ?',
content: Text('Are you sure to delete this photo from your visit ?',
style: TextStyle(fontSize: 13, color: _color1)),
actions: [
cancelButton,
@ -458,28 +505,38 @@ class _PhotoListPageState extends State<PhotoListPage> {
);
}
Future<void> deleteFile(File file) async {
try {
if (await file.exists()) {
await file.delete();
}
} catch (e) {
// Error in getting access to the file.
// todo toast
print(e.toString());
}
Future<File> moveFileFromTempToPhotosDir(File tempFile) async {
// Get the application's document directory
final Directory documentsDir = await getApplicationDocumentsDirectory();
// Set the new file path with the original file name
final String newPath =
'${documentsDir.path}/photos/${tempFile.path.split('/').last}';
// Rename the file to move it to the documents directory
await tempFile.rename(newPath);
// Create and return a new File object for the new file
return File(newPath);
}
Future refreshData() async {
/// clear all data
/// TODO on pourrait simplement ajouter les nouvelles photos (bien mieux)
_photoData.clear();
photoFiles.clear();
Future<void> rotateAndReplaceImage(File imageFile, int angle) async {
// Read the image file into a Uint8List
Uint8List bytes = await imageFile.readAsBytes();
/// reload all photo data from database
_loadData();
// Decode the bytes into an Image object using the image package
img.Image? image = img.decodeImage(bytes);
/// reinitialiez AnimatedList widget
setState(() => _listKey = GlobalKey());
// Rotate the image clockwise by 90 degrees
img.Image? rotatedImage = img.copyRotate(image!, angle: angle);
// Encode the rotated image back into a Uint8List
Uint8List rotatedBytes = img.encodePng(rotatedImage);
// Overwrite the original file with the rotated image
await imageFile.writeAsBytes(rotatedBytes);
// remove the flutter cache image
FileImage(imageFile).evict();
}
}

View File

@ -17,6 +17,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.6.0"
archive:
dependency: transitive
description:
name: archive
sha256: d6347d54a2d8028e0437e3c099f66fdb8ae02c4720c1e7534c9f24c10351f85d
url: "https://pub.dev"
source: hosted
version: "3.3.6"
args:
dependency: transitive
description:
@ -461,6 +469,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.2"
image:
dependency: "direct main"
description:
name: image
sha256: "483a389d6ccb292b570c31b3a193779b1b0178e7eb571986d9a49904b6861227"
url: "https://pub.dev"
source: hosted
version: "4.0.15"
image_picker:
dependency: "direct main"
description:
@ -781,6 +797,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
pointycastle:
dependency: transitive
description:
name: pointycastle
sha256: c3120a968135aead39699267f4c74bc9a08e4e909e86bc1b0af5bfd78691123c
url: "https://pub.dev"
source: hosted
version: "3.7.2"
pool:
dependency: transitive
description:

View File

@ -53,6 +53,7 @@ dependencies:
image_picker: ^0.8.7
camera: ^0.10.3+2
permission_handler: 10.2.0
image: ^4.0.15
flutter_cache_manager: ^3.3.0