Skip to content

Instantly share code, notes, and snippets.

@AmineLAHRIM
Last active September 24, 2021 14:17
Show Gist options
  • Save AmineLAHRIM/daea0b15cb338c26f3861f0ae26dd8b7 to your computer and use it in GitHub Desktop.
Save AmineLAHRIM/daea0b15cb338c26f3861f0ae26dd8b7 to your computer and use it in GitHub Desktop.
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());
}
}
}
}
@atif-afridi
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment