Skip to content

Commit

Permalink
feat: notion parser (#557)
Browse files Browse the repository at this point in the history
* 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
dawnfire05 authored Feb 10, 2025
1 parent 341cb2f commit fa9b22e
Show file tree
Hide file tree
Showing 5 changed files with 641 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ abstract class NotionApi {
@factoryMethod
factory NotionApi(GroupsDio dio) = _NotionApi;

// @GET('{pageId}')
// Future<Map<String, dynamic>> getGroups(@Path('pageId') int pageId);
@GET('{pageId}')
Future<String> getGroups(@Path('pageId') String pageId);
}
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);
}
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 lib/app/modules/groups/presentation/blocs/notion_bloc.dart
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;
}
Loading

0 comments on commit fa9b22e

Please sign in to comment.