-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace state machine (#52)
* refactor: replace state machine * fix: contributing web url * fix: add missing ViewModel inheritance * fix: tests
- Loading branch information
1 parent
7190f38
commit 14b13a6
Showing
31 changed files
with
187 additions
and
925 deletions.
There are no files selected for viewing
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
3 changes: 3 additions & 0 deletions
3
domain/src/main/java/com/jcaique/dialetus/domain/regions/RegionsService.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package com.jcaique.dialetus.domain.regions | ||
|
||
import com.jcaique.dialetus.domain.errors.GatewayIntegrationIssues | ||
import com.jcaique.dialetus.domain.errors.UnexpectedResponse | ||
import com.jcaique.dialetus.domain.models.Region | ||
|
||
interface RegionsService { | ||
|
||
@Throws(UnexpectedResponse::class, GatewayIntegrationIssues::class) | ||
suspend fun fetchRegions(): List<Region> | ||
} |
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
6 changes: 3 additions & 3 deletions
6
...n/src/main/java/com/jcaique/dialetus/presentation/contributing/ContributingInteraction.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package com.jcaique.dialetus.presentation.contributing | ||
|
||
internal interface ContributingInteraction { | ||
object OpenAndroid : ContributingInteraction | ||
object OpenWeb : ContributingInteraction | ||
internal sealed class ContributingInteraction(val url: String) { | ||
object OpenAndroid : ContributingInteraction(ContributingConst.ANDROID) | ||
object OpenWeb : ContributingInteraction(ContributingConst.API) | ||
} |
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
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
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
74 changes: 21 additions & 53 deletions
74
presentation/src/main/java/com/jcaique/dialetus/presentation/dialects/DialectsViewModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,33 @@ | ||
package com.jcaique.dialetus.presentation.dialects | ||
|
||
import androidx.lifecycle.ViewModel | ||
import cafe.adriel.dalek.Dalek | ||
import cafe.adriel.dalek.DalekEvent | ||
import com.jcaique.dialetus.domain.dialects.DialectsService | ||
import com.jcaique.dialetus.domain.models.Dialect | ||
import com.jcaique.dialetus.utils.dataflow.StateMachine | ||
import com.jcaique.dialetus.utils.dataflow.StateTransition | ||
import com.jcaique.dialetus.utils.dataflow.UnsupportedUserInteraction | ||
import com.jcaique.dialetus.utils.dataflow.UserInteraction | ||
import com.jcaique.dialetus.utils.extensions.normalize | ||
import kotlinx.coroutines.coroutineScope | ||
import com.jcaique.dialetus.domain.models.Region | ||
import com.jcaique.dialetus.presentation.ktx.matches | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
internal class DialectsViewModel( | ||
private val machine: StateMachine<DialectsPresentation>, | ||
private val service: DialectsService | ||
) { | ||
) : ViewModel() { | ||
|
||
// TODO cache dialects elsewhere | ||
private var dialects = emptyList<Dialect>() | ||
|
||
fun bind() = machine.states() | ||
|
||
fun handle(interaction: UserInteraction) { | ||
interpret(interaction) | ||
.let(machine::consume) | ||
} | ||
|
||
private fun interpret(interaction: UserInteraction) = | ||
when (interaction) { | ||
is ShowDialects -> StateTransition( | ||
::showDialects, | ||
interaction | ||
) | ||
is FilterDialects -> StateTransition( | ||
::filterDialects, | ||
interaction | ||
) | ||
else -> throw UnsupportedUserInteraction | ||
|
||
fun getDialects(region: Region): Flow<DalekEvent<DialectsPresentation>> = | ||
Dalek { | ||
service | ||
.getDialectsBy(region.name.toLowerCase()) | ||
.also(::dialects::set) | ||
.let(::DialectsPresentation) | ||
} | ||
|
||
private suspend fun showDialects( | ||
parameters: StateTransition.Parameters | ||
): DialectsPresentation = coroutineScope { | ||
val interaction = parameters as ShowDialects | ||
|
||
service | ||
.getDialectsBy(interaction.region.name.toLowerCase()) | ||
.let(::DialectsPresentation) | ||
} | ||
|
||
private suspend fun filterDialects( | ||
parameters: StateTransition.Parameters | ||
): DialectsPresentation = coroutineScope { | ||
val interaction = parameters as FilterDialects | ||
|
||
dialects | ||
.filter { | ||
it.dialect | ||
.normalize() | ||
.contains( | ||
other = interaction.query.normalize(), | ||
ignoreCase = true | ||
) | ||
} | ||
.let(::DialectsPresentation) | ||
} | ||
fun filterDialects(query: String): Flow<DalekEvent<DialectsPresentation>> = | ||
Dalek { | ||
dialects | ||
.filter { it.matches(query) } | ||
.let(::DialectsPresentation) | ||
} | ||
} |
9 changes: 0 additions & 9 deletions
9
...entation/src/main/java/com/jcaique/dialetus/presentation/dialects/dialect_interactions.kt
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
presentation/src/main/java/com/jcaique/dialetus/presentation/ktx/dialect.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.jcaique.dialetus.presentation.ktx | ||
|
||
import com.jcaique.dialetus.domain.models.Dialect | ||
import com.jcaique.dialetus.utils.extensions.normalize | ||
|
||
internal fun Dialect.matches(query: String): Boolean = | ||
dialect | ||
.normalize() | ||
.contains(other = query.normalize(), ignoreCase = true) |
Oops, something went wrong.