Skip to content

Commit

Permalink
Share decks with a link (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsreichardt authored Dec 31, 2023
1 parent 8b0c722 commit 9387dd4
Show file tree
Hide file tree
Showing 24 changed files with 1,138 additions and 80 deletions.
26 changes: 26 additions & 0 deletions lib/src/infrastructure/session_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,30 @@ class SessionRepository {
.snapshots()
.map((event) => event.data());
}

Future<SessionDto?> getSession(SessionId sessionId) async {
final doc = await firestore
.collection('Sessions')
.doc(sessionId)
.withConverter(
fromFirestore: (doc, options) =>
SessionDto.fromJsonWithInjectedId(sessionId, doc.data()!),
toFirestore: (_, __) => throw UnimplementedError(),
)
.get();
return doc.data();
}

Future<void> setVisibility(
SessionId sessionId,
Visibility visibility,
) async {
await functions.httpsCallableFromUrl(routeFunctionsUrl).call({
'destination': 'setVisibility',
'payload': {
'sessionId': sessionId,
'visibility': visibility.name,
}
});
}
}
42 changes: 42 additions & 0 deletions lib/src/models/session_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:ankigpt/src/models/csv_metadata.dart';
import 'package:ankigpt/src/models/input_type.dart';
import 'package:ankigpt/src/models/language.dart';
import 'package:ankigpt/src/models/session_id.dart';
import 'package:ankigpt/src/models/user_id.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

Expand All @@ -24,6 +25,8 @@ class SessionDto with _$SessionDto {
required SessionStatus status,
@JsonKey(fromJson: parseError) String? error,
required int numberOfCards,
@JsonKey(fromJson: parseVisibility) required Visibility visibility,
required UserId userId,
}) = _SessionDto;

factory SessionDto.fromJson(Map<String, dynamic> json) =>
Expand Down Expand Up @@ -53,6 +56,40 @@ Map<String, AnkiCard>? parseCards(Map<String, dynamic>? json) {
);
}

Visibility parseVisibility(Map<String, dynamic>? json) {
const defaultValue = Visibility.private;

if (json == null) {
return defaultValue;
}

final value = json['value'];
if (value is! String?) {
return defaultValue;
}

return Visibility.values.tryByName(json['value']);
}

extension EnumByNameWithDefault<T extends Enum> on Iterable<T> {
T tryByName(String? name, {T? defaultValue}) {
for (T value in this) {
if (value.name == name) return value;
}

if (defaultValue != null) return defaultValue;
throw ArgumentError.value(name, "name", "No enum value with that name");
}

T? byNameOrNull(String? name) {
for (T value in this) {
if (value.name == name) return value;
}

return null;
}
}

String? parseError(dynamic data) {
if (data == null) null;

Expand All @@ -70,6 +107,11 @@ enum SessionStatus {
stopped,
}

enum Visibility {
private,
anyoneWithLink,
}

@Freezed(fromJson: true, toStringOverride: false)
class Input with _$Input {
const Input._();
Expand Down
60 changes: 53 additions & 7 deletions lib/src/models/session_dto.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ mixin _$SessionDto {
@JsonKey(fromJson: parseError)
String? get error => throw _privateConstructorUsedError;
int get numberOfCards => throw _privateConstructorUsedError;
@JsonKey(fromJson: parseVisibility)
Visibility get visibility => throw _privateConstructorUsedError;
String get userId => throw _privateConstructorUsedError;

Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
Expand All @@ -54,7 +57,9 @@ abstract class $SessionDtoCopyWith<$Res> {
@JsonKey(fromJson: parseCards) Map<String, AnkiCard>? cards,
SessionStatus status,
@JsonKey(fromJson: parseError) String? error,
int numberOfCards});
int numberOfCards,
@JsonKey(fromJson: parseVisibility) Visibility visibility,
String userId});

$InputCopyWith<$Res> get input;
$CsvMetadataCopyWith<$Res>? get csv;
Expand Down Expand Up @@ -82,6 +87,8 @@ class _$SessionDtoCopyWithImpl<$Res, $Val extends SessionDto>
Object? status = null,
Object? error = freezed,
Object? numberOfCards = null,
Object? visibility = null,
Object? userId = null,
}) {
return _then(_value.copyWith(
id: null == id
Expand Down Expand Up @@ -120,6 +127,14 @@ class _$SessionDtoCopyWithImpl<$Res, $Val extends SessionDto>
? _value.numberOfCards
: numberOfCards // ignore: cast_nullable_to_non_nullable
as int,
visibility: null == visibility
? _value.visibility
: visibility // ignore: cast_nullable_to_non_nullable
as Visibility,
userId: null == userId
? _value.userId
: userId // ignore: cast_nullable_to_non_nullable
as String,
) as $Val);
}

