Skip to content

Commit

Permalink
move api call to main page
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jun 28, 2024
1 parent 1468f61 commit 0d6c2a5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
4 changes: 3 additions & 1 deletion lib/domain/services/boot_strap_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class BootStrapService {
: _sipService = sipService ?? SipService(),
_investmentService = investmentService ?? InvestmentService();

Future<void> performBootStrapOperations() => Future.wait([
Future<void> performBootStrapOperations() {
return Future.wait([
_sipService.performSipTransactions(),
_investmentService.updateValues(),
]);
}
}
2 changes: 0 additions & 2 deletions lib/domain/services/script_executor_service.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:logger/logger.dart';
import 'package:wealth_wave/domain/services/dsl_parser.dart';

class ScriptExecutorService {
Expand Down Expand Up @@ -36,7 +35,6 @@ class ScriptExecutorService {
_getValueFromJsonPath(response.body, parsedDefn.responsePath);
return value;
} else {
Logger().e('Failed to execute script: ${response.body}');
return null;
}
}
Expand Down
33 changes: 15 additions & 18 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wealth_wave/api/db/app_database.dart';
import 'package:wealth_wave/app_router.dart';
import 'package:wealth_wave/domain/services/boot_strap_service.dart';

void main() {
runApp(Provider<AppDatabase>(
Expand All @@ -17,22 +16,20 @@ class WealthWaveApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: BootStrapService().performBootStrapOperations(),
builder: (context, snapshot) => MaterialApp(
title: 'Wealth Wave',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
onGenerateRoute: (settings) {
return MaterialPageRoute(
settings: RouteSettings(
name: settings.name, arguments: settings.arguments),
maintainState: true,
builder: (context) => AppRouter.route(settings.name!));
},
));
return MaterialApp(
title: 'Wealth Wave',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
onGenerateRoute: (settings) {
return MaterialPageRoute(
settings: RouteSettings(
name: settings.name, arguments: settings.arguments),
maintainState: true,
builder: (context) => AppRouter.route(settings.name!));
},
);
}
}
19 changes: 18 additions & 1 deletion lib/presentation/main_presenter.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import 'package:wealth_wave/core/presenter.dart';
import 'package:wealth_wave/core/single_event.dart';
import 'package:wealth_wave/domain/services/backup_service.dart';
import 'package:wealth_wave/domain/services/boot_strap_service.dart';

class MainPresenter extends Presenter<MainViewState> {
final BackupService _backupService;
final BootStrapService _bootStrapService;

MainPresenter({final BackupService? backupService})
MainPresenter(
{final BackupService? backupService,
final BootStrapService? bootStrapService})
: _backupService = backupService ?? BackupService(),
_bootStrapService = bootStrapService ?? BootStrapService(),
super(MainViewState());

void performImportFile() {
Expand All @@ -20,9 +25,21 @@ class MainPresenter extends Presenter<MainViewState> {
viewState.onBackupCompleted = SingleEvent(null);
}));
}

void performSync() {
_bootStrapService
.performBootStrapOperations()
.then((value) => updateViewState((viewState) {
viewState.contentLoading = false;
}))
.onError((error, stackTrace) => updateViewState((viewState) {
viewState.contentLoading = false;
}));
}
}

class MainViewState {
SingleEvent<void>? onBackupCompleted;
SingleEvent<void>? onImportCompleted;
bool contentLoading = true;
}
17 changes: 17 additions & 0 deletions lib/ui/pages/main_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class MainPage extends StatefulWidget {
class _MainPageState extends PageState<MainViewState, MainPage, MainPresenter> {
var _selectedIndex = 0;

@override
void initState() {
super.initState();
presenter.performSync();
}

@override
Widget buildWidget(BuildContext context, MainViewState snapshot) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Expand All @@ -27,6 +33,17 @@ class _MainPageState extends PageState<MainViewState, MainPage, MainPresenter> {
});
});

if(snapshot.contentLoading) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text("Wealth Wave"),
),
body: const Center(
child: CircularProgressIndicator(),
));
}

return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
Expand Down

0 comments on commit 0d6c2a5

Please sign in to comment.