Skip to content

Commit

Permalink
Update location of chat connection repository
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Aug 12, 2023
1 parent 31bdbe6 commit af4b63b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:spinifyapp/src/feature/authentication/model/user.dart';
abstract interface class IAuthenticationRepository {
Stream<User> userChanges();
FutureOr<User> getUser();
FutureOr<String> getToken();
Future<void> signIn(SignInData data);
Future<void> signOut();
}
Expand All @@ -20,6 +21,16 @@ class AuthenticationRepositoryImpl implements IAuthenticationRepository {
@override
FutureOr<User> getUser() => _user;

@override
Future<String> getToken() async {
switch (_user) {
case AuthenticatedUser user:
return user.token;
case UnauthenticatedUser _:
throw Exception('User is not authenticated');
}
}

@override
Stream<User> userChanges() => _userController.stream;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ final class ChatConnectionController
void connect(String url) => handle(() => _repository.connect(url));

void disconnect() => handle(_repository.disconnect);

@override
void dispose() {
_repository.disconnect();
super.dispose();
}
}
15 changes: 1 addition & 14 deletions example/lib/src/feature/chat/data/chat_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@ abstract interface class IChatRepository {

/// Disconnect from chat server
Future<void> disconnect();

/// Dispose
Future<void> dispose();
}

final class ChatRepositorySpinifyImpl implements IChatRepository {
ChatRepositorySpinifyImpl({required FutureOr<String?> Function()? getToken})
: _spinify = Spinify(
SpinifyConfig(
getToken: getToken,
),
);
ChatRepositorySpinifyImpl({required Spinify spinify}) : _spinify = spinify;

/// Centrifugo client
final Spinify _spinify;
Expand Down Expand Up @@ -58,9 +50,4 @@ final class ChatRepositorySpinifyImpl implements IChatRepository {

@override
Future<void> disconnect() => _spinify.disconnect();

@override
Future<void> dispose() async {
await _spinify.close();
}
}
9 changes: 3 additions & 6 deletions example/lib/src/feature/chat/widget/chat_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:spinifyapp/src/common/controller/state_consumer.dart';
import 'package:spinifyapp/src/feature/authentication/model/user.dart';
import 'package:spinifyapp/src/feature/chat/controller/chat_connection_controller.dart';
import 'package:spinifyapp/src/feature/chat/controller/chat_connection_state.dart';
import 'package:spinifyapp/src/feature/chat/data/chat_repository.dart';
import 'package:spinifyapp/src/feature/dependencies/widget/dependencies_scope.dart';

/// {@template chat_screen}
/// ChatRoom widget.
Expand All @@ -21,15 +21,13 @@ class ChatRoom extends StatefulWidget {

/// State for widget ChatRoom.
class _ChatRoomState extends State<ChatRoom> {
late final IChatRepository _repository;
late final ChatConnectionController _chatConnectionController;

@override
void initState() {
super.initState();
_repository = ChatRepositorySpinifyImpl(getToken: () => widget.user.token);
_chatConnectionController =
ChatConnectionController(repository: _repository);
_chatConnectionController = ChatConnectionController(
repository: DependenciesScope.of(context).chatRepository);
_chatConnectionController.connect(widget.user.endpoint);
}

Expand All @@ -45,7 +43,6 @@ class _ChatRoomState extends State<ChatRoom> {
@override
void dispose() {
_chatConnectionController.dispose();
_repository.dispose();
super.dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import 'dart:async';
import 'package:l/l.dart';
import 'package:meta/meta.dart';
import 'package:platform_info/platform_info.dart';
import 'package:spinify/spinify.dart';
import 'package:spinifyapp/src/common/constant/config.dart';
import 'package:spinifyapp/src/common/constant/pubspec.yaml.g.dart';
import 'package:spinifyapp/src/common/controller/controller.dart';
import 'package:spinifyapp/src/common/controller/controller_observer.dart';
import 'package:spinifyapp/src/common/util/screen_util.dart';
import 'package:spinifyapp/src/feature/authentication/data/authentication_repository.dart';
import 'package:spinifyapp/src/feature/chat/data/chat_repository.dart';
import 'package:spinifyapp/src/feature/dependencies/initialization/platform/initialization_vm.dart'
// ignore: uri_does_not_exist
if (dart.library.html) 'package:spinifyapp/src/feature/dependencies/initialization/platform/initialization_js.dart';
Expand All @@ -24,6 +26,9 @@ class _MutableDependencies implements Dependencies {

@override
late IAuthenticationRepository authenticationRepository;

@override
late IChatRepository chatRepository;
}

@internal
Expand Down Expand Up @@ -99,16 +104,15 @@ mixin InitializeDependencies {
AuthenticationRepositoryImpl(),
),
(
'Fake delay 1',
(_) => Future<void>.delayed(const Duration(seconds: 1)),
),
(
'Fake delay 2',
(_) => Future<void>.delayed(const Duration(seconds: 1)),
),
(
'Fake delay 3',
(_) => Future<void>.delayed(const Duration(seconds: 1)),
'Chat repository',
(dependencies) =>
dependencies.chatRepository = ChatRepositorySpinifyImpl(
spinify: Spinify(
SpinifyConfig(
getToken: dependencies.authenticationRepository.getToken,
),
),
),
),
];
}
4 changes: 4 additions & 0 deletions example/lib/src/feature/dependencies/model/dependencies.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:spinifyapp/src/feature/authentication/data/authentication_repository.dart';
import 'package:spinifyapp/src/feature/chat/data/chat_repository.dart';
import 'package:spinifyapp/src/feature/dependencies/model/app_metadata.dart';

abstract interface class Dependencies {
Expand All @@ -7,4 +8,7 @@ abstract interface class Dependencies {

/// Authentication repository
abstract final IAuthenticationRepository authenticationRepository;

/// Chat repository
abstract final IChatRepository chatRepository;
}

0 comments on commit af4b63b

Please sign in to comment.