Skip to content

Commit

Permalink
Fix collaboration dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Oct 27, 2024
1 parent 414fd6c commit 6c5b7bc
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 49 deletions.
9 changes: 4 additions & 5 deletions app/lib/dialogs/collaboration/start.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 84,19 @@ class _StartCollaborationDialogState extends State<StartCollaborationDialog>
],
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(AppLocalizations.of(context).close),
),
ElevatedButton(
onPressed: kIsWeb
? null
: () {
if (!(_formKey.currentState?.validate() ?? false)) return;
Navigator.of(context).pop();
_start();
},
child: Text(AppLocalizations.of(context).start),
),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(AppLocalizations.of(context).close),
),
],
),
),
Expand Down
1 change: 0 additions & 1 deletion app/lib/dialogs/collaboration/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 46,6 @@ class ViewCollaborationDialog extends StatelessWidget {
OutlinedButton(
child: Text(AppLocalizations.of(context).stop),
onPressed: () {
Navigator.of(context).pop();
service.closeNetworking();
},
),
Expand Down
73 changes: 36 additions & 37 deletions app/lib/services/network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 35,7 @@ enum NetworkingType {

const kDefaultPort = 28005;
const kTimeout = Duration(seconds: 10);
typedef NetworkingState = (NetworkerBase, RpcNetworkerPipe);
typedef NetworkingState = (NetworkerBase, NamedRpcNetworkerPipe<NetworkEvent>);

@freezed
class NetworkingInitMessage with _$NetworkingInitMessage {
Expand All @@ -56,11 56,19 @@ class NetworkingUser with _$NetworkingUser {
_$NetworkingUserFromJson(json);
}

enum NetworkEvent {
event,
init,
connections,
user,
enum NetworkEvent with RpcFunctionName {
event(mode: RpcNetworkerMode.any, canRunLocally: false),
init(mode: RpcNetworkerMode.authority, canRunLocally: false),
connections(mode: RpcNetworkerMode.authority, canRunLocally: false),
user(mode: RpcNetworkerMode.any, canRunLocally: false);

@override
final RpcNetworkerMode mode;
@override
final bool canRunLocally;

const NetworkEvent(
{this.mode = RpcNetworkerMode.authority, this.canRunLocally = false});
}

class NetworkingService {
Expand Down Expand Up @@ -95,7 103,7 @@ class NetworkingService {
? InternetAddress(address, type: InternetAddressType.any)
: InternetAddress.anyIPv4,
port ?? kDefaultPort);
final rpc = RpcServerNetworkerPipe();
final rpc = NamedRpcServerNetworkerPipe<NetworkEvent>();
_setupRpc(rpc, server);
void sendConnections() {
rpc.callFunction(NetworkEvent.connections.index,
Expand Down Expand Up @@ -123,7 131,7 @@ class NetworkingService {
uri = uri.replace(port: kDefaultPort);
}
final client = NetworkerSocketClient(uri);
final rpc = RpcClientNetworkerPipe();
final rpc = NamedRpcClientNetworkerPipe<NetworkEvent>();
_setupRpc(rpc, client);
final completer = Completer<Uint8List?>();
rpc
Expand All @@ -147,52 155,43 @@ class NetworkingService {
_users.add({});
}

void _setupRpc(RpcNetworkerPipe rpc, NetworkerBase networker) {
void _setupRpc(
NamedRpcNetworkerPipe<NetworkEvent> rpc, NetworkerBase networker) {
rpc.registerNamedFunctions(NetworkEvent.values);
rpc.getNamedFunction(NetworkEvent.event)?.connect(RawJsonNetworkerPlugin()
..read.listen((message) {
final event = DocumentEvent.fromJson(message.data);
onMessage(event);
}));
rpc
.registerFunction(NetworkEvent.event.index, mode: RpcNetworkerMode.any)
.connect(RawJsonNetworkerPlugin()
..read.listen((message) {
final event = DocumentEvent.fromJson(message.data);
onMessage(event);
}));
rpc
.registerFunction(
NetworkEvent.connections.index,
mode: RpcNetworkerMode.authority,
)
.connect(RawJsonNetworkerPlugin()
.getNamedFunction(NetworkEvent.connections)
?.connect(RawJsonNetworkerPlugin()
..read.listen((message) {
final ids = Set<Channel>.from(message.data);
_connections.add(ids);
_users.add(Map.from(_users.value)
..removeWhere((key, value) => !ids.contains(key)));
}));
rpc
.registerFunction(
NetworkEvent.user.index,
mode: RpcNetworkerMode.any,
)
.connect(RawJsonNetworkerPlugin()
..read.listen((message) {
final user = NetworkingUser.fromJson(message.data);
final users = Map<Channel?, NetworkingUser>.from(_users.value)
..[message.channel] = user;
_users.add(users);
_bloc?.state.currentIndexCubit
?.updateNetworkingState(_bloc!, users);
}));
rpc.getNamedFunction(NetworkEvent.user)?.connect(RawJsonNetworkerPlugin()
..read.listen((message) {
final user = NetworkingUser.fromJson(message.data);
final users = Map<Channel?, NetworkingUser>.from(_users.value)
..[message.channel] = user;
_users.add(users);
_bloc?.state.currentIndexCubit?.updateNetworkingState(_bloc!, users);
}));
}

void sendUser(NetworkingUser user) {
state?.$2.callFunction(NetworkEvent.user.index,
state?.$2.callNamedFunction(NetworkEvent.user,
Uint8List.fromList(jsonEncode(user.toJson()).codeUnits));
}

bool _externalEvent = false;

void onEvent(DocumentEvent event) {
if (!event.shouldSync() || _externalEvent) return;
state?.$2.callFunction(NetworkEvent.event.index,
state?.$2.callNamedFunction(NetworkEvent.event,
Uint8List.fromList(jsonEncode(event.toJson()).codeUnits));
}

Expand Down
8 changes: 4 additions & 4 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -914,17 914,17 @@ packages:
dependency: "direct main"
description:
path: "packages/networker/networker"
ref: "5a3658cad80fcb8863b8be6f84460e77607eb959"
resolved-ref: "5a3658cad80fcb8863b8be6f84460e77607eb959"
ref: "071e02761bf55a47bcde0cdc2a711b8ff5fbbb18"
resolved-ref: "071e02761bf55a47bcde0cdc2a711b8ff5fbbb18"
url: "https://github.com/LinwoodDev/dart_pkgs"
source: git
version: "1.0.0"
networker_socket:
dependency: "direct main"
description:
path: "packages/networker/networker_socket"
ref: "40b8553ab86ab4f06d9f689268a65d65ed936fda"
resolved-ref: "40b8553ab86ab4f06d9f689268a65d65ed936fda"
ref: "9085d42cf75c3d3edc8edb9ef22697dbbe2bf38c"
resolved-ref: "9085d42cf75c3d3edc8edb9ef22697dbbe2bf38c"
url: "https://github.com/LinwoodDev/dart_pkgs"
source: git
version: "1.0.0"
Expand Down
4 changes: 2 additions & 2 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 79,12 @@ dependencies:
networker:
git:
url: https://github.com/LinwoodDev/dart_pkgs
ref: 5a3658cad80fcb8863b8be6f84460e77607eb959
ref: 071e02761bf55a47bcde0cdc2a711b8ff5fbbb18
path: packages/networker/networker
networker_socket:
git:
url: https://github.com/LinwoodDev/dart_pkgs
ref: 40b8553ab86ab4f06d9f689268a65d65ed936fda
ref: 9085d42cf75c3d3edc8edb9ef22697dbbe2bf38c
path: packages/networker/networker_socket
lw_file_system:
git:
Expand Down
1 change: 1 addition & 0 deletions metadata/en-US/changelogs/120.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 10,7 @@
* Fix property view logic
* Fix property view ui padding
* Fix archive importing
* Fix collaboration dialog
* Upgrade networking stack for collaboration experiment
* Update flutter and agp

Expand Down

0 comments on commit 6c5b7bc

Please sign in to comment.