-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: notion parser and notion page builder * refactor: delete unused files * refactor&fix: throw exception in rest notion repository * refactor: no async * refactor: remove annotation * feat: move notionParser * refactor: use method instead of for
- Loading branch information
1 parent
341cb2f
commit fa9b22e
Showing
5 changed files
with
641 additions
and
2 deletions.
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
46 changes: 46 additions & 0 deletions
46
lib/app/modules/groups/data/repositories/rest_notion_repository.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,46 @@ | ||
import 'dart:convert'; | ||
import 'package:injectable/injectable.dart'; | ||
import 'package:ziggle/app/modules/groups/data/data_sources/remote/notion_api.dart'; | ||
import 'package:ziggle/app/modules/groups/domain/repository/notion_repository.dart'; | ||
|
||
@Injectable(as: NotionRepository) | ||
class RestNotionRepository implements NotionRepository { | ||
final NotionApi _api; | ||
|
||
RestNotionRepository(this._api); | ||
|
||
@override | ||
Future<Map<String, dynamic>> getGroups(String pageId) async { | ||
try { | ||
final raw = await _api.getGroups(pageId); | ||
|
||
final Map<String, dynamic> parse = notionParser(raw); | ||
|
||
return parse; | ||
} on Exception catch (e) { | ||
throw Exception(e); | ||
} | ||
} | ||
} | ||
|
||
Map<String, dynamic> notionParser(String raw) { | ||
final Map<String, dynamic> originalJson = jsonDecode(raw); | ||
|
||
return originalJson.map((blockId, blockContainer) { | ||
final blockValue = blockContainer["value"]; | ||
if (blockValue == null) { | ||
return MapEntry(blockId, null); | ||
} | ||
|
||
return MapEntry(blockId, { | ||
"id": blockId, | ||
"type": blockValue["type"] ?? "", | ||
"properties": (blockValue["properties"] as Map<String, dynamic>?) ?? {}, | ||
"content": (blockValue["content"] as List?) ?? [], | ||
"format": (blockValue["format"] as Map<String, dynamic>?) ?? {}, | ||
"parent_id": blockValue["parent_id"], | ||
"last_edited_time": blockValue["last_edited_time"], | ||
}); | ||
}) | ||
..removeWhere((key, value) => value == null); | ||
} |
3 changes: 3 additions & 0 deletions
3
lib/app/modules/groups/domain/repository/notion_repository.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,3 @@ | ||
abstract class NotionRepository { | ||
Future<Map<String, dynamic>> getGroups(String pageId); | ||
} |
41 changes: 41 additions & 0 deletions
41
lib/app/modules/groups/presentation/blocs/notion_bloc.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,41 @@ | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:injectable/injectable.dart'; | ||
import 'package:ziggle/app/modules/groups/domain/repository/notion_repository.dart'; | ||
|
||
part 'notion_bloc.freezed.dart'; | ||
|
||
@injectable | ||
class NotionBloc extends Bloc<NotionEvent, NotionState> { | ||
final NotionRepository _repository; | ||
|
||
NotionBloc(this._repository) : super(const NotionState.initial()) { | ||
on<_Load>((event, emit) => _loadNotionPage(emit, event.pageId)); | ||
} | ||
|
||
Future<void> _loadNotionPage( | ||
Emitter<NotionState> emit, | ||
String pageId, | ||
) async { | ||
emit(const NotionState.loading()); | ||
try { | ||
final data = await _repository.getGroups(pageId); | ||
emit(NotionState.done(data)); | ||
} catch (e) { | ||
emit(NotionState.error(e.toString())); | ||
} | ||
} | ||
} | ||
|
||
@freezed | ||
class NotionEvent with _$NotionEvent { | ||
const factory NotionEvent.load({required String pageId}) = _Load; | ||
} | ||
|
||
@freezed | ||
class NotionState with _$NotionState { | ||
const factory NotionState.initial() = _Initial; | ||
const factory NotionState.loading() = _Loading; | ||
const factory NotionState.done(Map<String, dynamic> data) = _Done; | ||
const factory NotionState.error(String message) = _Error; | ||
} |
Oops, something went wrong.