mirror of
https://github.com/seemoo-lab/openhaystack.git
synced 2026-08-23 21:46:15 +00:00
Adding OpenHaystack Mobile app
Co-Authored-By: Lukas Burg <lukas.burg@hemalu.de>
This commit is contained in:
committed by
Alexander Heinrich
co-authored by
Lukas Burg
parent
b65a6e6be0
commit
3d593a006c
@@ -0,0 +1,58 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:openhaystack_mobile/location/location_model.dart';
|
||||
import 'package:openhaystack_mobile/preferences/user_preferences_model.dart';
|
||||
|
||||
class PreferencesPage extends StatefulWidget {
|
||||
|
||||
/// Displays this preferences page with information about the app.
|
||||
const PreferencesPage({ Key? key }) : super(key: key);
|
||||
|
||||
@override
|
||||
_PreferencesPageState createState() => _PreferencesPageState();
|
||||
}
|
||||
|
||||
class _PreferencesPageState extends State<PreferencesPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Settings'),
|
||||
),
|
||||
body: Consumer<UserPreferences>(
|
||||
builder: (BuildContext context, UserPreferences prefs, Widget? child) {
|
||||
return Center(
|
||||
child: Container(
|
||||
constraints: const BoxConstraints(maxWidth: 500),
|
||||
child: ListView(
|
||||
children: [
|
||||
SwitchListTile(
|
||||
title: const Text('Show this devices location'),
|
||||
value: !prefs.locationPreferenceKnown! || (prefs.locationAccessWanted ?? true),
|
||||
onChanged: (showLocation) {
|
||||
prefs.setLocationPreference(showLocation);
|
||||
var locationModel = Provider.of<LocationModel>(context, listen: false);
|
||||
if (showLocation) {
|
||||
locationModel.requestLocationUpdates();
|
||||
} else {
|
||||
locationModel.cancelLocationUpdates();
|
||||
}
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: TextButton(
|
||||
child: const Text('About'),
|
||||
onPressed: () => showAboutDialog(
|
||||
context: context,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
const introductionShownKey = 'INTRODUCTION_SHOWN';
|
||||
const locationPreferenceKnownKey = 'LOCATION_PREFERENCE_KNOWN';
|
||||
const locationAccessWantedKey = 'LOCATION_PREFERENCE_WANTED';
|
||||
|
||||
class UserPreferences extends ChangeNotifier {
|
||||
|
||||
/// If these settings are initialized.
|
||||
bool initialized = false;
|
||||
/// The shared preferences storage.
|
||||
SharedPreferences? _prefs;
|
||||
|
||||
/// Manages information about the users preferences.
|
||||
UserPreferences() {
|
||||
_initializeAsync();
|
||||
}
|
||||
|
||||
/// Initialize shared preferences access
|
||||
void _initializeAsync() async {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
|
||||
// For Debugging:
|
||||
// await prefs.clear();
|
||||
|
||||
initialized = true;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Returns if the introduction should be shown.
|
||||
bool? shouldShowIntroduction() {
|
||||
if (_prefs == null) {
|
||||
return null;
|
||||
} else {
|
||||
if (!_prefs!.containsKey(introductionShownKey)) {
|
||||
return true; // Initial start of the app
|
||||
}
|
||||
return _prefs?.getBool(introductionShownKey);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns if the user's locaiton preference is known.
|
||||
bool? get locationPreferenceKnown {
|
||||
return _prefs?.getBool(locationPreferenceKnownKey) ?? false;
|
||||
}
|
||||
|
||||
/// Returns if the user desires location access.
|
||||
bool? get locationAccessWanted {
|
||||
return _prefs?.getBool(locationAccessWantedKey);
|
||||
}
|
||||
|
||||
/// Updates the location access preference of the user.
|
||||
Future<bool> setLocationPreference(bool locationAccessWanted) async {
|
||||
_prefs ??= await SharedPreferences.getInstance();
|
||||
var success = await _prefs!.setBool(locationPreferenceKnownKey, true);
|
||||
if (!success) {
|
||||
return Future.value(false);
|
||||
} else {
|
||||
var result = await _prefs!.setBool(locationAccessWantedKey, locationAccessWanted);
|
||||
notifyListeners();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user