Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-4629] fix: propagate id and key fields for connection object for Android and iOS #505

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import java.util.Map;

import io.ably.flutter.plugin.dto.EnrichedConnectionStateChange;
import io.ably.flutter.plugin.generated.PlatformConstants;
import io.ably.lib.realtime.AblyRealtime;
import io.ably.lib.realtime.Channel;
import io.ably.lib.realtime.ChannelStateListener;
import io.ably.lib.realtime.ConnectionStateListener;
Expand Down Expand Up @@ -76,12 +78,15 @@ static private class Listener {

static private class PluginConnectionStateListener extends Listener implements ConnectionStateListener {

PluginConnectionStateListener(EventChannel.EventSink eventSink) {
private final AblyRealtime realtime;

PluginConnectionStateListener(EventChannel.EventSink eventSink, AblyRealtime realtime) {
super(eventSink);
this.realtime = realtime;
}

public void onConnectionStateChanged(ConnectionStateChange stateChange) {
eventSink.success(stateChange);
eventSink.success(new EnrichedConnectionStateChange(stateChange, realtime.connection.id, realtime.connection.key));
}

}
Expand Down Expand Up @@ -132,8 +137,9 @@ public void onListen(Object object, EventChannel.EventSink uiThreadEventSink) {
try {
switch (eventName) {
case PlatformConstants.PlatformMethod.onRealtimeConnectionStateChanged:
connectionStateListener = new PluginConnectionStateListener(eventSink);
instanceStore.getRealtime(ablyMessage.handle).connection.on(connectionStateListener);
final AblyRealtime realtime = instanceStore.getRealtime(ablyMessage.handle);
connectionStateListener = new PluginConnectionStateListener(eventSink, realtime);
realtime.connection.on(connectionStateListener);
break;
case PlatformConstants.PlatformMethod.onRealtimeChannelStateChanged:
assert eventPayload != null : "onRealtimeChannelStateChanged: event message is missing";
Expand Down
18 changes: 10 additions & 8 deletions android/src/main/java/io/ably/flutter/plugin/AblyMessageCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.HashMap;
import java.util.Map;

import io.ably.flutter.plugin.dto.EnrichedConnectionStateChange;
import io.ably.flutter.plugin.generated.PlatformConstants;
import io.ably.flutter.plugin.types.SerializationException;
import io.ably.flutter.plugin.types.PlatformClientOptions;
Expand All @@ -30,7 +31,6 @@
import io.ably.lib.realtime.ChannelStateListener;
import io.ably.lib.realtime.ConnectionEvent;
import io.ably.lib.realtime.ConnectionState;
import io.ably.lib.realtime.ConnectionStateListener;
import io.ably.lib.rest.Auth;
import io.ably.lib.rest.Auth.TokenDetails;
import io.ably.lib.rest.DeviceDetails;
Expand Down Expand Up @@ -186,7 +186,7 @@ private Byte getType(Object value) {
return PlatformConstants.CodecTypes.tokenParams;
} else if (value instanceof AsyncPaginatedResult) {
return PlatformConstants.CodecTypes.paginatedResult;
} else if (value instanceof ConnectionStateListener.ConnectionStateChange) {
} else if (value instanceof EnrichedConnectionStateChange) {
return PlatformConstants.CodecTypes.connectionStateChange;
} else if (value instanceof ChannelStateListener.ChannelStateChange) {
return PlatformConstants.CodecTypes.channelStateChange;
Expand Down Expand Up @@ -821,14 +821,16 @@ private Map<String, Object> encodePaginatedResult(AsyncPaginatedResult<Object> c
return jsonMap;
}

private Map<String, Object> encodeConnectionStateChange(ConnectionStateListener.ConnectionStateChange c) {
private Map<String, Object> encodeConnectionStateChange(EnrichedConnectionStateChange c) {
if (c == null) return null;
final HashMap<String, Object> jsonMap = new HashMap<>();
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.current, encodeConnectionState(c.current));
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.previous, encodeConnectionState(c.previous));
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.event, encodeConnectionEvent(c.event));
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.retryIn, c.retryIn);
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.reason, encodeErrorInfo(c.reason));
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.current, encodeConnectionState(c.stateChange.current));
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.previous, encodeConnectionState(c.stateChange.previous));
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.event, encodeConnectionEvent(c.stateChange.event));
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.retryIn, c.stateChange.retryIn);
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.reason, encodeErrorInfo(c.stateChange.reason));
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.connectionId, c.connectionId);
writeValueToJson(jsonMap, PlatformConstants.TxConnectionStateChange.connectionKey,c.connectionKey);
return jsonMap;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.ably.flutter.plugin.dto;

