owntracks-viewer/lib/main.dart

143 lines
3.8 KiB
Dart
Raw Normal View History

2024-03-12 20:41:04 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
2024-03-14 19:50:30 +00:00
import 'package:ot_viewer_app/global_location_store.dart';
import 'package:ot_viewer_app/refresh_cubit.dart';
2024-03-12 20:41:04 +00:00
import 'package:ot_viewer_app/settings_page.dart';
import 'package:path_provider/path_provider.dart';
import 'map_page.dart';
2024-03-14 19:50:30 +00:00
import 'package:get_it/get_it.dart';
2024-03-12 20:41:04 +00:00
import 'owntracks_api.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: kIsWeb
? HydratedStorage.webStorageDirectory
: await getApplicationDocumentsDirectory(),
);
2024-03-14 19:50:30 +00:00
GetIt.I
.registerSingleton<GlobalLocationStoreCubit>(GlobalLocationStoreCubit());
GetIt.I.registerSingleton<RefreshCubit>(RefreshCubit());
2024-03-12 20:41:04 +00:00
runApp(
MultiBlocProvider(
providers: [
BlocProvider(
lazy: false,
create: (BuildContext context) => SettingsCubit(),
),
BlocProvider(
create: (BuildContext context) => GetIt.I.get<RefreshCubit>(),
),
],
child: MyApp(),
),
);
2024-03-12 20:41:04 +00:00
}
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();
},
),
],
),
2024-03-14 19:50:30 +00:00
body: const MainPage(),
2024-03-12 20:41:04 +00:00
),
);
}
}
class MainPage extends StatefulWidget {
const MainPage({super.key});
@override
createState() => _MainPageState();
}
2024-11-10 11:11:40 +00:00
class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
2024-03-12 20:41:04 +00:00
int _currentIndex = 0;
final List<Widget> _pages = [
const MapPage(),
2024-11-10 11:11:40 +00:00
const SettingsPage(),
2024-03-12 20:41:04 +00:00
];
2024-11-10 11:11:40 +00:00
// 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();
}
2024-03-12 20:41:04 +00:00
void _onItemTapped(int index) {
setState(() {
_currentIndex = index;
});
}
2024-11-10 11:11:40 +00:00
// 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;
}
2024-03-12 20:41:04 +00:00
@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,
2024-03-12 20:41:04 +00:00
),
);
}
}