-
Notifications
You must be signed in to change notification settings - Fork 27
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
refactor feature fetch message structure #1528
Open
ManhNTX
wants to merge
3
commits into
main
Choose a base branch
from
feature/refactor_code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
abstract class QueryParameter<T> { | ||
final String queryName; | ||
final T queryValue; | ||
|
||
QueryParameter(this.queryName, this.queryValue); | ||
} | ||
|
||
class BooleanQueryParameter extends QueryParameter<bool> { | ||
BooleanQueryParameter(String queryName, bool queryValue) : super(queryName, queryValue); | ||
} | ||
|
||
class StringQueryParameter extends QueryParameter<String> { | ||
StringQueryParameter(String queryName, String queryValue) : super(queryName, queryValue); | ||
} | ||
|
||
class IntQueryParameter extends QueryParameter<int> { | ||
IntQueryParameter(String queryName, int queryValue) : super(queryName, queryValue); | ||
} | ||
|
||
extension ListQueryParameterExtension on List<QueryParameter?> { | ||
Map<String, dynamic> toMap() => { for (var data in this) if (data != null) data.queryName : data.queryValue }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:twake/models/globals/globals.dart'; | ||
|
||
abstract class RepositoryHelper { | ||
navigatorDatasource({ | ||
VoidCallback? onLocal, | ||
VoidCallback? onRemote, | ||
}) { | ||
if (!Globals.instance.isNetworkConnected) { | ||
onLocal?.call(); | ||
return; | ||
} | ||
onRemote?.call(); | ||
Comment on lines
+9
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, it is not generic enough. For example: when we need to first: fetch data from local, in parallel, fetch the data with network to update. |
||
} | ||
} | ||
|
||
abstract class Repository with RepositoryHelper {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:twake/core/domain/state/failure.dart'; | ||
import 'package:twake/core/domain/state/success.dart'; | ||
|
||
|
||
@immutable | ||
abstract class AppState with EquatableMixin { | ||
final Either<Failure, Success> viewState; | ||
|
||
AppState(this.viewState); | ||
|
||
@override | ||
List<Object?> get props => [viewState]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:equatable/equatable.dart'; | ||
|
||
abstract class Failure extends Equatable {} | ||
|
||
abstract class FeatureFailure extends Failure {} | ||
|
||
class ExceptionFailure extends Failure { | ||
final dynamic exception; | ||
|
||
ExceptionFailure(this.exception); | ||
|
||
@override | ||
List<Object?> get props => [ | ||
exception, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'package:equatable/equatable.dart'; | ||
|
||
abstract class Success with EquatableMixin {} | ||
|
||
abstract class ViewState extends Success {} | ||
|
||
abstract class ViewEvent extends Success {} | ||
|
||
class UIState extends ViewState { | ||
static final idle = UIState(); | ||
|
||
UIState() : super(); | ||
|
||
@override | ||
List<Object?> get props => []; | ||
} | ||
|
||
class SuccessUIState<T> extends ViewState { | ||
final T result; | ||
|
||
SuccessUIState(this.result) : super(); | ||
|
||
@override | ||
List<Object?> get props => [ | ||
result, | ||
]; | ||
} | ||
|
||
class LoadingState extends UIState { | ||
LoadingState(); | ||
|
||
@override | ||
List<Object?> get props => []; | ||
} | ||
|
||
class LoadingMoreState extends UIState { | ||
LoadingMoreState(); | ||
|
||
@override | ||
List<Object?> get props => []; | ||
} | ||
|
||
class RefreshingState extends UIState { | ||
RefreshingState(); | ||
|
||
@override | ||
List<Object?> get props => []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:twake/core/domain/state/failure.dart'; | ||
|
||
abstract class UseCase<Input extends UseCaseInput, Success> { | ||
Either<ExceptionFailure, Success> execute({required Input input}); | ||
} | ||
|
||
abstract class NonInputUseCase<Success> { | ||
Either<ExceptionFailure, Success> execute(); | ||
} | ||
|
||
abstract class UseCaseInput {} |
16 changes: 16 additions & 0 deletions
16
lib/features/message/data/datasource/message_datasource.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:twake/features/message/data/model/message/response/message.dart'; | ||
|
||
abstract class MessageDataSource { | ||
Future<List<Message>> fetch({ | ||
String? companyId, | ||
String? workspaceId, | ||
required String channelId, | ||
String? threadId, | ||
String? afterMessageId, | ||
bool? withExistedFiles = false, | ||
}); | ||
|
||
Future<void> multiInsert({ | ||
required Iterable<Message> data, | ||
}); | ||
} |
47 changes: 47 additions & 0 deletions
47
lib/features/message/data/datasource_impl/local_message_datasource_impl.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'package:twake/features/message/data/datasource/message_datasource.dart'; | ||
import 'package:twake/features/message/data/model/message/response/message.dart'; | ||
import 'package:twake/services/storage_service.dart'; | ||
|
||
class LocalMessageDataSourceImpl extends MessageDataSource { | ||
final _storage = StorageService.instance; | ||
|
||
@override | ||
Future<List<Message>> fetch( | ||
{String? companyId, | ||
String? workspaceId, | ||
required String channelId, | ||
String? threadId, | ||
String? afterMessageId, | ||
bool? withExistedFiles = false}) async { | ||
var where = 'channel_id = ?'; | ||
if (threadId == null) { | ||
where += ' AND thread_id = id'; | ||
} else { | ||
where += ' AND thread_id = ?'; | ||
} | ||
if (withExistedFiles == true) { | ||
where += ' AND files <> ?'; | ||
} | ||
|
||
final localResult = await _storage.select( | ||
table: Table.message, | ||
where: where, | ||
limit: threadId == null ? 25 : 9999, | ||
orderBy: 'created_at DESC', | ||
whereArgs: [ | ||
channelId, | ||
if (threadId != null) threadId, | ||
if (withExistedFiles == true) '[]' | ||
], | ||
); | ||
final messages = | ||
localResult.map((entry) => Message.fromJson(entry)).toList(); | ||
|
||
return messages; | ||
} | ||
|
||
@override | ||
Future<void> multiInsert({required Iterable<Message> data}) async { | ||
await _storage.multiInsert(table: Table.message, data: data); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
lib/features/message/data/datasource_impl/remote_message_datasource_impl.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import 'package:sprintf/sprintf.dart'; | ||
import 'package:twake/core/data/model/query/query_parameter.dart'; | ||
import 'package:twake/features/message/data/datasource/message_datasource.dart'; | ||
import 'package:twake/features/message/data/model/message/response/message.dart'; | ||
import 'package:twake/models/globals/globals.dart'; | ||
import 'package:twake/services/api_service.dart'; | ||
import 'package:twake/services/endpoints.dart'; | ||
|
||
class RemoteMessageDataSourceImpl extends MessageDataSource { | ||
final _api = ApiService.instance; | ||
|
||
@override | ||
Future<List<Message>> fetch( | ||
{String? companyId, | ||
String? workspaceId, | ||
required String channelId, | ||
String? threadId, | ||
String? afterMessageId, | ||
bool? withExistedFiles = false}) async { | ||
List<dynamic> remoteResult; | ||
|
||
final queryParameters = <QueryParameter?>[ | ||
IntQueryParameter('include_users', 1), | ||
BooleanQueryParameter('emoji', false), | ||
StringQueryParameter('direction', 'history'), | ||
IntQueryParameter('limit', 25), | ||
]; | ||
|
||
if (afterMessageId != null) { | ||
queryParameters.addAll([ | ||
StringQueryParameter('page_token', afterMessageId), | ||
StringQueryParameter('direction', 'future'), | ||
]); | ||
} | ||
if (withExistedFiles == true) { | ||
queryParameters.add( | ||
StringQueryParameter('filter', 'files'), | ||
); | ||
} | ||
if (threadId == null) { | ||
remoteResult = await _api.get( | ||
endpoint: sprintf(Endpoint.threadsChannel, [ | ||
companyId ?? Globals.instance.companyId, | ||
workspaceId ?? Globals.instance.workspaceId, | ||
channelId | ||
]), | ||
queryParameters: queryParameters.toMap(), | ||
key: 'resources', | ||
); | ||
} else { | ||
final queryParameters = <QueryParameter?>[ | ||
IntQueryParameter('include_users', 1), | ||
BooleanQueryParameter('emoji', false), | ||
StringQueryParameter('direction', 'history'), | ||
]; | ||
|
||
remoteResult = await _api.get( | ||
endpoint: sprintf(Endpoint.threadMessages, [ | ||
companyId ?? Globals.instance.companyId, | ||
threadId, | ||
]), | ||
queryParameters: queryParameters.toMap(), | ||
key: 'resources', | ||
); | ||
} | ||
|
||
return remoteResult | ||
.where((rm) => | ||
rm['type'] == 'message' && | ||
rm['subtype'] != 'system' && | ||
rm['subtype'] != | ||
'application') // TODO remove the last condition once the support for applications has been implemented | ||
.map((entry) => Message.fromJson( | ||
entry, | ||
channelId: channelId, | ||
jsonify: true, | ||
transform: true, | ||
)) | ||
.toList(); | ||
} | ||
|
||
@override | ||
Future<void> multiInsert({required Iterable<Message> data}) { | ||
// TODO: implement multiInsert | ||
throw UnimplementedError(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you explain your idea?