Last active
September 24, 2021 14:17
-
-
Save AmineLAHRIM/daea0b15cb338c26f3861f0ae26dd8b7 to your computer and use it in GitHub Desktop.
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
typedef Future<NumberTrivia> _ConcreteOrRandomChooser(); | |
class NumberTriviaRepositoryImpl implements NumberTriviaRepository { | |
final NumberTriviaRemoteDataSource remoteDataSource; | |
final NumberTriviaLocalDataSource localDataSource; | |
final NetworkInfo networkInfo; | |
NumberTriviaRepositoryImpl({ | |
@required this.remoteDataSource, | |
@required this.localDataSource, | |
@required this.networkInfo, | |
}); | |
@override | |
Future<Either<Failure, NumberTrivia>> getConcreteNumberTrivia( | |
int number, | |
) async { | |
return await _getTrivia(() { | |
return remoteDataSource.getConcreteNumberTrivia(number); | |
}); | |
} | |
@override | |
Future<Either<Failure, NumberTrivia>> getRandomNumberTrivia() async { | |
return await _getTrivia(() { | |
return remoteDataSource.getRandomNumberTrivia(); | |
}); | |
} | |
Future<Either<Failure, NumberTrivia>> _getTrivia( | |
_ConcreteOrRandomChooser getConcreteOrRandom, | |
) async { | |
if (await networkInfo.isConnected) { | |
try { | |
final remoteTrivia = await getConcreteOrRandom(); | |
localDataSource.cacheNumberTrivia(remoteTrivia); | |
return Right(remoteTrivia); | |
} on ServerException { | |
return Left(ServerFailure()); | |
} | |
} else { | |
try { | |
final localTrivia = await localDataSource.getLastNumberTrivia(); | |
return Right(localTrivia); | |
} on CacheException { | |
return Left(CacheFailure()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. hope you doing good.
i would really appreciate if you could elaborate the exact working of _ConcreteOrRandomChooser to me its just a method parameter and is declared at to of the class. hope i made my question clear.
Thanks