Skip to content

Commit

Permalink
♻️ refactor backend
Browse files Browse the repository at this point in the history
  • Loading branch information
BirjuVachhani committed Aug 4, 2024
1 parent e0d4f8b commit 5bbe839
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 20 deletions.
2 changes: 1 addition & 1 deletion celest/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
};
}

Expand Down
11 changes: 11 additions & 0 deletions lib/backend/backend_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:shared/shared.dart';
import 'package:unsplash_client/unsplash_client.dart';

abstract class BackendService {
Future<void> init({bool local = false});

Future<Photo?> randomUnsplashImage({
required UnsplashSource source,
required UnsplashPhotoOrientation orientation,
});
}
34 changes: 34 additions & 0 deletions lib/backend/celest_backend_service.dart
Original file line number Diff line number Diff line change
@@ -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<void> 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<Photo?> randomUnsplashImage({
required UnsplashSource source,
required UnsplashPhotoOrientation orientation,
}) =>
celest.functions.unsplash
.randomUnsplashImage(source: source, orientation: orientation);
}
7 changes: 4 additions & 3 deletions lib/home/background_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -116,6 +116,8 @@ abstract class _BackgroundStore with Store, LazyInitializationMixin {
@readonly
ObservableList<UnsplashSource> _customSources = ObservableList.of([]);

final BackendService backendService = GetIt.instance.get<BackendService>();

@computed
bool get isLiked {
return currentImage != null &&
Expand Down Expand Up @@ -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),
Expand Down
31 changes: 15 additions & 16 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -46,29 +45,29 @@ class MyApp extends StatelessWidget {
}

Future<void> 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<LocalStorageManager>(storage);
GetIt.instance.registerSingleton<WeatherService>(OpenMeteoWeatherService());
GetIt.instance
.registerSingleton<GeocodingService>(OpenMeteoGeocodingService());

await GetIt.instance.allReady();
await loadPackageInfo();
}

Future<void> initializeBackend() async {
// Initialize Celest
final BackendService service = await getBackend();

await service.init(local: useLocalServer);

GetIt.instance.registerSingleton<BackendService>(service);
}

BackendService getBackend() => CelestBackendService();

ThemeData buildTheme(BuildContext context) {
return ThemeData(
colorScheme: ColorScheme.fromSeed(
Expand Down

0 comments on commit 5bbe839

Please sign in to comment.