owntracks-viewer/lib/main.dart

143 lines
3.8 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:ot_viewer_app/global_location_store.dart';
import 'package:ot_viewer_app/refresh_cubit.dart';
import 'package:ot_viewer_app/settings_page.dart';
import 'package:path_provider/path_provider.dart';
import 'map_page.dart';
import 'package:get_it/get_it.dart';
import 'owntracks_api.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: kIsWeb
? HydratedStorage.webStorageDirectory
: await getApplicationDocumentsDirectory(),
);
GetIt.I
.registerSingleton<GlobalLocationStoreCubit>(GlobalLocationStoreCubit());
GetIt.I.registerSingleton<RefreshCubit>(RefreshCubit());
runApp(
MultiBlocProvider(
providers: [
BlocProvider(
lazy: false,
create: (BuildContext context) => SettingsCubit(),
),
BlocProvider(
create: (BuildContext context) => GetIt.I.get<RefreshCubit>(),
),
],
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'OwnTracks Data Viewer',
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const Text('OwnTrakcs Data Viewer'),
actions: [
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () async {
context.read<RefreshCubit>().triggerRefresh();
},
),
],
),
body: const MainPage(),
),
);
}
}
class MainPage extends StatefulWidget {
const MainPage({super.key});
@override
createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
int _currentIndex = 0;
final List<Widget> _pages = [
const MapPage(),
const SettingsPage(),
];
// Variable to keep track of the last lifecycle state
AppLifecycleState? _lastLifecycleState;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
void _onItemTapped(int index) {
setState(() {
_currentIndex = index;
});
}
// Listen for app lifecycle changes
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// Check if the app is transitioning from paused to resumed
print('DEBUG: lifecycle state changed from $_lastLifecycleState to $state');
if (_lastLifecycleState == AppLifecycleState.paused &&
(state == AppLifecycleState.resumed ||
state == AppLifecycleState.inactive ||
state == AppLifecycleState.detached ||
state == AppLifecycleState.hidden)) {
print('DEBUG: app is unpaused, triggering refresh');
// Trigger the refresh when the app is "unpaused"
context.read<RefreshCubit>().triggerRefresh();
}
// Update the last lifecycle state
_lastLifecycleState = state;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _currentIndex,
children: _pages,
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.map),
label: 'Map',
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
),
],
currentIndex: _currentIndex,
onTap: _onItemTapped,
),
);
}
}