Skip to content

Commit

Permalink
add future location form #11
Browse files Browse the repository at this point in the history
  • Loading branch information
LGro committed May 24, 2024
1 parent 45fc181 commit 560ff8f
Show file tree
Hide file tree
Showing 8 changed files with 590 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/ui/locations/check_in/widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class _MyFormState extends State<MyForm> {
_state.title.isNotEmpty)
? _onSubmit
: null,
child: const Text('Submit'),
child: const Text('Share'),
),
const SizedBox(height: 16),
])));
Expand Down
7 changes: 5 additions & 2 deletions lib/ui/locations/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import '../../data/repositories/contacts.dart';
import '../widgets/circles/widget.dart';
import 'check_in/widget.dart';
import 'cubit.dart';
import 'schedule/widget.dart';

class LocationForm extends StatefulWidget {
LocationForm({super.key, Random? seed}) : seed = seed ?? Random();
Expand Down Expand Up @@ -372,11 +373,13 @@ class LocationsPage extends StatelessWidget {
'Importing new locations from the calendar will come soon'),
)),
icon: const Icon(Icons.calendar_month)),
// TODO: Add manually via form
IconButton(
onPressed: (state.circleMembersips.isEmpty)
? null
: context.read<LocationsCubit>().addRandomLocation,
: () async => Navigator.push(
context,
MaterialPageRoute<ScheduleWidget>(
builder: (_) => const ScheduleWidget())),
icon: const Icon(Icons.add)),
],
),
Expand Down
67 changes: 67 additions & 0 deletions lib/ui/locations/schedule/cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 The Coagulate Authors. All rights reserved.
// SPDX-License-Identifier: MPL-2.0

import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:latlong2/latlong.dart';
import 'package:location/location.dart';

import '../../../data/models/contact_location.dart';
import '../../../data/repositories/contacts.dart';

part 'cubit.g.dart';
part 'state.dart';

class ScheduleCubit extends Cubit<ScheduleState> {
ScheduleCubit(this.contactsRepository)
: super(ScheduleState(
checkingIn: false, circles: contactsRepository.getCircles()));

final ContactsRepository contactsRepository;

Future<void> schedule({
required String name,
required String details,
required DateTime start,
required DateTime end,
required LatLng coordinates,
required List<String> circles,
}) async {
emit(state.copyWith(checkingIn: true));

final profileContact = contactsRepository.getProfileContact();
if (profileContact == null) {
if (!isClosed) {
//TODO: Emit failure state
emit(state.copyWith(checkingIn: false));
}
return;
}

// TODO: Switch to unawaited because it'll be synced eventually?
await contactsRepository
.updateContact(profileContact.copyWith(temporaryLocations: [
...profileContact.temporaryLocations
.map((l) => l.copyWith(checkedIn: false)),
ContactTemporaryLocation(
coagContactId: contactsRepository.profileContactId!,
longitude: coordinates.longitude,
latitude: coordinates.latitude,
start: start,
name: name,
details: details,
end: end,
circles: circles)
]))
// Make sure to regenerate the sharing profiles and update DHT sharing records
.then((_) => contactsRepository
.updateProfileContact(profileContact.coagContactId));

if (!isClosed) {
emit(state.copyWith(checkingIn: false));
}
}
}
19 changes: 19 additions & 0 deletions lib/ui/locations/schedule/cubit.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions lib/ui/locations/schedule/state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 The Coagulate Authors. All rights reserved.
// SPDX-License-Identifier: MPL-2.0

part of 'cubit.dart';

@JsonSerializable()
final class ScheduleState extends Equatable {
const ScheduleState({required this.checkingIn, required this.circles});

factory ScheduleState.fromJson(Map<String, dynamic> json) =>
_$ScheduleStateFromJson(json);

final bool checkingIn;
final Map<String, String> circles;

Map<String, dynamic> toJson() => _$ScheduleStateToJson(this);

ScheduleState copyWith({bool? checkingIn, Map<String, String>? circles}) =>
ScheduleState(
checkingIn: checkingIn ?? this.checkingIn,
circles: circles ?? this.circles);

@override
List<Object?> get props => [checkingIn, circles];
}
Loading

0 comments on commit 560ff8f

Please sign in to comment.