Skip to content

Instantly share code, notes, and snippets.

View douglasiacovelli's full-sized avatar

Douglas Iacovelli douglasiacovelli

  • @contratadome
  • Bertioga
View GitHub Profile
interface Repository {
suspend fun getRedditPosts(): ResultWrapper<String>
}
class RepositoryImpl(private val service: RedditService,
private val dispatcher: CoroutineDispatcher = Dispatchers.IO) : Repository {
override suspend fun getRedditPosts(): ResultWrapper<RedditPosts> {
return safeApiCall(dispatcher) { service.getRedditPosts().toRedditPosts() }
}
fun fetchData() {
viewModelScope.launch {
val redditResponse = repository.getRedditPosts()
when (redditResponse) {
is NetworkError -> showNetworkError()
is GenericError-> showGenericError(redditResponse)
is Success -> showSuccess(redditResponse.value)
}
}
}
class NetworkHelperTest {
private val dispatcher = TestCoroutineDispatcher()
@Test
fun `when lambda returns successfully then it should emit the result as success`() {
runBlockingTest {
val lambdaResult = true
val result = safeApiCall(dispatcher) { lambdaResult }
assertEquals(ResultWrapper.Success(lambdaResult), result)
}
@douglasiacovelli
douglasiacovelli / AirbnbFlavorBuild.gradle
Created October 24, 2019 14:19
Airbnb snippet on how to add modules to app according to flavor
project.flavors.each { flavor, config ->
project.dependencies.add("${flavor}Compile", project(config.entryModule))
}
data class ErrorResponse(
val error_description: String, // this is the translated error shown to the user directly from the API
val causes: Map<String, String> = emptyMap() //this is for errors on specific field on a form
)
class Credentials {
final String givenName;
final String familyName;
Credentials(this.givenName, this.familyName);
}
void main() {
final credentials1 = Credentials('Doug', 'blabla');
@douglasiacovelli
douglasiacovelli / auth_interceptor.dart
Created January 5, 2021 11:40
This is a code for an auth interceptor for Dio, which uses a header to avoid retrying indefinitely to refresh the token.
/// This interceptor is simplified because is doesn't contemplate
/// a refresh token, which you should take care of.
/// I haven't tested this code, but I believe it should work.
/// If you have some feedback, please leave a comment and I'll review it :) Thanks.
class AuthInterceptor extends InterceptorsWrapper {
final Dio loggedDio;
final Dio tokenDio;
import 'dart:async';
void main() {
escutarDeepLinks().listen((e) {
print(e);
}, onError: (error) {
print('deu erro vei. $error');
});
@douglasiacovelli
douglasiacovelli / Matchfile
Created March 26, 2021 15:47
Matchfile sample
git_url("[email protected]:<yourcompany>/<repo>.git")
storage_mode("git")
type("development") # The default type, can be: appstore, adhoc, enterprise or development
# These keys must be filled in order to fetch the certificates
# Git basic authorization for match encoded in base64
# This is only needed if the user is not authenticated using ssh on git or if you're running on CI/CD server.
# MATCH_GIT_BASIC_AUTHORIZATION=
# Password used to encrypt/decrypt certificates
MATCH_PASSWORD=
# (Optional) Your local machine password, such as the one you use to login.