import io.ably.lib.realtime.ConnectionStateListener;

public class EnrichedConnectionStateChange {
maratal marked this conversation as resolved.
Show resolved Hide resolved
public final ConnectionStateListener.ConnectionStateChange stateChange;
public final String connectionId;
public final String connectionKey;

public EnrichedConnectionStateChange(ConnectionStateListener.ConnectionStateChange stateChange, String connectionId, String connectionKey) {
this.stateChange = stateChange;
this.connectionId = connectionId;
this.connectionKey = connectionKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ static final public class TxConnectionStateChange {
public static final String event = "event";
public static final String retryIn = "retryIn";
public static final String reason = "reason";
public static final String connectionId = "connectionId";
public static final String connectionKey = "connectionKey";
}

static final public class TxChannelStateChange {
Expand Down
21 changes: 14 additions & 7 deletions example/lib/ui/realtime_sliver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,16 @@ class RealtimeSliver extends HookWidget {
);

Widget buildReleaseRealtimeChannelButton(
ValueNotifier<ably.ConnectionState> connectionState,
ValueNotifier<ably.ChannelState> channelState) =>
ValueNotifier<ably.ConnectionState> connectionState,
ValueNotifier<ably.ChannelState> channelState,
ValueNotifier<String?> connectionId,
) =>
TextButton(
onPressed: () async {
await channel.detach();
realtime.channels.release(Constants.channelName);
channel = realtime.channels.get(Constants.channelName);
setupListeners(connectionState, channelState);
setupListeners(connectionState, channelState, connectionId);
},
child: const Text('Release'),
);
Expand Down Expand Up @@ -176,6 +178,7 @@ class RealtimeSliver extends HookWidget {
Widget build(BuildContext context) {
final connectionState =
useState<ably.ConnectionState>(realtime.connection.state);
final connectionId = useState<String?>(realtime.connection.id);
final channelState = useState<ably.ChannelState>(channel.state);
final latestMessage = useState<ably.Message?>(null);
final channelSubscription =
Expand All @@ -185,7 +188,7 @@ class RealtimeSliver extends HookWidget {

useEffect(() {
realtime.time().then((value) => realtimeTime.value = value);
setupListeners(connectionState, channelState);
setupListeners(connectionState, channelState, connectionId);
return dispose;
}, []);

Expand All @@ -198,6 +201,7 @@ class RealtimeSliver extends HookWidget {
),
Text('Realtime time: ${realtimeTime.value}'),
Text('Connection State: ${connectionState.value}'),
Text('Connection Id: ${connectionId.value ?? '-'}'),
buildEncryptionSwitch(useEncryption),
Row(
children: <Widget>[
Expand All @@ -222,7 +226,7 @@ class RealtimeSliver extends HookWidget {
Expanded(child: buildChannelDetachButton(channelState.value)),
Expanded(
child: buildReleaseRealtimeChannelButton(
connectionState, channelState)),
connectionState, channelState, connectionId)),
],
),
Row(
Expand Down Expand Up @@ -286,15 +290,18 @@ class RealtimeSliver extends HookWidget {
);
}

void setupListeners(ValueNotifier<ably.ConnectionState> connectionState,
ValueNotifier<ably.ChannelState> channelState) {
void setupListeners(
ValueNotifier<ably.ConnectionState> connectionState,
ValueNotifier<ably.ChannelState> channelState,
ValueNotifier<String?> connectionId) {
dispose();
final connectionSubscription =
realtime.connection.on().listen((connectionStateChange) {
if (connectionStateChange.current == ably.ConnectionState.failed) {
logAndDisplayError(connectionStateChange.reason);
}
connectionState.value = connectionStateChange.current;
connectionId.value = realtime.connection.id;
print('${DateTime.now()}:'
' ConnectionStateChange event: ${connectionStateChange.event}'
'\nReason: ${connectionStateChange.reason}');
Expand Down
12 changes: 12 additions & 0 deletions ios/Classes/AblyFlutterStreamHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
@import Flutter;
#import "AblyFlutter.h"

@class ARTConnectionStateChange;

NS_ASSUME_NONNULL_BEGIN

@interface _AblyConnectionStateChange : NSObject

@property(nonatomic, readonly) ARTConnectionStateChange *value;
@property(nonatomic, readonly) NSString *connectionId;
@property(nonatomic, readonly) NSString *connectionKey;

- (instancetype)initWithStateChange:(ARTConnectionStateChange *)stateChange connectionId:(NSString *)connectionId connectionKey:(NSString *)connectionKey;

@end

@interface AblyFlutterStreamHandler : NSObject<FlutterStreamHandler>

@property(nonatomic, readonly) AblyInstanceStore * instanceStore;
Expand Down
17 changes: 16 additions & 1 deletion ios/Classes/AblyFlutterStreamHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
#import "AblyFlutterMessage.h"
#import "codec/AblyPlatformConstants.h"

@implementation _AblyConnectionStateChange

- (instancetype)initWithStateChange:(ARTConnectionStateChange *)stateChange connectionId:(NSString *)connectionId connectionKey:(NSString *)connectionKey {
self = [super init];
if (self) {
_value = stateChange;
_connectionId = connectionId;
_connectionKey = connectionKey;
}
return self;
}

@end

@implementation AblyFlutterStreamHandler{
ARTEventListener *listener;
}
Expand Down Expand Up @@ -42,7 +56,8 @@ - (void) startListening:(AblyFlutterMessage *const)message emitter:(FlutterEvent
details:eventName
]);
} else {
emitter(stateChange);
_AblyConnectionStateChange * const _stateChange = [[_AblyConnectionStateChange alloc] initWithStateChange:stateChange connectionId:realtime.connection.id connectionKey:realtime.connection.key];
emitter(_stateChange);
}
}];
} else if ([AblyPlatformMethod_onRealtimeChannelStateChanged isEqual: eventName]) {
Expand Down
17 changes: 10 additions & 7 deletions ios/Classes/codec/AblyFlutterWriter.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#import "AblyFlutterMessage.h"
#import "AblyFlutterReader.h"
#import "AblyPlatformConstants.h"
#import "AblyFlutterStreamHandler.h"


NS_ASSUME_NONNULL_BEGIN
Expand All @@ -21,7 +22,7 @@ + (UInt8) getType:(id)value{
return CodecTypeAblyMessage;
}else if([value isKindOfClass:[ARTErrorInfo class]]){
return CodecTypeErrorInfo;
}else if([value isKindOfClass:[ARTConnectionStateChange class]]){
} else if([value isKindOfClass:[_AblyConnectionStateChange class]]){
return CodecTypeConnectionStateChange;
}else if([value isKindOfClass:[ARTChannelStateChange class]]){
return CodecTypeChannelStateChange;
Expand Down Expand Up @@ -207,19 +208,21 @@ +(NSString *) encodeChannelEvent: (ARTChannelEvent) event {
}
}

static AblyCodecEncoder encodeConnectionStateChange = ^NSMutableDictionary*(ARTConnectionStateChange *const stateChange) {
static AblyCodecEncoder encodeConnectionStateChange = ^NSMutableDictionary*(_AblyConnectionStateChange *const stateChange) {
NSMutableDictionary<NSString *, NSObject *> *dictionary = [[NSMutableDictionary alloc] init];
WRITE_VALUE(dictionary,
TxConnectionStateChange_current,
[AblyFlutterWriter encodeConnectionState: [stateChange current]]);
[AblyFlutterWriter encodeConnectionState: [stateChange.value current]]);
WRITE_VALUE(dictionary,
TxConnectionStateChange_previous,
[AblyFlutterWriter encodeConnectionState: [stateChange previous]]);
[AblyFlutterWriter encodeConnectionState: [stateChange.value previous]]);
WRITE_VALUE(dictionary,
TxConnectionStateChange_event,
[AblyFlutterWriter encodeConnectionEvent: [stateChange event]]);
WRITE_VALUE(dictionary, TxConnectionStateChange_retryIn, [stateChange retryIn]?@((int)([stateChange retryIn] * 1000)):nil);
WRITE_VALUE(dictionary, TxConnectionStateChange_reason, encodeErrorInfo([stateChange reason]));
[AblyFlutterWriter encodeConnectionEvent: [stateChange.value event]]);
WRITE_VALUE(dictionary, TxConnectionStateChange_retryIn, [stateChange.value retryIn]?@((int)([stateChange.value retryIn] * 1000)):nil);
WRITE_VALUE(dictionary, TxConnectionStateChange_reason, encodeErrorInfo([stateChange.value reason]));
WRITE_VALUE(dictionary, TxConnectionStateChange_connectionId, stateChange.connectionId);
WRITE_VALUE(dictionary, TxConnectionStateChange_connectionKey, stateChange.connectionKey);
return dictionary;
};

Expand Down
2 changes: 2 additions & 0 deletions ios/Classes/codec/AblyPlatformConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ extern NSString *const TxConnectionStateChange_previous;
extern NSString *const TxConnectionStateChange_event;
extern NSString *const TxConnectionStateChange_retryIn;
extern NSString *const TxConnectionStateChange_reason;
extern NSString *const TxConnectionStateChange_connectionId;
extern NSString *const TxConnectionStateChange_connectionKey;

// key constants for ChannelStateChange
extern NSString *const TxChannelStateChange_current;
Expand Down
2 changes: 2 additions & 0 deletions ios/Classes/codec/AblyPlatformConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@
NSString *const TxConnectionStateChange_event = @"event";
NSString *const TxConnectionStateChange_retryIn = @"retryIn";
NSString *const TxConnectionStateChange_reason = @"reason";
NSString *const TxConnectionStateChange_connectionId = @"connectionId";
NSString *const TxConnectionStateChange_connectionKey = @"connectionKey";

// key constants for ChannelStateChange
NSString *const TxChannelStateChange_current = @"current";
Expand Down
2 changes: 2 additions & 0 deletions lib/src/generated/platform_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ class TxConnectionStateChange {
static const String event = 'event';
static const String retryIn = 'retryIn';
static const String reason = 'reason';
static const String connectionId = 'connectionId';
static const String connectionKey = 'connectionKey';
}

class TxChannelStateChange {
Expand Down
26 changes: 18 additions & 8 deletions lib/src/platform/src/codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ class Codec extends StandardMessageCodec {

// Events - Connection
CodecTypes.connectionStateChange:
_CodecPair<ConnectionStateChange>(null, _decodeConnectionStateChange),
_CodecPair<EnrichedConnectionStateChange>(
null, _decodeConnectionStateChange),

// Events - Channel
CodecTypes.channelStateChange:
Expand Down Expand Up @@ -1096,9 +1097,14 @@ class Codec extends StandardMessageCodec {
/// @nodoc
/// Decodes value [jsonMap] to [ConnectionStateChange]
/// returns null if [jsonMap] is null
ConnectionStateChange _decodeConnectionStateChange(
EnrichedConnectionStateChange _decodeConnectionStateChange(
Map<String, dynamic> jsonMap,
) {
final connectionId =
_readFromJson<String>(jsonMap, TxConnectionStateChange.connectionId);
final connectionKey =
_readFromJson<String>(jsonMap, TxConnectionStateChange.connectionKey);

final current = _decodeConnectionState(
_readFromJson<String>(jsonMap, TxConnectionStateChange.current));
final previous = _decodeConnectionState(
Expand All @@ -1112,12 +1118,16 @@ class Codec extends StandardMessageCodec {
TxConnectionStateChange.reason,
));
final reason = (errorInfo == null) ? null : _decodeErrorInfo(errorInfo);
return ConnectionStateChange(
current: current,
previous: previous,
event: event,
retryIn: retryIn,
reason: reason,
return EnrichedConnectionStateChange(
stateChange: ConnectionStateChange(
current: current,
previous: previous,
event: event,
retryIn: retryIn,
reason: reason,
),
connectionId: connectionId,
connectionKey: connectionKey,
);
}

Expand Down
Loading
Loading