Skip to content

Commit 5970dbb

Browse files
committed
added formal docs to docusaurus and cleaned up
1 parent 00bbd32 commit 5970dbb

25 files changed

+238
-662
lines changed

docs/blog/2019-05-28-first-blog-post.md

-12
This file was deleted.

docs/blog/2019-05-29-long-blog-post.md

-44
This file was deleted.

docs/blog/2021-08-01-mdx-blog-post.mdx

-20
This file was deleted.
Binary file not shown.

docs/blog/2021-08-26-welcome/index.md

-25
This file was deleted.

docs/blog/authors.yml

-17
This file was deleted.

docs/docs/core_concepts.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
title: 2. Core Concepts
3+
sidebar_position: 2
4+
---
5+
6+
# Core Concepts
7+
8+
The **Model-View-Intent (MVI)** pattern in adidas MVI follows these principles:
9+
10+
## Model
11+
12+
The **Model** represents the state of the application. It’s immutable and stores all the information required to render the user interface.
13+
14+
```kotlin
15+
sealed class LoginState {
16+
data class LoggedOut(val isLoggingIn: Boolean) : LoginState()
17+
data class LoggedIn(val username: String) : LoginState()
18+
}
19+
```
20+
21+
## View
22+
23+
The **View** observes the model and updates itself accordingly. It reacts to changes in the **Model** and displays the current state.
24+
25+
## Intent
26+
27+
An **Intent** represents a user action or an event that leads to a state change in the **Model**.
28+
29+
```kotlin
30+
sealed class LoginIntent {
31+
data class Login(val username: String, val password: String) : LoginIntent()
32+
object Logout : LoginIntent()
33+
}
34+
```
35+
36+
## Side Effects
37+
38+
A **SideEffect** represents an external action triggered by a state change, such as network calls, database updates, or other asynchronous operations.
39+
40+
```kotlin
41+
sealed class LoginSideEffect {
42+
object ShowInvalidCredentialsError : LoginSideEffect()
43+
object Close : LoginSideEffect()
44+
}
45+
```
46+
47+
## State
48+
49+
The **State** combines the **ViewState** and **SideEffect** components to provide a unified application state. It helps manage complex states within the app.

