From 5bbe839277182c0976b7c8c2cfabc3d9104efe07 Mon Sep 17 00:00:00 2001 From: birjuvachhani Date: Sun, 4 Aug 2024 14:44:34 +0530 Subject: [PATCH] :recycle: refactor backend --- celest/lib/client.dart | 2 +- lib/backend/backend_service.dart | 11 ++++++++ lib/backend/celest_backend_service.dart | 34 +++++++++++++++++++++++++ lib/home/background_store.dart | 7 ++--- lib/main.dart | 31 +++++++++++----------- 5 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 lib/backend/backend_service.dart create mode 100644 lib/backend/celest_backend_service.dart diff --git a/celest/lib/client.dart b/celest/lib/client.dart index da02a67..4d3c834 100644 --- a/celest/lib/client.dart +++ b/celest/lib/client.dart @@ -23,7 +23,7 @@ enum CelestEnvironment { local => kIsWeb || !_$io.Platform.isAndroid ? Uri.parse('http://localhost:7777') : Uri.parse('http://10.0.2.2:7777'), - production => Uri.parse('https://pluto-7e6b20-v76lntiq7q-el.a.run.app'), + production => Uri.parse('https://pluto-fd3a2f-d4nrgles6q-el.a.run.app'), }; } diff --git a/lib/backend/backend_service.dart b/lib/backend/backend_service.dart new file mode 100644 index 0000000..646c423 --- /dev/null +++ b/lib/backend/backend_service.dart @@ -0,0 +1,11 @@ +import 'package:shared/shared.dart'; +import 'package:unsplash_client/unsplash_client.dart'; + +abstract class BackendService { + Future init({bool local = false}); + + Future randomUnsplashImage({ + required UnsplashSource source, + required UnsplashPhotoOrientation orientation, + }); +} diff --git a/lib/backend/celest_backend_service.dart b/lib/backend/celest_backend_service.dart new file mode 100644 index 0000000..af0d4ca --- /dev/null +++ b/lib/backend/celest_backend_service.dart @@ -0,0 +1,34 @@ +import 'dart:developer'; + +import 'package:celest_backend/client.dart'; +import 'package:shared/shared.dart'; +import 'package:unsplash_client/unsplash_client.dart'; + +import 'backend_service.dart'; + +class CelestBackendService extends BackendService { + @override + Future init({bool local = false}) async { + // Initialize Celest at the start of your app + bool celestInitialized = false; + + if (local) { + log('Initializing Celest with local server'); + celest.init(environment: CelestEnvironment.local); + celestInitialized = true; + } + + if (!celestInitialized) { + log('Initializing Celest with production server'); + celest.init(environment: CelestEnvironment.production); + } + } + + @override + Future randomUnsplashImage({ + required UnsplashSource source, + required UnsplashPhotoOrientation orientation, + }) => + celest.functions.unsplash + .randomUnsplashImage(source: source, orientation: orientation); +} diff --git a/lib/home/background_store.dart b/lib/home/background_store.dart index 8fe0a26..7b1c93c 100644 --- a/lib/home/background_store.dart +++ b/lib/home/background_store.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'dart:developer'; import 'dart:math' hide log; -import 'package:celest_backend/client.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -15,6 +14,7 @@ import 'package:shared/shared.dart'; import 'package:unsplash_client/unsplash_client.dart'; import 'package:url_launcher/url_launcher.dart'; +import '../backend/backend_service.dart'; import '../model/background_settings.dart'; import '../model/color_gradient.dart'; import '../model/flat_color.dart'; @@ -116,6 +116,8 @@ abstract class _BackgroundStore with Store, LazyInitializationMixin { @readonly ObservableList _customSources = ObservableList.of([]); + final BackendService backendService = GetIt.instance.get(); + @computed bool get isLiked { return currentImage != null && @@ -634,8 +636,7 @@ abstract class _BackgroundStore with Store, LazyInitializationMixin { final Size size = _imageResolution.toSize() ?? windowSize; switch (_imageSource) { case ImageSource.unsplash: - final Photo? photo = - await celest.functions.unsplash.randomUnsplashImage( + final Photo? photo = await backendService.randomUnsplashImage( source: _unsplashSource, orientation: UnsplashPhotoOrientation.fromAspectRatio(size.aspectRatio), diff --git a/lib/main.dart b/lib/main.dart index 60367ed..d298391 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,11 +1,10 @@ -import 'dart:developer'; - -import 'package:celest_backend/client.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; import 'package:package_info_plus/package_info_plus.dart'; +import 'backend/backend_service.dart'; +import 'backend/celest_backend_service.dart'; import 'home/home.dart'; import 'resources/colors.dart'; import 'utils/geocoding_service.dart'; @@ -46,19 +45,7 @@ class MyApp extends StatelessWidget { } Future initialize() async { - // Initialize Celest at the start of your app - bool celestInitialized = false; - - if (useLocalServer) { - log('Initializing Celest with local server'); - celest.init(environment: CelestEnvironment.local); - celestInitialized = true; - } - - if (!celestInitialized) { - log('Initializing Celest with production server'); - celest.init(environment: CelestEnvironment.production); - } + await initializeBackend(); final storage = await SharedPreferencesStorageManager.create(); GetIt.instance.registerSingleton(storage); @@ -66,9 +53,21 @@ Future initialize() async { GetIt.instance .registerSingleton(OpenMeteoGeocodingService()); + await GetIt.instance.allReady(); await loadPackageInfo(); } +Future initializeBackend() async { + // Initialize Celest + final BackendService service = await getBackend(); + + await service.init(local: useLocalServer); + + GetIt.instance.registerSingleton(service); +} + +BackendService getBackend() => CelestBackendService(); + ThemeData buildTheme(BuildContext context) { return ThemeData( colorScheme: ColorScheme.fromSeed(