From ef097d15dd1910de2c11ef9f0ff4a15db87f1d61 Mon Sep 17 00:00:00 2001 From: xpwmaosldk Date: Thu, 23 Jun 2022 13:14:14 +0900 Subject: [PATCH] Clean code of shared folder (#249) * optimize android side gradle settings * android minsdk back to 21 * remove unused package, update linter and fix lint error * clean code of 'shared module' with offical dart style guide * restore uploadProfileImage method in UserService --- docker/docker-compose.yml | 8 +-- mobile/lib/main.dart | 7 ++- .../lib/shared/providers/asset.provider.dart | 4 +- .../providers/release_info.provider.dart | 2 +- .../providers/server_info.provider.dart | 11 ++-- .../shared/services/device_info.service.dart | 4 +- .../lib/shared/services/network.service.dart | 57 ++++++++----------- mobile/lib/shared/services/user.service.dart | 4 +- mobile/lib/shared/ui/immich_toast.dart | 45 ++++++++------- .../shared/views/immich_loading_overlay.dart | 24 ++++---- .../views/version_announcement_overlay.dart | 35 ++++++------ mobile/pubspec.yaml | 2 +- 12 files changed, 100 insertions(+), 103 deletions(-) diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index d6e90dd864..fe9be0b026 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -3,7 +3,7 @@ version: "3.8" services: immich-server: image: altran1502/immich-server:latest - entrypoint: ["/bin/sh", "./start-server.sh"] + entrypoint: [ "/bin/sh", "./start-server.sh" ] expose: - "3000" volumes: @@ -21,7 +21,7 @@ services: immich-microservices: image: altran1502/immich-server:latest - entrypoint: ["/bin/sh", "./start-microservices.sh"] + entrypoint: [ "/bin/sh", "./start-microservices.sh" ] volumes: - ${UPLOAD_LOCATION}:/usr/src/app/upload env_file: @@ -37,7 +37,7 @@ services: immich-machine-learning: image: altran1502/immich-machine-learning:latest - entrypoint: ["/bin/sh", "./entrypoint.sh"] + entrypoint: [ "/bin/sh", "./entrypoint.sh" ] expose: - "3001" volumes: @@ -54,7 +54,7 @@ services: immich-web: image: altran1502/immich-web:latest - entrypoint: ["/bin/sh", "./entrypoint.sh"] + entrypoint: [ "/bin/sh", "./entrypoint.sh" ] env_file: - .env ports: diff --git a/mobile/lib/main.dart b/mobile/lib/main.dart index d256934e3b..144bddd278 100644 --- a/mobile/lib/main.dart +++ b/mobile/lib/main.dart @@ -4,18 +4,19 @@ import 'package:hive_flutter/hive_flutter.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:immich_mobile/constants/immich_colors.dart'; import 'package:immich_mobile/modules/backup/models/hive_backup_albums.model.dart'; +import 'package:immich_mobile/modules/backup/providers/backup.provider.dart'; import 'package:immich_mobile/modules/login/models/hive_saved_login_info.model.dart'; import 'package:immich_mobile/modules/login/providers/authentication.provider.dart'; -import 'package:immich_mobile/shared/providers/asset.provider.dart'; import 'package:immich_mobile/routing/router.dart'; import 'package:immich_mobile/routing/tab_navigation_observer.dart'; import 'package:immich_mobile/shared/providers/app_state.provider.dart'; -import 'package:immich_mobile/modules/backup/providers/backup.provider.dart'; +import 'package:immich_mobile/shared/providers/asset.provider.dart'; import 'package:immich_mobile/shared/providers/release_info.provider.dart'; import 'package:immich_mobile/shared/providers/server_info.provider.dart'; import 'package:immich_mobile/shared/providers/websocket.provider.dart'; import 'package:immich_mobile/shared/views/immich_loading_overlay.dart'; import 'package:immich_mobile/shared/views/version_announcement_overlay.dart'; + import 'constants/hive_box.dart'; void main() async { @@ -39,7 +40,7 @@ void main() async { } class ImmichApp extends ConsumerStatefulWidget { - const ImmichApp({Key? key}) : super(key: key); + const ImmichApp({super.key}); @override ImmichAppState createState() => ImmichAppState(); diff --git a/mobile/lib/shared/providers/asset.provider.dart b/mobile/lib/shared/providers/asset.provider.dart index ab9c09a08a..c362b3b0c2 100644 --- a/mobile/lib/shared/providers/asset.provider.dart +++ b/mobile/lib/shared/providers/asset.provider.dart @@ -33,12 +33,12 @@ class AssetNotifier extends StateNotifier> { deleteAssets(Set deleteAssets) async { var deviceInfo = await _deviceInfoService.getDeviceInfo(); var deviceId = deviceInfo["deviceId"]; - List deleteIdList = []; + var deleteIdList = []; // Delete asset from device for (var asset in deleteAssets) { // Delete asset on device if present if (asset.deviceId == deviceId) { - AssetEntity? localAsset = await AssetEntity.fromId(asset.deviceAssetId); + var localAsset = await AssetEntity.fromId(asset.deviceAssetId); if (localAsset != null) { deleteIdList.add(localAsset.id); diff --git a/mobile/lib/shared/providers/release_info.provider.dart b/mobile/lib/shared/providers/release_info.provider.dart index 16659960e4..65276dd737 100644 --- a/mobile/lib/shared/providers/release_info.provider.dart +++ b/mobile/lib/shared/providers/release_info.provider.dart @@ -15,7 +15,7 @@ class ReleaseInfoNotifier extends StateNotifier { try { String? localReleaseVersion = box.get(githubReleaseInfoKey); - Response res = await dio.get( + var res = await dio.get( "https://api.github.com/repos/alextran1502/immich/releases/latest", options: Options( headers: {"Accept": "application/vnd.github.v3+json"}, diff --git a/mobile/lib/shared/providers/server_info.provider.dart b/mobile/lib/shared/providers/server_info.provider.dart index 36abea4f31..00ee4aec7f 100644 --- a/mobile/lib/shared/providers/server_info.provider.dart +++ b/mobile/lib/shared/providers/server_info.provider.dart @@ -11,7 +11,8 @@ class ServerInfoNotifier extends StateNotifier { : super( ServerInfoState( mapboxInfo: MapboxInfo(isEnable: false, mapboxSecret: ""), - serverVersion: ServerVersion(major: 0, patch: 0, minor: 0, build: 0), + serverVersion: + ServerVersion(major: 0, patch: 0, minor: 0, build: 0), isVersionMismatch: false, versionMismatchErrorMessage: "", ), @@ -33,7 +34,7 @@ class ServerInfoNotifier extends StateNotifier { state = state.copyWith(serverVersion: serverVersion); - PackageInfo packageInfo = await PackageInfo.fromPlatform(); + var packageInfo = await PackageInfo.fromPlatform(); Map appVersion = _getDetailVersion(packageInfo.version); @@ -57,7 +58,8 @@ class ServerInfoNotifier extends StateNotifier { return; } - state = state.copyWith(isVersionMismatch: false, versionMismatchErrorMessage: ""); + state = state.copyWith( + isVersionMismatch: false, versionMismatchErrorMessage: ""); } Map _getDetailVersion(String version) { @@ -75,6 +77,7 @@ class ServerInfoNotifier extends StateNotifier { } } -final serverInfoProvider = StateNotifierProvider((ref) { +final serverInfoProvider = + StateNotifierProvider((ref) { return ServerInfoNotifier(); }); diff --git a/mobile/lib/shared/services/device_info.service.dart b/mobile/lib/shared/services/device_info.service.dart index 124daacf5b..30762ae348 100644 --- a/mobile/lib/shared/services/device_info.service.dart +++ b/mobile/lib/shared/services/device_info.service.dart @@ -4,8 +4,8 @@ import 'dart:io' show Platform; class DeviceInfoService { Future> getDeviceInfo() async { // Get device info - String deviceId = await FlutterUdid.consistentUdid; - String deviceType = ""; + var deviceId = await FlutterUdid.consistentUdid; + var deviceType = ""; if (Platform.isAndroid) { deviceType = "ANDROID"; diff --git a/mobile/lib/shared/services/network.service.dart b/mobile/lib/shared/services/network.service.dart index 206099aadd..5c53f2580b 100644 --- a/mobile/lib/shared/services/network.service.dart +++ b/mobile/lib/shared/services/network.service.dart @@ -4,15 +4,22 @@ import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:hive/hive.dart'; +import 'package:http_parser/http_parser.dart'; +import 'package:image_picker/image_picker.dart'; import 'package:immich_mobile/constants/hive_box.dart'; import 'package:immich_mobile/utils/dio_http_interceptor.dart'; +import 'package:immich_mobile/utils/files_helper.dart'; class NetworkService { + late final Dio dio; + + NetworkService() { + dio = Dio(); + dio.interceptors.add(AuthenticatedRequestInterceptor()); + } + Future deleteRequest({required String url, dynamic data}) async { try { - var dio = Dio(); - dio.interceptors.add(AuthenticatedRequestInterceptor()); - var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey); Response res = await dio.delete('$savedEndpoint/$url', data: data); @@ -26,11 +33,11 @@ class NetworkService { } } - Future getRequest({required String url, bool isByteResponse = false, bool isStreamReponse = false}) async { + Future getRequest( + {required String url, + bool isByteResponse = false, + bool isStreamReponse = false}) async { try { - var dio = Dio(); - dio.interceptors.add(AuthenticatedRequestInterceptor()); - var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey); if (isByteResponse) { @@ -66,12 +73,9 @@ class NetworkService { Future postRequest({required String url, dynamic data}) async { try { - var dio = Dio(); - dio.interceptors.add(AuthenticatedRequestInterceptor()); - var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey); - String validUrl = Uri.parse('$savedEndpoint/$url').toString(); - Response res = await dio.post(validUrl, data: data); + var validUrl = Uri.parse('$savedEndpoint/$url').toString(); + var res = await dio.post(validUrl, data: data); return res; } on DioError catch (e) { @@ -85,12 +89,9 @@ class NetworkService { Future putRequest({required String url, dynamic data}) async { try { - var dio = Dio(); - dio.interceptors.add(AuthenticatedRequestInterceptor()); - var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey); - String validUrl = Uri.parse('$savedEndpoint/$url').toString(); - Response res = await dio.put(validUrl, data: data); + var validUrl = Uri.parse('$savedEndpoint/$url').toString(); + var res = await dio.put(validUrl, data: data); return res; } on DioError catch (e) { @@ -104,13 +105,9 @@ class NetworkService { Future patchRequest({required String url, dynamic data}) async { try { - var dio = Dio(); - dio.interceptors.add(AuthenticatedRequestInterceptor()); - var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey); - - String validUrl = Uri.parse('$savedEndpoint/$url').toString(); - Response res = await dio.patch(validUrl, data: data); + var validUrl = Uri.parse('$savedEndpoint/$url').toString(); + var res = await dio.patch(validUrl, data: data); return res; } on DioError catch (e) { @@ -122,21 +119,15 @@ class NetworkService { Future pingServer() async { try { - var dio = Dio(); - var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey); - - String validUrl = Uri.parse('$savedEndpoint/server-info/ping').toString(); + var validUrl = Uri.parse('$savedEndpoint/server-info/ping').toString(); debugPrint("ping server at url $validUrl"); - Response res = await dio.get(validUrl); + + var res = await dio.get(validUrl); var jsonRespsonse = jsonDecode(res.toString()); - if (jsonRespsonse["res"] == "pong") { - return true; - } else { - return false; - } + return jsonRespsonse["res"] == "pong"; } on DioError catch (e) { debugPrint("[PING SERVER] DioError: ${e.response} - $e"); return false; diff --git a/mobile/lib/shared/services/user.service.dart b/mobile/lib/shared/services/user.service.dart index 10c5ef4698..c36a5fa0d3 100644 --- a/mobile/lib/shared/services/user.service.dart +++ b/mobile/lib/shared/services/user.service.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:hive/hive.dart'; +import 'package:http_parser/http_parser.dart'; import 'package:image_picker/image_picker.dart'; import 'package:immich_mobile/constants/hive_box.dart'; import 'package:immich_mobile/shared/models/upload_profile_image_repsonse.model.dart'; @@ -10,14 +11,13 @@ import 'package:immich_mobile/shared/models/user.model.dart'; import 'package:immich_mobile/shared/services/network.service.dart'; import 'package:immich_mobile/utils/dio_http_interceptor.dart'; import 'package:immich_mobile/utils/files_helper.dart'; -import 'package:http_parser/http_parser.dart'; class UserService { final NetworkService _networkService = NetworkService(); Future> getAllUsersInfo() async { try { - Response res = await _networkService.getRequest(url: 'user'); + var res = await _networkService.getRequest(url: 'user'); List decodedData = jsonDecode(res.toString()); List result = List.from(decodedData.map((e) => User.fromMap(e))); diff --git a/mobile/lib/shared/ui/immich_toast.dart b/mobile/lib/shared/ui/immich_toast.dart index b28ef1619c..a4caca4d82 100644 --- a/mobile/lib/shared/ui/immich_toast.dart +++ b/mobile/lib/shared/ui/immich_toast.dart @@ -10,12 +10,10 @@ class ImmichToast { ToastType toastType = ToastType.info, ToastGravity gravity = ToastGravity.TOP, }) { - FToast fToast; - - fToast = FToast(); + final fToast = FToast(); fToast.init(context); - _getColor(ToastType type, BuildContext context) { + Color _getColor(ToastType type, BuildContext context) { switch (type) { case ToastType.info: return Theme.of(context).primaryColor; @@ -26,6 +24,26 @@ class ImmichToast { } } + Icon _getIcon(ToastType type) { + switch (type) { + case ToastType.info: + return Icon( + Icons.info_outline_rounded, + color: Theme.of(context).primaryColor, + ); + case ToastType.success: + return const Icon( + Icons.check_circle_rounded, + color: Color.fromARGB(255, 78, 140, 124), + ); + case ToastType.error: + return const Icon( + Icons.error_outline_rounded, + color: Color.fromARGB(255, 240, 162, 156), + ); + } + } + fToast.showToast( child: Container( padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0), @@ -40,24 +58,7 @@ class ImmichToast { child: Row( mainAxisSize: MainAxisSize.min, children: [ - (toastType == ToastType.info) - ? Icon( - Icons.info_outline_rounded, - color: Theme.of(context).primaryColor, - ) - : Container(), - (toastType == ToastType.success) - ? const Icon( - Icons.check_circle_rounded, - color: Color.fromARGB(255, 78, 140, 124), - ) - : Container(), - (toastType == ToastType.error) - ? const Icon( - Icons.error_outline_rounded, - color: Color.fromARGB(255, 240, 162, 156), - ) - : Container(), + _getIcon(toastType), const SizedBox( width: 12.0, ), diff --git a/mobile/lib/shared/views/immich_loading_overlay.dart b/mobile/lib/shared/views/immich_loading_overlay.dart index 70bfdb9224..6e4ef166bc 100644 --- a/mobile/lib/shared/views/immich_loading_overlay.dart +++ b/mobile/lib/shared/views/immich_loading_overlay.dart @@ -9,25 +9,25 @@ class ImmichLoadingOverlay extends StatelessWidget { @override Widget build(BuildContext context) { return ValueListenableBuilder( - valueListenable: ImmichLoadingOverlayController.appLoader.loaderShowingNotifier, + valueListenable: + ImmichLoadingOverlayController.appLoader.loaderShowingNotifier, builder: (context, shouldShow, child) { - if (shouldShow) { - return const Scaffold( - backgroundColor: Colors.black54, - body: Center( - child: ImmichLoadingIndicator(), - ), - ); - } else { - return Container(); - } + return shouldShow + ? const Scaffold( + backgroundColor: Colors.black54, + body: Center( + child: ImmichLoadingIndicator(), + ), + ) + : const SizedBox(); }, ); } } class ImmichLoadingOverlayController { - static final ImmichLoadingOverlayController appLoader = ImmichLoadingOverlayController(); + static final ImmichLoadingOverlayController appLoader = + ImmichLoadingOverlayController(); ValueNotifier loaderShowingNotifier = ValueNotifier(false); ValueNotifier loaderTextNotifier = ValueNotifier('error message'); diff --git a/mobile/lib/shared/views/version_announcement_overlay.dart b/mobile/lib/shared/views/version_announcement_overlay.dart index ff90a8a233..b7e0b672ff 100644 --- a/mobile/lib/shared/views/version_announcement_overlay.dart +++ b/mobile/lib/shared/views/version_announcement_overlay.dart @@ -85,7 +85,7 @@ class VersionAnnouncementOverlay extends HookConsumerWidget { const TextSpan( text: " and ensure your docker-compose and .env setup is up-to-date to prevent any misconfigurations, especially if you use WatchTower or any mechanism that handles updating your server application automatically.", - ) + ), ], ), ), @@ -93,23 +93,24 @@ class VersionAnnouncementOverlay extends HookConsumerWidget { Padding( padding: const EdgeInsets.only(top: 16.0), child: ElevatedButton( - style: ElevatedButton.styleFrom( - shape: const StadiumBorder(), - visualDensity: VisualDensity.standard, - primary: Colors.indigo, - onPrimary: Colors.grey[50], - elevation: 2, - padding: const EdgeInsets.symmetric( - vertical: 10, horizontal: 25), + style: ElevatedButton.styleFrom( + shape: const StadiumBorder(), + visualDensity: VisualDensity.standard, + primary: Colors.indigo, + onPrimary: Colors.grey[50], + elevation: 2, + padding: const EdgeInsets.symmetric( + vertical: 10, horizontal: 25), + ), + onPressed: onAcknowledgeTapped, + child: const Text( + "Acknowledge", + style: TextStyle( + fontSize: 14, ), - onPressed: onAcknowledgeTapped, - child: const Text( - "Acknowledge", - style: TextStyle( - fontSize: 14, - ), - )), - ) + ), + ), + ), ], ), ), diff --git a/mobile/pubspec.yaml b/mobile/pubspec.yaml index c4d3ad9c3a..e5483adc4c 100644 --- a/mobile/pubspec.yaml +++ b/mobile/pubspec.yaml @@ -5,7 +5,7 @@ publish_to: "none" version: 1.12.1+19 environment: - sdk: ">=2.15.1 <3.0.0" + sdk: ">=2.17.0 <3.0.0" dependencies: flutter: