Skip to content

Instantly share code, notes, and snippets.

View ifucolo's full-sized avatar
🌞
🇧🇷 ➡️ 🇳🇱

Iago Mendes Fucolo ifucolo

🌞
🇧🇷 ➡️ 🇳🇱
View GitHub Profile
fun example() {
//once your isGameAvailableListener being called it will return inside of isGameAvailableListener
ifgameavailable(gamecode = "CODE", isGameAvailableListener = { isGameAvailable ->
})
}
fun ifgameavailable(gamecode: String, isGameAvailableListener: (Boolean) -> Unit) {
isGameAvailableListener.invoke(true)
}
interface AndroidJobsRepository {
fun getJobs(): Flow<List<AndroidJob>>
suspend fun fetchFreshJobs()
fun add()
}
@JsonClass(generateAdapter = true)
data class AndroidJob(
@Json(name = "title") val title: String,
@Json(name = "required_experience_years") val experienceTimeRequired: String,
@Json(name = "native") val native: Boolean,
@Json(name = "country") val country: String
)
class AndroidJobsRepositoryImpl @Inject constructor(
@IoDispatcher
private val coroutineDispatcher: CoroutineDispatcher
@DefaultDispatcher
@Provides
fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default
@IoDispatcher
@Provides
fun providesIoDispatcher(): CoroutineDispatcher = Dispatchers.IO
@MainDispatcher
@Provides
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class IoDispatcher
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class MainDispatcher
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class DefaultDispatcher
@Module
@InstallIn(SingletonComponent::class)
object DispatcherModule
override suspend fun fetchFreshJobs() {
withContext(coroutineDispatcher) {
serverApi.fetchJobs()
.also { jobs ->
jobsDao.updateData(jobs.map { it.asCache() })
}
}
}
override fun getJobs(): Flow<List<AndroidJob>> {
return jobsDao.getJobs().map { jobs ->
jobs.map(AndroidJobCache::asExternalModel)
}.onEach {
if (it.isEmpty()) {
fetchFreshJobs()
}
}
}
@IoDispatcher
private val coroutineDispatcher: CoroutineDispatcher