Skip to content

Commit

Permalink
Finish initial handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aguilaair committed Mar 11, 2022
1 parent 470e385 commit ca5c810
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 46 deletions.
26 changes: 20 additions & 6 deletions lib/providers/location_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class LocationProvider extends StateNotifier<LocationState> {
final Ref ref;

ReceivePort port = ReceivePort();
StreamController controller = StreamController();

Future<bool> checkPremission() async {
try {
Expand Down Expand Up @@ -137,20 +138,30 @@ class LocationProvider extends StateNotifier<LocationState> {
),
),
);
state = state.copyWith(isLocating: true);
}

Future<void> initializeTracking() async {
IsolateNameServer.registerPortWithName(port.sendPort, isolateName);
port.listen(handlLocationUpdates);
await BackgroundLocator.initialize();
final isTracking = await BackgroundLocator.isRegisterLocationUpdate();
state = state.copyWith(isLocating: isTracking);
final isRegistered = await BackgroundLocator.isRegisterLocationUpdate();
final isServiceRunning = await BackgroundLocator.isServiceRunning();

if (!(isRegistered && isServiceRunning)) {
IsolateNameServer.registerPortWithName(port.sendPort, isolateName);
try {
port.listen(handlLocationUpdates);
} catch (_) {
print("tried to relisten");
}
await BackgroundLocator.initialize();
}

state = state.copyWith(isLocating: isRegistered && isServiceRunning);
}

Future<void> stopAndDispose() async {
IsolateNameServer.removePortNameMapping(isolateName);
await BackgroundLocator.unRegisterLocationUpdate();
//state = state.copyWith(isLocating: false);
state = state.copyWith(isLocating: false);
}

void handlLocationUpdates(dynamic data) {
Expand Down Expand Up @@ -178,4 +189,7 @@ void initCallback(Map<String, dynamic>? data) {

void disposedCallback() {
print('Disposed');

const apiHereKey = "-ZxMvdMIBcgHopigc15uxqXOmYal-1fP7Q-dFT-5g98";
const appHereId = "wnsjv5V5W9zcwGSajCjN";
}
110 changes: 71 additions & 39 deletions lib/screens/loc_sharing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,59 @@ class _LocSharingPageState extends ConsumerState<LocSharingPage> {
final accountProv = ref.watch(accountProvider).account;
final authProv = ref.watch(authProvider.notifier);

if (locationState.isPermissionGranted && !locationState.isLocating) {
locationStateNotifier
.initializeTracking()
.then((value) => locationStateNotifier.beginTracking());
}

if (ref.read(accountProvider).account == null) {
WidgetsBinding.instance?.addPostFrameCallback((_) => setState(() {}));
}

return Scaffold(
body: SafeArea(
child: SizedBox(
width: double.infinity,
height: double.infinity,
child: Stack(
alignment: Alignment.center,
alignment: Alignment.topCenter,
children: [
Card(
margin: const EdgeInsets.all(15),
child: SizedBox(
height: 60,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Logo(width: 100),
Row(
children: [
CircleAvatar(
backgroundColor:
Theme.of(context).colorScheme.background,
child: Text(getInitials(accountProv?.name)),
),
const SizedBox(
width: 10,
),
Text(
accountProv?.name ?? "No Name",
style: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(fontSize: 20),
),
IconButton(
onPressed: () async {
await authProv.logout();
Beamer.of(context).beamToNamed('/login');
},
icon: const Icon(Icons.logout))
],
),
],
),
),
),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Card(
margin: const EdgeInsets.all(15),
child: SizedBox(
height: 60,
Expand All @@ -53,38 +88,35 @@ class _LocSharingPageState extends ConsumerState<LocSharingPage> {
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Logo(width: 100),
Row(
children: [
CircleAvatar(
backgroundColor:
Theme.of(context).colorScheme.background,
child: Text(getInitials(accountProv?.name)),
),
const SizedBox(
width: 10,
),
Text(
accountProv?.name ?? "No Name",
style: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(fontSize: 20),
),
IconButton(
onPressed: () async {
await authProv.logout();
Beamer.of(context).beamToNamed('/login');
},
icon: const Icon(Icons.logout))
],
Text(
locationState.isLocating
? "Tracking is Started"
: "Tracking is Stopped",
style: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(fontSize: 20),
),
OutlinedButton.icon(
onPressed: () async {
if (locationState.isLocating) {
locationStateNotifier.stopAndDispose();
} else {
await locationStateNotifier
.initializeTracking();
locationStateNotifier.beginTracking();
}
},
icon: Icon(locationState.isLocating
? Icons.stop_rounded
: Icons.play_arrow_rounded),
label: Text(
locationState.isLocating ? "Stop" : "Start")),
],
),
),
)),
const SizedBox(
height: 10,
),
),
),
],
),
Expand Down
6 changes: 5 additions & 1 deletion lib/screens/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ class LoginPage extends StatefulHookConsumerWidget {
}

class _LoginPageState extends ConsumerState<LoginPage> {
var isLoginButton = false;

@override
Widget build(BuildContext context) {
ref.listen<AuthState>(authProvider, (previous, next) {
if (previous!.token != null && pre) {
if (previous!.token != null && !isLoginButton) {
if (mounted) context.beamToNamed("/locSharing");
}
});
Expand Down Expand Up @@ -154,6 +156,8 @@ Please log in to continue''',
),
),
onPressed: () {
isLoginButton = true;

login(usernameController.text,
passwordController.text, ref, context);
},
Expand Down

0 comments on commit ca5c810

Please sign in to comment.