Skip to content

Commit

Permalink
feat: implement getScheduleById to fetch from local and remote data s…
Browse files Browse the repository at this point in the history
…ources
  • Loading branch information
jjoonleo committed Nov 5, 2024
1 parent a5c0708 commit 58de4f6
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions lib/data/repositories/schedule_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,38 @@ class ScheduleRepositoryImpl implements ScheduleRepository {
}

@override
Stream<ScheduleEntity> getScheduleById(int id) {
throw UnimplementedError();
Stream<ScheduleEntity> getScheduleById(int id) async* {
try {
final streamController = StreamController<ScheduleEntity>();
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
Expand Down

0 comments on commit 58de4f6

Please sign in to comment.