Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/features/photo/camera/camera_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ class ClassifyLatestPhotoNotifier extends _$ClassifyLatestPhotoNotifier {
final longitude = position?.longitude;

if (latitude != null && longitude != null) {
logger.d('位置情報を取得しました: userId=$userId, photoId=$modifiedPhotoId, 緯度=$latitude, 経度=$longitude');
await ref.read(photoRepositoryProvider).registerStoreInfo(
photoId: modifiedPhotoId,
userId: userId,
latitude: latitude,
longitude: longitude,
);
} else {
logger.w('位置情報の取得に失敗しました: userId=$userId, photoId=$modifiedPhotoId');
}

final photoFile = await latestPhoto.file;
Expand Down
91 changes: 59 additions & 32 deletions lib/features/photo/photo_picker/photo_picker_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import 'dart:io';
import 'dart:typed_data';

import 'package:exif/exif.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:photo_manager/photo_manager.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../../../core/image_helper.dart';
import '../../../core/logger.dart';
import '../../../core/timestamp_converter.dart';
import '../../auth/auth_controller.dart';
import '../local_photo_manager_service.dart';
import '../local_photo_repository.dart';
import '../remote_photo_repository.dart';

part 'photo_picker_controller.g.dart';
Expand Down Expand Up @@ -85,10 +86,9 @@ class ClassifyLocalPhotoNotifier extends _$ClassifyLocalPhotoNotifier {
}

Future<void> classifyPhotoAsFood({
required XFile image,
bool isFood = true,
required AssetEntity photo,
}) async {
final modifiedPhotoId = image.path.split('/').last.replaceAll('/', '-');
final modifiedPhotoId = photo.id.replaceAll('/', '-');
final userId = ref.read(userIdProvider);

if (userId == null) {
Expand All @@ -97,41 +97,68 @@ class ClassifyLocalPhotoNotifier extends _$ClassifyLocalPhotoNotifier {
}

try {
final location = await _getImageLocation(image.path);

if (isFood) {
if (location != null && location.isNotEmpty) {
// サーバーに位置情報を送信
await ref.read(photoRepositoryProvider).registerStoreInfo(
photoId: modifiedPhotoId,
userId: userId,
latitude: location['latitude'],
longitude: location['longitude'],
);
}

// 画像ファイルの圧縮と送信
final photoFile = File(image.path);
final compressedData = await ImageHelper.compress(photoFile);

if (compressedData != null) {
await ref.read(photoRepositoryProvider).categorizeFood(
userId: userId,
photoId: modifiedPhotoId,
photoData: compressedData,
);
}
final photoFile = await photo.file;
if (photoFile == null) {
logger.e('写真ファイルの取得に失敗しました');
return;
}
} on Exception catch (e, stackTrace) {
state = AsyncValue.error(e, stackTrace);
logger.e('Error in classifyPhotoAsFood: $e');

await ref.read(localPhotoRepositoryProvider).savePhoto(
photo: photo,
isFood: true,
);

logger.d('photo_managerパッケージ modifiedPhotoId: $modifiedPhotoId');
logger.d('photo_managerパッケージ photo: $photo');
logger.d('photo_managerパッケージ latitude: ${photo.latitude}');
logger.d('photo_managerパッケージ longitude: ${photo.longitude}');

Map<String, double>? location;
if (Platform.isIOS && photo.latitude != null && photo.longitude != null) {
location = {
'latitude': photo.latitude!,
'longitude': photo.longitude!,
};
} else {
location = await _getImageLocation(photoFile.path);
}
logger.d('位置情報: $location');

if (location != null && location.isNotEmpty) {
// サーバーに位置情報を送信
await ref.read(photoRepositoryProvider).registerStoreInfo(
photoId: modifiedPhotoId,
userId: userId,
latitude: location['latitude'],
longitude: location['longitude'],
);

// 画像ファイルの圧縮と送信
final compressedData = await ImageHelper.compress(photoFile);

if (compressedData != null) {
await ref.read(photoRepositoryProvider).registerPhotoData(
userId: userId,
shotAt: UnionTimestamp.dateTime(photo.createDateTime),
photoId: modifiedPhotoId,
);
await ref.read(photoRepositoryProvider).categorizeFood(
userId: userId,
photoId: modifiedPhotoId,
photoData: compressedData,
);
}
}
} on Exception catch (e, stackTrace) {
state = AsyncValue.error(e, stackTrace);
logger.e('Error in classifyPhotoAsFood: $e');
}
}

Future<Map<String, double>?> _getImageLocation(String imagePath) async {
try {
final file = File(imagePath);
final bytes = await file.readAsBytes();
final bytes = file.readAsBytesSync();

// EXIFデータを解析
final data = await readExifFromBytes(bytes);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 4 additions & 16 deletions lib/features/photo/photo_picker/photo_picker_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:gap/gap.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';

import '../../../core/logger.dart';
import '../../../core/permission/permission_handler.dart';
import '../../../core/themes.dart';

Expand Down Expand Up @@ -243,26 +241,16 @@ class PhotoPickerPage extends HookConsumerWidget {

// 非同期で分類処理を実行(バックグラウンド処理)
await Future.microtask(() async {
final selectedPhotos =
final selectedPhotos =
selectedLocalPhotos.toList();
final photoListNotifier = ref.read(
classifyLocalPhotoNotifierProvider.notifier,
);

final tasks = selectedPhotos.map((photo) async {
try {
final file = await photo.file;

if (file != null) {
final xFile = XFile(file.path);
await photoListNotifier.classifyPhotoAsFood(
image: xFile,
);
}
} on Exception catch (e) {
logger
.e('Error classify processing photo: $e');
}
await photoListNotifier.classifyPhotoAsFood(
photo: photo,
);
}).toList();

unawaited(Future.wait(tasks));
Expand Down
Loading