Skip to content

Instantly share code, notes, and snippets.

Avatar
💻
Refactoring something somewhere

Pavlos-Petros Tournaris pavlospt

💻
Refactoring something somewhere
View GitHub Profile
View ResultUseCase.kt
abstract class ResultUseCase<Type, in Params> where Type : Any {
protected abstract val workDispatcher: CoroutineDispatcher
abstract suspend fun run(params: Params): Result<Type>
suspend operator fun invoke(params: Params): Result<Type> = withContext(workDispatcher) {
run(params)
}
}
View NoResultUseCase.kt
abstract class NoResultUseCase<in Params> {
protected abstract val workDispatcher: CoroutineDispatcher
abstract suspend fun run(params: Params)
suspend operator fun invoke(params: Params) = withContext(workDispatcher) {
run(params)
}
}
@pavlospt
pavlospt / FlowUseCase.kt
Last active May 3, 2020
FlowUseCase.kt
View FlowUseCase.kt
interface ObservableUseCase<Type> {
val dispatcher: CoroutineDispatcher
fun observe(): Flow<Type>
}
@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class)
abstract class FlowUseCase<Type : Any, Params : Any> : ObservableUseCase<Type> {
private val channel = ConflatedBroadcastChannel<Params>()
operator fun invoke(params: Params) = channel.sendBlocking(params)
View OurViewModel.kt
class OurViewModel(observeGithubReposUseCase: ObserveGithubReposUseCase): ViewModel {
val githubRepos: LiveData<List<GithubRepoItem>>
get() = observeGithubReposUseCase
.observe()
.map { reposList ->
reposList
.sortedByDescending { it.stars }
.map {
View OurViewModel.kt
class OurViewModel: ViewModel() {
private val _intentChannel = ConflatedBroadcastChannel<OurViewIntent>()
init {
_intentChannel
.asFlow()
.onEach { viewIntent ->
when (viewIntent) {
OurViewIntent.Refresh -> refreshData()
}
View viewModelDagger.kt
@Suppress("UNCHECKED_CAST")
class ViewModelFactory @Inject constructor(
private val creators: Map<Class<out ViewModel>, Provider<ViewModel>>
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
val creator = creators[modelClass]
?: creators.asIterable().find { (key, _) -> modelClass.isAssignableFrom(key) }?.value
?: throw IllegalArgumentException("Unknown ViewModel class $modelClass")
View usecaseModule.kt
import org.koin.dsl.module
val usecaseModule = module {
factory {
RefreshGithubReposUseCase(
appCoroutineDispatchers = get(),
githubRemoteRepo = get(),
githubLocalRepo = get()
)
}
View Dockerfile
FROM balenalib/raspberrypi3-openjdk:8-jdk
ENV APPLICATION_USER ktor
RUN useradd -ms /bin/bash $APPLICATION_USER
RUN mkdir /app
RUN chown -R $APPLICATION_USER /app
USER $APPLICATION_USER
View example.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/a_container_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="52dp"
android:layout_marginEnd="16dp"
android:maxWidth="296dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
View ViewBindingDiffUtilCallback.kt
abstract class ViewBindingDiffUtilCallback<Item : ViewBindingAdapterItem> :
DiffUtil.ItemCallback<Item>()
You can’t perform that action at this time.