diff --git a/lib/data/repositories/schedule_repository_impl.dart b/lib/data/repositories/schedule_repository_impl.dart index dcb3419..b373ff1 100644 --- a/lib/data/repositories/schedule_repository_impl.dart +++ b/lib/data/repositories/schedule_repository_impl.dart @@ -23,8 +23,38 @@ class ScheduleRepositoryImpl implements ScheduleRepository { } @override - Stream getScheduleById(int id) { - throw UnimplementedError(); + Stream getScheduleById(int id) async* { + try { + final streamController = StreamController(); + final localScheduleEntity = scheduleLocalDataSource.getScheduleById(id); + final remoteScheduleEntity = scheduleRemoteDataSource.getScheduleById(id); + + bool isFirstResponse = true; + + localScheduleEntity.then((localScheduleEntity) { + if (isFirstResponse) { + isFirstResponse = false; + streamController.add(localScheduleEntity); + } + }); + + remoteScheduleEntity.then((remoteScheduleEntity) async { + if (isFirstResponse) { + isFirstResponse = false; + streamController.add(remoteScheduleEntity); + } else { + if (await localScheduleEntity != remoteScheduleEntity) { + streamController.add(remoteScheduleEntity); + await scheduleLocalDataSource.updateSchedule(remoteScheduleEntity); + } + } + }); + + await Future.wait([localScheduleEntity, remoteScheduleEntity]); + yield* streamController.stream; + } catch (e) { + rethrow; + } } @override