docs/docs/examples.md

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: 4. Examples
3+
sidebar_position: 4
4+
---
5+
6+
# Examples
7+
8+
This section provides examples of how to implement the adidas MVI library in your Android application.
9+
For more detail check the sample provided.
10+
11+
## 1. Login Flow Example
12+
13+
Here is a complete example of how to implement a login flow using the adidas MVI library.
14+
15+
### ViewModel Implementation
16+
17+
The following code shows the `LoginViewModel`, which handles the login process.
18+
19+
```kotlin
20+
class LoginViewModel(
21+
logger: Logger,
22+
coroutineDispatcher: CoroutineDispatcher = Dispatchers.Default
23+
) : ViewModel(), MviHost<LoginIntent, State<LoginState, LoginSideEffect>> {
24+
25+
private val reducer = Reducer(
26+
coroutineScope = viewModelScope,
27+
defaultDispatcher = coroutineDispatcher,
28+
initialInnerState = LoginState.LoggedOut(isLoggingIn = false),
29+
logger = logger,
30+
intentExecutor = this::executeIntent,
31+
)
32+
33+
override val state = reducer.state
34+
35+
override fun execute(intent: LoginIntent) {
36+
reducer.executeIntent(intent)
37+
}
38+
39+
private fun executeIntent(intent: LoginIntent) =
40+
when (intent) {
41+
is LoginIntent.Login -> executeLogin(intent)
42+
LoginIntent.Logout -> executeLogout()
43+
LoginIntent.Close -> executeClose()
44+
}
45+
46+
private fun executeLogin(intent: LoginIntent.Login) = flow {
47+
emit(LoginTransform.SetIsLoggingIn(isLoggingIn = true))
48+
delay(300) // Simulate a network call
49+
emit(LoginTransform.SetIsLoggingIn(isLoggingIn = false))
50+
51+
if (intent.username.isEmpty() || intent.password.isEmpty()) {
52+
emit(LoginTransform.AddSideEffect(LoginSideEffect.ShowInvalidCredentialsError))
53+
} else {
54+
emit(LoginTransform.SetLoggedIn(intent.username))
55+
}
56+
}
57+
}
58+
```
59+
60+
# Intent Example
61+
62+
Define the intents that your application will handle:
63+
64+
```kotlin
65+
internal sealed class LoginIntent : Intent {
66+
data class Login(val username: String, val password: String) : LoginIntent()
67+
object Logout : LoginIntent()
68+
object Close : LoginIntent()
69+
}
70+
```
71+
72+
# Activity Implementation
73+
74+
Here's how to set up the `MviSampleActivity` to use the `LoginViewModel`.
75+
76+
```kotlin
77+
class MviSampleActivity : AppCompatActivity() {
78+
private val viewModel: LoginViewModel by viewModel() // Assuming you're using Koin for dependency injection
79+
80+
override fun onCreate(savedInstanceState: Bundle?) {
81+
super.onCreate(savedInstanceState)
82+
setContent {
83+
LoginScreen(viewModel)
84+
}
85+
}
86+
}
87+
```
88+
89+
# View Implementation
90+
91+
In your composable function, observe the state from the `LoginViewModel` and render the UI accordingly.
92+
93+
```kotlin
94+
@Composable
95+
fun LoginScreen(viewModel: LoginViewModel) {
96+
val state by viewModel.state.collectAsState()
97+
98+
when (state) {
99+
is LoginState.LoggedOut -> {
100+
// Show login UI
101+
}
102+
is LoginState.LoggedIn -> {
103+
// Show logged-in UI
104+
}
105+
}
106+
}
107+
```

docs/docs/getting_started.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: 3. Getting started
3+
sidebar_position: 3
4+
---
5+
6+
# Getting Started
7+
8+
To begin using adidas MVI in your project, follow these steps:
9+
10+
## 1. Add the Dependency
11+
12+
Add the following to your `build.gradle` file:
13+
14+
```gradle
15+
implementation("com.adidas.mvi:mvi:{LATEST_VERSION}")
16+
```
17+
18+
Sync your project to download the necessary dependencies.
19+
20+
## 2. Create the Components
21+
22+
Here's an example of setting up the **LoginViewModel** and managing intents.
23+
24+
### Login ViewModel
25+
26+
The **LoginViewModel** manages user actions and updates the application state based on intents.
27+
28+
```kotlin
29+
class LoginViewModel( logger: Logger ) : ViewModel(), MviHost<LoginIntent, State<LoginState, LoginSideEffect>> {
30+
private val reducer = Reducer(
31+
coroutineScope = viewModelScope,
32+
initialInnerState = LoginState.LoggedOut(isLoggingIn = false),
33+
logger = logger,
34+
intentExecutor = this::executeIntent
35+
)
36+
37+
override val state = reducer.state
38+
39+
override fun execute(intent: LoginIntent) {
40+
reducer.executeIntent(intent)
41+
}
42+
43+
private fun executeIntent(intent: LoginIntent) =
44+
when (intent) {
45+
is LoginIntent.Login -> executeLogin(intent)
46+
LoginIntent.Logout -> executeLogout()
47+
LoginIntent.Close -> executeClose()
48+
}
49+
}
50+
```
51+
52+
## 3. Update the Activity
53+
54+
In your `Activity`, use the view model and set the content:
55+
56+
```kotlin
57+
class MviSampleActivity : AppCompatActivity() {
58+
override fun onCreate(savedInstanceState: Bundle?) {
59+
super.onCreate(savedInstanceState)
60+
setContent {
61+
LoginScreen()
62+
}
63+
}
64+
}
65+
```
66+
67+
Now you're ready to use adidas MVI in your project!

0 commit comments

Comments
 (0)