exif_bottom_sheet.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import 'dart:io';
  2. import 'package:easy_localization/easy_localization.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_map/flutter_map.dart';
  5. import 'package:hooks_riverpod/hooks_riverpod.dart';
  6. import 'package:immich_mobile/modules/asset_viewer/ui/description_input.dart';
  7. import 'package:immich_mobile/modules/map/ui/map_thumbnail.dart';
  8. import 'package:immich_mobile/shared/models/asset.dart';
  9. import 'package:immich_mobile/shared/ui/drag_sheet.dart';
  10. import 'package:latlong2/latlong.dart';
  11. import 'package:immich_mobile/utils/bytes_units.dart';
  12. import 'package:url_launcher/url_launcher.dart';
  13. class ExifBottomSheet extends HookConsumerWidget {
  14. final Asset asset;
  15. const ExifBottomSheet({Key? key, required this.asset}) : super(key: key);
  16. bool get hasCoordinates =>
  17. asset.exifInfo?.latitude != null && asset.exifInfo?.longitude != null;
  18. String get formattedDateTime {
  19. final fileCreatedAt = asset.fileCreatedAt.toLocal();
  20. final date = DateFormat.yMMMEd().format(fileCreatedAt);
  21. final time = DateFormat.jm().format(fileCreatedAt);
  22. return '$date • $time';
  23. }
  24. Future<Uri?> _createCoordinatesUri() async {
  25. if (!hasCoordinates) {
  26. return null;
  27. }
  28. double latitude = asset.exifInfo!.latitude!;
  29. double longitude = asset.exifInfo!.longitude!;
  30. const zoomLevel = 16;
  31. if (Platform.isAndroid) {
  32. Uri uri = Uri(
  33. scheme: 'geo',
  34. host: '$latitude,$longitude',
  35. queryParameters: {
  36. 'z': '$zoomLevel',
  37. 'q': '$latitude,$longitude($formattedDateTime)',
  38. },
  39. );
  40. if (await canLaunchUrl(uri)) {
  41. return uri;
  42. }
  43. } else if (Platform.isIOS) {
  44. var params = {
  45. 'll': '$latitude,$longitude',
  46. 'q': formattedDateTime,
  47. 'z': '$zoomLevel',
  48. };
  49. Uri uri = Uri.https('maps.apple.com', '/', params);
  50. if (await canLaunchUrl(uri)) {
  51. return uri;
  52. }
  53. }
  54. return Uri(
  55. scheme: 'https',
  56. host: 'openstreetmap.org',
  57. queryParameters: {'mlat': '$latitude', 'mlon': '$longitude'},
  58. fragment: 'map=$zoomLevel/$latitude/$longitude',
  59. );
  60. }
  61. @override
  62. Widget build(BuildContext context, WidgetRef ref) {
  63. final exifInfo = asset.exifInfo;
  64. var isDarkTheme = Theme.of(context).brightness == Brightness.dark;
  65. var textColor = isDarkTheme ? Colors.white : Colors.black;
  66. buildMap() {
  67. return Padding(
  68. padding: const EdgeInsets.symmetric(vertical: 16.0),
  69. child: LayoutBuilder(
  70. builder: (context, constraints) {
  71. return MapThumbnail(
  72. coords: LatLng(
  73. exifInfo?.latitude ?? 0,
  74. exifInfo?.longitude ?? 0,
  75. ),
  76. height: 150,
  77. zoom: 16.0,
  78. markers: [
  79. Marker(
  80. anchorPos: AnchorPos.align(AnchorAlign.top),
  81. point: LatLng(
  82. exifInfo?.latitude ?? 0,
  83. exifInfo?.longitude ?? 0,
  84. ),
  85. builder: (ctx) => const Image(
  86. image: AssetImage('assets/location-pin.png'),
  87. ),
  88. ),
  89. ],
  90. onTap: (tapPosition, latLong) async {
  91. Uri? uri = await _createCoordinatesUri();
  92. if (uri == null) {
  93. return;
  94. }
  95. debugPrint('Opening Map Uri: $uri');
  96. launchUrl(uri);
  97. },
  98. );
  99. },
  100. ),
  101. );
  102. }
  103. buildSizeText(Asset a) {
  104. String resolution = a.width != null && a.height != null
  105. ? "${a.height} x ${a.width} "
  106. : "";
  107. String fileSize = a.exifInfo?.fileSize != null
  108. ? formatBytes(a.exifInfo!.fileSize!)
  109. : "";
  110. String text = resolution + fileSize;
  111. return text.isEmpty ? null : Text(text);
  112. }
  113. buildDragHeader() {
  114. return const Column(
  115. crossAxisAlignment: CrossAxisAlignment.start,
  116. children: [
  117. SizedBox(height: 12),
  118. Align(
  119. alignment: Alignment.center,
  120. child: CustomDraggingHandle(),
  121. ),
  122. SizedBox(height: 12),
  123. ],
  124. );
  125. }
  126. buildLocation() {
  127. // Guard no lat/lng
  128. if (!hasCoordinates) {
  129. return Container();
  130. }
  131. return Column(
  132. children: [
  133. // Location
  134. Column(
  135. crossAxisAlignment: CrossAxisAlignment.start,
  136. children: [
  137. Text(
  138. "exif_bottom_sheet_location",
  139. style: TextStyle(
  140. fontSize: 11,
  141. color: textColor,
  142. fontWeight: FontWeight.bold,
  143. ),
  144. ).tr(),
  145. buildMap(),
  146. RichText(
  147. text: TextSpan(
  148. style: TextStyle(
  149. fontSize: 12,
  150. fontWeight: FontWeight.bold,
  151. color: textColor,
  152. fontFamily: 'WorkSans',
  153. ),
  154. children: [
  155. if (exifInfo != null && exifInfo.city != null)
  156. TextSpan(
  157. text: exifInfo.city,
  158. ),
  159. if (exifInfo != null &&
  160. exifInfo.city != null &&
  161. exifInfo.state != null)
  162. const TextSpan(
  163. text: ", ",
  164. ),
  165. if (exifInfo != null && exifInfo.state != null)
  166. TextSpan(
  167. text: "${exifInfo.state}",
  168. ),
  169. ],
  170. ),
  171. ),
  172. Text(
  173. "${exifInfo!.latitude!.toStringAsFixed(4)}, ${exifInfo.longitude!.toStringAsFixed(4)}",
  174. style: const TextStyle(fontSize: 12),
  175. ),
  176. ],
  177. ),
  178. ],
  179. );
  180. }
  181. buildDate() {
  182. return Text(
  183. formattedDateTime,
  184. style: const TextStyle(
  185. fontWeight: FontWeight.bold,
  186. fontSize: 14,
  187. ),
  188. );
  189. }
  190. buildDetail() {
  191. return Column(
  192. crossAxisAlignment: CrossAxisAlignment.start,
  193. children: [
  194. Padding(
  195. padding: const EdgeInsets.only(bottom: 8.0),
  196. child: Text(
  197. "exif_bottom_sheet_details",
  198. style: TextStyle(
  199. fontSize: 11,
  200. color: textColor,
  201. fontWeight: FontWeight.bold,
  202. ),
  203. ).tr(),
  204. ),
  205. ListTile(
  206. contentPadding: const EdgeInsets.all(0),
  207. dense: true,
  208. leading: Icon(
  209. Icons.image,
  210. color: textColor.withAlpha(200),
  211. ),
  212. title: Text(
  213. asset.fileName,
  214. style: TextStyle(
  215. fontWeight: FontWeight.bold,
  216. color: textColor,
  217. ),
  218. ),
  219. subtitle: buildSizeText(asset),
  220. ),
  221. if (exifInfo?.make != null)
  222. ListTile(
  223. contentPadding: const EdgeInsets.all(0),
  224. dense: true,
  225. leading: Icon(
  226. Icons.camera,
  227. color: textColor.withAlpha(200),
  228. ),
  229. title: Text(
  230. "${exifInfo!.make} ${exifInfo.model}",
  231. style: TextStyle(
  232. color: textColor,
  233. fontWeight: FontWeight.bold,
  234. ),
  235. ),
  236. subtitle: Text(
  237. "ƒ/${exifInfo.fNumber} ${exifInfo.exposureTime} ${exifInfo.focalLength} mm ISO${exifInfo.iso} ",
  238. ),
  239. ),
  240. ],
  241. );
  242. }
  243. return GestureDetector(
  244. onTap: () {
  245. // FocusScope.of(context).unfocus();
  246. },
  247. child: SingleChildScrollView(
  248. child: Card(
  249. shape: const RoundedRectangleBorder(
  250. borderRadius: BorderRadius.only(
  251. topLeft: Radius.circular(15),
  252. topRight: Radius.circular(15),
  253. ),
  254. ),
  255. margin: const EdgeInsets.all(0),
  256. child: Container(
  257. margin: const EdgeInsets.symmetric(horizontal: 16.0),
  258. child: LayoutBuilder(
  259. builder: (context, constraints) {
  260. if (constraints.maxWidth > 600) {
  261. // Two column
  262. return Padding(
  263. padding: const EdgeInsets.symmetric(horizontal: 12.0),
  264. child: Column(
  265. crossAxisAlignment: CrossAxisAlignment.stretch,
  266. children: [
  267. buildDragHeader(),
  268. buildDate(),
  269. if (asset.isRemote) DescriptionInput(asset: asset),
  270. Row(
  271. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  272. crossAxisAlignment: CrossAxisAlignment.start,
  273. children: [
  274. Flexible(
  275. flex: hasCoordinates ? 5 : 0,
  276. child: Padding(
  277. padding: const EdgeInsets.only(right: 8.0),
  278. child: buildLocation(),
  279. ),
  280. ),
  281. Flexible(
  282. flex: 5,
  283. child: Padding(
  284. padding: const EdgeInsets.only(left: 8.0),
  285. child: buildDetail(),
  286. ),
  287. ),
  288. ],
  289. ),
  290. const SizedBox(height: 50),
  291. ],
  292. ),
  293. );
  294. }
  295. // One column
  296. return Column(
  297. crossAxisAlignment: CrossAxisAlignment.stretch,
  298. children: [
  299. buildDragHeader(),
  300. buildDate(),
  301. if (asset.isRemote) DescriptionInput(asset: asset),
  302. const SizedBox(height: 8.0),
  303. buildLocation(),
  304. SizedBox(height: hasCoordinates ? 16.0 : 0.0),
  305. buildDetail(),
  306. const SizedBox(height: 50),
  307. ],
  308. );
  309. },
  310. ),
  311. ),
  312. ),
  313. ),
  314. );
  315. }
  316. }