Merge pull request #1170 from GetStream/refactor/enums

refactor(llc, ui): migrate enums to v3
This commit is contained in:
Salvatore Giordano
2022-05-20 12:37:25 +02:00
committed by GitHub
15 changed files with 92 additions and 124 deletions
@@ -10,15 +10,6 @@ enum PushProvider {
apn, apn,
} }
/// Helper extension for [PushProvider]
extension PushProviderX on PushProvider {
/// Returns the string notion for [PushProvider].
String get name => {
PushProvider.apn: 'apn',
PushProvider.firebase: 'firebase',
}[this]!;
}
/// Defines the api dedicated to device operations /// Defines the api dedicated to device operations
class DeviceApi { class DeviceApi {
/// Initialize a new device api /// Initialize a new device api
@@ -36,7 +36,7 @@ class AuthInterceptor extends Interceptor {
final params = {'user_id': token.userId}; final params = {'user_id': token.userId};
final headers = { final headers = {
'Authorization': token.rawValue, 'Authorization': token.rawValue,
'stream-auth-type': token.authType.raw, 'stream-auth-type': token.authType.name,
}; };
options options
..queryParameters.addAll(params) ..queryParameters.addAll(params)
@@ -18,15 +18,6 @@ enum AuthType {
anonymous, anonymous,
} }
/// Extension for returning the AuthType as a string
extension AuthTypeX on AuthType {
/// Returns the AuthType as a string
String get raw => {
AuthType.jwt: 'jwt',
AuthType.anonymous: 'anonymous',
}[this]!;
}
/// Token designed to store the JWT and the user it is related to. /// Token designed to store the JWT and the user it is related to.
class Token extends Equatable { class Token extends Equatable {
const Token._({ const Token._({
@@ -90,6 +90,9 @@ class AttachmentFile {
/// Union class to hold various [UploadState] of a attachment. /// Union class to hold various [UploadState] of a attachment.
@freezed @freezed
class UploadState with _$UploadState { class UploadState with _$UploadState {
// Dummy private constructor in order to use getters
const UploadState._();
/// Preparing state of the union /// Preparing state of the union
const factory UploadState.preparing() = Preparing; const factory UploadState.preparing() = Preparing;
@@ -108,10 +111,7 @@ class UploadState with _$UploadState {
/// Creates a new instance from a json /// Creates a new instance from a json
factory UploadState.fromJson(Map<String, dynamic> json) => factory UploadState.fromJson(Map<String, dynamic> json) =>
_$UploadStateFromJson(json); _$UploadStateFromJson(json);
}
/// Helper extension for UploadState
extension UploadStateX on UploadState? {
/// Returns true if state is [Preparing] /// Returns true if state is [Preparing]
bool get isPreparing => this is Preparing; bool get isPreparing => this is Preparing;
@@ -122,8 +122,10 @@ class __$$PreparingCopyWithImpl<$Res> extends _$UploadStateCopyWithImpl<$Res>
/// @nodoc /// @nodoc
@JsonSerializable() @JsonSerializable()
class _$Preparing implements Preparing { class _$Preparing extends Preparing {
const _$Preparing({final String? $type}) : $type = $type ?? 'preparing'; const _$Preparing({final String? $type})
: $type = $type ?? 'preparing',
super._();
factory _$Preparing.fromJson(Map<String, dynamic> json) => factory _$Preparing.fromJson(Map<String, dynamic> json) =>
_$$PreparingFromJson(json); _$$PreparingFromJson(json);
@@ -226,8 +228,9 @@ class _$Preparing implements Preparing {
} }
} }
abstract class Preparing implements UploadState { abstract class Preparing extends UploadState {
const factory Preparing() = _$Preparing; const factory Preparing() = _$Preparing;
const Preparing._() : super._();
factory Preparing.fromJson(Map<String, dynamic> json) = _$Preparing.fromJson; factory Preparing.fromJson(Map<String, dynamic> json) = _$Preparing.fromJson;
} }
@@ -270,10 +273,11 @@ class __$$InProgressCopyWithImpl<$Res> extends _$UploadStateCopyWithImpl<$Res>
/// @nodoc /// @nodoc
@JsonSerializable() @JsonSerializable()
class _$InProgress implements InProgress { class _$InProgress extends InProgress {
const _$InProgress( const _$InProgress(
{required this.uploaded, required this.total, final String? $type}) {required this.uploaded, required this.total, final String? $type})
: $type = $type ?? 'inProgress'; : $type = $type ?? 'inProgress',
super._();
factory _$InProgress.fromJson(Map<String, dynamic> json) => factory _$InProgress.fromJson(Map<String, dynamic> json) =>
_$$InProgressFromJson(json); _$$InProgressFromJson(json);
@@ -392,9 +396,10 @@ class _$InProgress implements InProgress {
} }
} }
abstract class InProgress implements UploadState { abstract class InProgress extends UploadState {
const factory InProgress( const factory InProgress(
{required final int uploaded, required final int total}) = _$InProgress; {required final int uploaded, required final int total}) = _$InProgress;
const InProgress._() : super._();
factory InProgress.fromJson(Map<String, dynamic> json) = factory InProgress.fromJson(Map<String, dynamic> json) =
_$InProgress.fromJson; _$InProgress.fromJson;
@@ -424,8 +429,10 @@ class __$$SuccessCopyWithImpl<$Res> extends _$UploadStateCopyWithImpl<$Res>
/// @nodoc /// @nodoc
@JsonSerializable() @JsonSerializable()
class _$Success implements Success { class _$Success extends Success {
const _$Success({final String? $type}) : $type = $type ?? 'success'; const _$Success({final String? $type})
: $type = $type ?? 'success',
super._();
factory _$Success.fromJson(Map<String, dynamic> json) => factory _$Success.fromJson(Map<String, dynamic> json) =>
_$$SuccessFromJson(json); _$$SuccessFromJson(json);
@@ -528,8 +535,9 @@ class _$Success implements Success {
} }
} }
abstract class Success implements UploadState { abstract class Success extends UploadState {
const factory Success() = _$Success; const factory Success() = _$Success;
const Success._() : super._();
factory Success.fromJson(Map<String, dynamic> json) = _$Success.fromJson; factory Success.fromJson(Map<String, dynamic> json) = _$Success.fromJson;
} }
@@ -565,9 +573,10 @@ class __$$FailedCopyWithImpl<$Res> extends _$UploadStateCopyWithImpl<$Res>
/// @nodoc /// @nodoc
@JsonSerializable() @JsonSerializable()
class _$Failed implements Failed { class _$Failed extends Failed {
const _$Failed({required this.error, final String? $type}) const _$Failed({required this.error, final String? $type})
: $type = $type ?? 'failed'; : $type = $type ?? 'failed',
super._();
factory _$Failed.fromJson(Map<String, dynamic> json) => factory _$Failed.fromJson(Map<String, dynamic> json) =>
_$$FailedFromJson(json); _$$FailedFromJson(json);
@@ -681,8 +690,9 @@ class _$Failed implements Failed {
} }
} }
abstract class Failed implements UploadState { abstract class Failed extends UploadState {
const factory Failed({required final String error}) = _$Failed; const factory Failed({required final String error}) = _$Failed;
const Failed._() : super._();
factory Failed.fromJson(Map<String, dynamic> json) = _$Failed.fromJson; factory Failed.fromJson(Map<String, dynamic> json) = _$Failed.fromJson;
@@ -53,29 +53,43 @@ enum FilterOperator {
nor, nor,
/// Matches any list that contains the specified value /// Matches any list that contains the specified value
contains, contains;
}
/// Helper extension for [FilterOperator] @override
extension FilterOperatorX on FilterOperator { String toString() {
/// Converts [FilterOperator] into rew values switch (this) {
String get rawValue => { case FilterOperator.equal:
FilterOperator.equal: r'$eq', return r'$eq';
FilterOperator.notEqual: r'$ne', case FilterOperator.notEqual:
FilterOperator.greater: r'$gt', return r'$ne';
FilterOperator.greaterOrEqual: r'$gte', case FilterOperator.greater:
FilterOperator.less: r'$lt', return r'$gt';
FilterOperator.lessOrEqual: r'$lte', case FilterOperator.greaterOrEqual:
FilterOperator.in_: r'$in', return r'$gte';
FilterOperator.notIn: r'$nin', case FilterOperator.less:
FilterOperator.query: r'$q', return r'$lt';
FilterOperator.autoComplete: r'$autocomplete', case FilterOperator.lessOrEqual:
FilterOperator.exists: r'$exists', return r'$lte';
FilterOperator.and: r'$and', case FilterOperator.in_:
FilterOperator.or: r'$or', return r'$in';
FilterOperator.nor: r'$nor', case FilterOperator.notIn:
FilterOperator.contains: r'$contains', return r'$nin';
}[this]!; case FilterOperator.query:
return r'$q';
case FilterOperator.autoComplete:
return r'$autocomplete';
case FilterOperator.exists:
return r'$exists';
case FilterOperator.and:
return r'$and';
case FilterOperator.or:
return r'$or';
case FilterOperator.nor:
return r'$nor';
case FilterOperator.contains:
return r'$contains';
}
}
} }
/// Stream supports a limited set of filters for querying channels, /// Stream supports a limited set of filters for querying channels,
@@ -96,11 +110,11 @@ class Filter extends Equatable {
this.key, this.key,
}); });
Filter._({ const Filter._({
required FilterOperator operator, required FilterOperator operator,
required this.value, required this.value,
this.key, this.key,
}) : operator = operator.rawValue; }) : operator = '$operator';
/// An empty filter /// An empty filter
const Filter.empty() const Filter.empty()
@@ -215,7 +229,7 @@ class Filter extends Equatable {
/// Serializes to json object /// Serializes to json object
Map<String, Object?> toJson() { Map<String, Object?> toJson() {
final json = <String, Object?>{}; final json = <String, Object?>{};
final groupOperators = _groupOperators.map((it) => it.rawValue); final groupOperators = _groupOperators.map((it) => '$it');
if (groupOperators.contains(operator)) { if (groupOperators.contains(operator)) {
// Filters with group operators are encoded in the following form: // Filters with group operators are encoded in the following form:
@@ -1,29 +0,0 @@
///
enum Location {
///
usEast,
///
euWest,
///
mumbai,
///
sydney,
///
singapore,
}
///
extension LocationX on Location {
///
String get name => {
Location.usEast: 'us-east',
Location.euWest: 'dublin',
Location.mumbai: 'mumbai',
Location.sydney: 'sydney',
Location.singapore: 'singapore',
}[this]!;
}
@@ -6,7 +6,6 @@ import 'package:logging/logging.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:rxdart/rxdart.dart'; import 'package:rxdart/rxdart.dart';
import 'package:stream_chat/src/core/error/error.dart'; import 'package:stream_chat/src/core/error/error.dart';
import 'package:stream_chat/src/core/http/token.dart';
import 'package:stream_chat/src/core/http/token_manager.dart'; import 'package:stream_chat/src/core/http/token_manager.dart';
import 'package:stream_chat/src/core/models/event.dart'; import 'package:stream_chat/src/core/models/event.dart';
import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/models/user.dart';
@@ -163,7 +162,7 @@ class WebSocket with TimerHelper {
'json': jsonEncode(params), 'json': jsonEncode(params),
'api_key': apiKey, 'api_key': apiKey,
'authorization': token.rawValue, 'authorization': token.rawValue,
'stream-auth-type': token.authType.raw, 'stream-auth-type': token.authType.name,
...queryParameters, ...queryParameters,
}; };
final scheme = baseUrl.startsWith('https') ? 'wss' : 'ws'; final scheme = baseUrl.startsWith('https') ? 'wss' : 'ws';
@@ -36,7 +36,6 @@ export './src/core/models/user.dart';
export './src/core/util/extension.dart'; export './src/core/util/extension.dart';
export './src/db/chat_persistence_client.dart'; export './src/db/chat_persistence_client.dart';
export './src/event_type.dart'; export './src/event_type.dart';
export './src/location.dart';
export './src/permission_type.dart'; export './src/permission_type.dart';
export './src/ws/connection_status.dart'; export './src/ws/connection_status.dart';
export 'src/client/channel.dart'; export 'src/client/channel.dart';
@@ -67,5 +66,4 @@ export 'src/core/models/user.dart';
export 'src/core/util/extension.dart'; export 'src/core/util/extension.dart';
export 'src/db/chat_persistence_client.dart'; export 'src/db/chat_persistence_client.dart';
export 'src/event_type.dart'; export 'src/event_type.dart';
export 'src/location.dart';
export 'src/ws/connection_status.dart'; export 'src/ws/connection_status.dart';
@@ -1,5 +1,4 @@
import 'package:mocktail/mocktail.dart'; import 'package:mocktail/mocktail.dart';
import 'package:stream_chat/src/core/api/device_api.dart';
import 'package:stream_chat/src/core/http/token.dart'; import 'package:stream_chat/src/core/http/token.dart';
import 'package:stream_chat/src/core/models/banned_user.dart'; import 'package:stream_chat/src/core/models/banned_user.dart';
import 'package:stream_chat/stream_chat.dart'; import 'package:stream_chat/stream_chat.dart';
@@ -46,7 +46,7 @@ void main() {
expect(updateHeaders.containsKey('Authorization'), isTrue); expect(updateHeaders.containsKey('Authorization'), isTrue);
expect(updateHeaders['Authorization'], token.rawValue); expect(updateHeaders['Authorization'], token.rawValue);
expect(updateHeaders.containsKey('stream-auth-type'), isTrue); expect(updateHeaders.containsKey('stream-auth-type'), isTrue);
expect(updateHeaders['stream-auth-type'], token.authType.raw); expect(updateHeaders['stream-auth-type'], token.authType.name);
expect(updatedQueryParams.containsKey('user_id'), isTrue); expect(updatedQueryParams.containsKey('user_id'), isTrue);
expect(updatedQueryParams['user_id'], token.userId); expect(updatedQueryParams['user_id'], token.userId);
@@ -10,7 +10,7 @@ void main() {
expect(token.userId, userId); expect(token.userId, userId);
expect(token.rawValue, isEmpty); expect(token.rawValue, isEmpty);
expect(token.authType, AuthType.anonymous); expect(token.authType, AuthType.anonymous);
expect(token.authType.raw, AuthType.anonymous.raw); expect(token.authType.name, AuthType.anonymous.name);
}); });
test('`.fromRawValue` should create token from rawValue', () { test('`.fromRawValue` should create token from rawValue', () {
@@ -36,7 +36,7 @@ void main() {
expect(token.userId, userId); expect(token.userId, userId);
expect(token.rawValue, isNotEmpty); expect(token.rawValue, isNotEmpty);
expect(token.authType, AuthType.jwt); expect(token.authType, AuthType.jwt);
expect(token.authType.raw, AuthType.jwt.raw); expect(token.authType.name, AuthType.jwt.name);
}); });
test( test(
@@ -51,7 +51,7 @@ void main() {
expect(token.userId, user.id); expect(token.userId, user.id);
expect(token.rawValue, isNotEmpty); expect(token.rawValue, isNotEmpty);
expect(token.authType, AuthType.jwt); expect(token.authType, AuthType.jwt);
expect(token.authType.raw, AuthType.jwt.raw); expect(token.authType.name, AuthType.jwt.name);
}, },
); );
} }
@@ -11,7 +11,7 @@ void main() {
final filter = Filter.equal(key, value); final filter = Filter.equal(key, value);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, value); expect(filter.value, value);
expect(filter.operator, FilterOperator.equal.rawValue); expect(filter.operator, FilterOperator.equal.toString());
}); });
test('notEqual', () { test('notEqual', () {
@@ -20,7 +20,7 @@ void main() {
final filter = Filter.notEqual(key, value); final filter = Filter.notEqual(key, value);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, value); expect(filter.value, value);
expect(filter.operator, FilterOperator.notEqual.rawValue); expect(filter.operator, FilterOperator.notEqual.toString());
}); });
test('greater', () { test('greater', () {
@@ -29,7 +29,7 @@ void main() {
final filter = Filter.greater(key, value); final filter = Filter.greater(key, value);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, value); expect(filter.value, value);
expect(filter.operator, FilterOperator.greater.rawValue); expect(filter.operator, FilterOperator.greater.toString());
}); });
test('greaterOrEqual', () { test('greaterOrEqual', () {
@@ -38,7 +38,7 @@ void main() {
final filter = Filter.greaterOrEqual(key, value); final filter = Filter.greaterOrEqual(key, value);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, value); expect(filter.value, value);
expect(filter.operator, FilterOperator.greaterOrEqual.rawValue); expect(filter.operator, FilterOperator.greaterOrEqual.toString());
}); });
test('less', () { test('less', () {
@@ -47,7 +47,7 @@ void main() {
final filter = Filter.less(key, value); final filter = Filter.less(key, value);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, value); expect(filter.value, value);
expect(filter.operator, FilterOperator.less.rawValue); expect(filter.operator, FilterOperator.less.toString());
}); });
test('lessOrEqual', () { test('lessOrEqual', () {
@@ -56,7 +56,7 @@ void main() {
final filter = Filter.lessOrEqual(key, value); final filter = Filter.lessOrEqual(key, value);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, value); expect(filter.value, value);
expect(filter.operator, FilterOperator.lessOrEqual.rawValue); expect(filter.operator, FilterOperator.lessOrEqual.toString());
}); });
test('in', () { test('in', () {
@@ -65,7 +65,7 @@ void main() {
final filter = Filter.in_(key, values); final filter = Filter.in_(key, values);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, values); expect(filter.value, values);
expect(filter.operator, FilterOperator.in_.rawValue); expect(filter.operator, FilterOperator.in_.toString());
}); });
test('in', () { test('in', () {
@@ -74,7 +74,7 @@ void main() {
final filter = Filter.in_(key, values); final filter = Filter.in_(key, values);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, values); expect(filter.value, values);
expect(filter.operator, FilterOperator.in_.rawValue); expect(filter.operator, FilterOperator.in_.toString());
}); });
test('notIn', () { test('notIn', () {
@@ -83,7 +83,7 @@ void main() {
final filter = Filter.notIn(key, values); final filter = Filter.notIn(key, values);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, values); expect(filter.value, values);
expect(filter.operator, FilterOperator.notIn.rawValue); expect(filter.operator, FilterOperator.notIn.toString());
}); });
test('query', () { test('query', () {
@@ -92,7 +92,7 @@ void main() {
final filter = Filter.query(key, value); final filter = Filter.query(key, value);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, value); expect(filter.value, value);
expect(filter.operator, FilterOperator.query.rawValue); expect(filter.operator, FilterOperator.query.toString());
}); });
test('autoComplete', () { test('autoComplete', () {
@@ -101,7 +101,7 @@ void main() {
final filter = Filter.autoComplete(key, value); final filter = Filter.autoComplete(key, value);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, value); expect(filter.value, value);
expect(filter.operator, FilterOperator.autoComplete.rawValue); expect(filter.operator, FilterOperator.autoComplete.toString());
}); });
test('exists', () { test('exists', () {
@@ -109,7 +109,7 @@ void main() {
final filter = Filter.exists(key); final filter = Filter.exists(key);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, isTrue); expect(filter.value, isTrue);
expect(filter.operator, FilterOperator.exists.rawValue); expect(filter.operator, FilterOperator.exists.toString());
}); });
test('notExists', () { test('notExists', () {
@@ -117,7 +117,7 @@ void main() {
final filter = Filter.notExists(key); final filter = Filter.notExists(key);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, isFalse); expect(filter.value, isFalse);
expect(filter.operator, FilterOperator.exists.rawValue); expect(filter.operator, FilterOperator.exists.toString());
}); });
test('custom', () { test('custom', () {
@@ -149,7 +149,7 @@ void main() {
final filter = Filter.contains(key, values); final filter = Filter.contains(key, values);
expect(filter.key, key); expect(filter.key, key);
expect(filter.value, values); expect(filter.value, values);
expect(filter.operator, FilterOperator.contains.rawValue); expect(filter.operator, FilterOperator.contains.toString());
}); });
group('groupedOperator', () { group('groupedOperator', () {
@@ -161,21 +161,21 @@ void main() {
final filter = Filter.and(filters); final filter = Filter.and(filters);
expect(filter.key, isNull); expect(filter.key, isNull);
expect(filter.value, filters); expect(filter.value, filters);
expect(filter.operator, FilterOperator.and.rawValue); expect(filter.operator, FilterOperator.and.toString());
}); });
test('or', () { test('or', () {
final filter = Filter.or(filters); final filter = Filter.or(filters);
expect(filter.key, isNull); expect(filter.key, isNull);
expect(filter.value, filters); expect(filter.value, filters);
expect(filter.operator, FilterOperator.or.rawValue); expect(filter.operator, FilterOperator.or.toString());
}); });
test('nor', () { test('nor', () {
final filter = Filter.nor(filters); final filter = Filter.nor(filters);
expect(filter.key, isNull); expect(filter.key, isNull);
expect(filter.value, filters); expect(filter.value, filters);
expect(filter.operator, FilterOperator.nor.rawValue); expect(filter.operator, FilterOperator.nor.toString());
}); });
}); });
}); });
@@ -189,7 +189,7 @@ void main() {
final encoded = json.encode(filter); final encoded = json.encode(filter);
expect( expect(
encoded, encoded,
'{"$key":{"${FilterOperator.equal.rawValue}":${json.encode(value)}}}', '''{"$key":{"${FilterOperator.equal.toString()}":${json.encode(value)}}}''',
); );
}); });
test('listValue', () { test('listValue', () {
@@ -199,7 +199,7 @@ void main() {
final encoded = json.encode(filter); final encoded = json.encode(filter);
expect( expect(
encoded, encoded,
'{"$key":{"${FilterOperator.in_.rawValue}":${json.encode(values)}}}', '''{"$key":{"${FilterOperator.in_.toString()}":${json.encode(values)}}}''',
); );
}); });
@@ -243,7 +243,7 @@ void main() {
final encoded = json.encode(filter); final encoded = json.encode(filter);
expect( expect(
encoded, encoded,
'{"${FilterOperator.and.rawValue}":${json.encode(filters)}}', '{"${FilterOperator.and.toString()}":${json.encode(filters)}}',
); );
}); });
@@ -1,6 +1,5 @@
export 'attachment_upload_state_builder.dart'; export 'attachment_upload_state_builder.dart';
export 'attachment_widget.dart' export 'attachment_widget.dart' show AttachmentError, AttachmentSource;
show AttachmentError, AttachmentSource, AttachmentSourceX;
export 'file_attachment.dart'; export 'file_attachment.dart';
export 'giphy_attachment.dart'; export 'giphy_attachment.dart';
export 'image_attachment.dart'; export 'image_attachment.dart';
@@ -7,14 +7,10 @@ enum AttachmentSource {
local, local,
/// Attachment is uploaded /// Attachment is uploaded
network, network;
}
/// Extension for identifying type of attachment
extension AttachmentSourceX on AttachmentSource {
/// The [when] method is the equivalent to pattern matching. /// The [when] method is the equivalent to pattern matching.
/// Its prototype depends on the AttachmentSource defined. /// Its prototype depends on the AttachmentSource defined.
// ignore: missing_return
T when<T>({ T when<T>({
required T Function() local, required T Function() local,
required T Function() network, required T Function() network,