Expand Down Expand Up @@ -161,7 +176,9 @@ abstract class _$$SessionDtoImplCopyWith<$Res>
@JsonKey(fromJson: parseCards) Map<String, AnkiCard>? cards,
SessionStatus status,
@JsonKey(fromJson: parseError) String? error,
int numberOfCards});
int numberOfCards,
@JsonKey(fromJson: parseVisibility) Visibility visibility,
String userId});

@override
$InputCopyWith<$Res> get input;
Expand Down Expand Up @@ -189,6 +206,8 @@ class __$$SessionDtoImplCopyWithImpl<$Res>
Object? status = null,
Object? error = freezed,
Object? numberOfCards = null,
Object? visibility = null,
Object? userId = null,
}) {
return _then(_$SessionDtoImpl(
id: null == id
Expand Down Expand Up @@ -227,6 +246,14 @@ class __$$SessionDtoImplCopyWithImpl<$Res>
? _value.numberOfCards
: numberOfCards // ignore: cast_nullable_to_non_nullable
as int,
visibility: null == visibility
? _value.visibility
: visibility // ignore: cast_nullable_to_non_nullable
as Visibility,
userId: null == userId
? _value.userId
: userId // ignore: cast_nullable_to_non_nullable
as String,
));
}
}
Expand All @@ -244,7 +271,9 @@ class _$SessionDtoImpl implements _SessionDto {
required final Map<String, AnkiCard>? cards,
required this.status,
@JsonKey(fromJson: parseError) this.error,
required this.numberOfCards})
required this.numberOfCards,
@JsonKey(fromJson: parseVisibility) required this.visibility,
required this.userId})
: _cards = cards;

factory _$SessionDtoImpl.fromJson(Map<String, dynamic> json) =>
Expand Down Expand Up @@ -279,10 +308,15 @@ class _$SessionDtoImpl implements _SessionDto {
final String? error;
@override
final int numberOfCards;
@override
@JsonKey(fromJson: parseVisibility)
final Visibility visibility;
@override
final String userId;

@override
String toString() {
return 'SessionDto(id: $id, language: $language, input: $input, createdAt: $createdAt, csv: $csv, cards: $cards, status: $status, error: $error, numberOfCards: $numberOfCards)';
return 'SessionDto(id: $id, language: $language, input: $input, createdAt: $createdAt, csv: $csv, cards: $cards, status: $status, error: $error, numberOfCards: $numberOfCards, visibility: $visibility, userId: $userId)';
}

@override
Expand All @@ -301,7 +335,10 @@ class _$SessionDtoImpl implements _SessionDto {
(identical(other.status, status) || other.status == status) &&
(identical(other.error, error) || other.error == error) &&
(identical(other.numberOfCards, numberOfCards) ||
other.numberOfCards == numberOfCards));
other.numberOfCards == numberOfCards) &&
(identical(other.visibility, visibility) ||
other.visibility == visibility) &&
(identical(other.userId, userId) || other.userId == userId));
}

@JsonKey(ignore: true)
Expand All @@ -316,7 +353,9 @@ class _$SessionDtoImpl implements _SessionDto {
const DeepCollectionEquality().hash(_cards),
status,
error,
numberOfCards);
numberOfCards,
visibility,
userId);

@JsonKey(ignore: true)
@override
Expand All @@ -343,7 +382,9 @@ abstract class _SessionDto implements SessionDto {
required final Map<String, AnkiCard>? cards,
required final SessionStatus status,
@JsonKey(fromJson: parseError) final String? error,
required final int numberOfCards}) = _$SessionDtoImpl;
required final int numberOfCards,
@JsonKey(fromJson: parseVisibility) required final Visibility visibility,
required final String userId}) = _$SessionDtoImpl;

factory _SessionDto.fromJson(Map<String, dynamic> json) =
_$SessionDtoImpl.fromJson;
Expand All @@ -370,6 +411,11 @@ abstract class _SessionDto implements SessionDto {
@override
int get numberOfCards;
@override
@JsonKey(fromJson: parseVisibility)
Visibility get visibility;
@override
String get userId;
@override
@JsonKey(ignore: true)
_$$SessionDtoImplCopyWith<_$SessionDtoImpl> get copyWith =>
throw _privateConstructorUsedError;
Expand Down
9 changes: 9 additions & 0 deletions lib/src/models/session_dto.g.dart

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

2 changes: 2 additions & 0 deletions lib/src/models/watch_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class WatchView with _$WatchView {
String? fileName,
SessionId? sessionId,
String? inputText,
// Defines if the user is the owner of the session.
bool? isOwner,
}) = _WatchView;

bool get hasFile => fileName != null;
Expand Down
Loading

0 comments on commit 9387dd4

Please sign in to comment.