Skip to content

Commit

Permalink
doc update
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Nov 19, 2021
1 parent 5c7dae5 commit 002f509
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 74 deletions.
2 changes: 1 addition & 1 deletion docs/reference/koin-android/dsl-update.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Android DSL - 3.1.x Update
title: Android Reflection DSL
---

## Compact Definition
Expand Down
126 changes: 62 additions & 64 deletions docs/reference/koin-android/viewmodel.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: Easy injection into ViewModel
title: Injecting Android ViewModel
---

The `koin-android-viewmodel` introduces a new `viewModel` DSL keyword that comes in complement of `single` and `factory`, to help declare a ViewModel
The `koin-android` Gradle module introduces a new `viewModel` DSL keyword that comes in complement of `single` and `factory`, to help declare a ViewModel
component and bind it to an Android Component lifecycle.

```kotlin
Expand Down Expand Up @@ -38,38 +38,6 @@ class DetailActivity : AppCompatActivity() {
}
```


In a similar way, in Java you can inject the ViewModel instance, but using `viewModel()` or `getViewModel` static functions from `ViewModelCompat`:

```java
// import viewModel
import static org.koin.android.viewmodel.compat.ViewModelCompat.viewModel;

// import getViewModel
import static org.koin.android.viewmodel.compat.ViewModelCompat.getViewModel;

public class JavaActivity extends AppCompatActivity {

// lazy ViewModel
private final Lazy<DetailViewModel> viewModelLazy = viewModel(this, DetailViewModel.class);

// directly get the ViewModel instance
private final DetailViewModel viewModel = getViewModel(this, DetailViewModel.class);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);

//...
}
}
```

:::note
ViewModel API is accessible from Koin & Scope instances. But also from [`ViewModelStoreOwner` class](https://github.com/InsertKoinIO/koin/tree/master/koin-projects/koin-androidx-viewmodel/src/main/java/org/koin/androidx/viewmodel/ext/android)
:::

## Shared ViewModel

One ViewModel instance can be shared between Fragments and their host Activity.
Expand Down Expand Up @@ -119,30 +87,7 @@ class WeatherListFragment : Fragment() {
}
```

:::info
The Activity sharing its ViewModel injects it with `by viewModel()` or `getViewModel()`. Fragments are reusing the shared ViewModel with `by sharedViewModel()`.
:::

For your Java Fragment, must be used `sharedViewModel` or `getSharedViewModel` from `SharedViewModelCompat`.

```java
// import sharedViewModel static function
import static org.koin.android.viewmodel.compat.SharedViewModelCompat.sharedViewModel;

public class JavaFragment extends Fragment {

private final Lazy<WeatherViewModel> viewModel = sharedViewModel(this, WeatherViewModel.class);

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//...
}
}
```


## ViewModel and injection parameters
## Passing Parameters to Constructor

the `viewModel` keyword and injection API is compatible with injection parameters.

Expand All @@ -156,9 +101,7 @@ val appModule = module {
}
```

or even

In the module:
or even In the module:

```kotlin
val appModule = module {
Expand All @@ -181,7 +124,7 @@ class DetailActivity : AppCompatActivity() {
}
```

## ViewModel and State Bundle (updated in 3.1.3)
## State Bundle Injection (3.1.3)

Add a new property typed `SavedStateHandle` to your constructor to handle your ViewModel state:

Expand Down Expand Up @@ -212,7 +155,7 @@ StateViewModel API is accessible from Koin & Scope instances.
:::


## Navigation Graph ViewModel (updated in 3.1.3)
## Navigation Graph ViewModel (3.1.3)

You can scope a ViewModel instance to your Navigation graph. Just retrieve with `by koinNavGraphViewModel()`. You just need your graph Id.

Expand All @@ -222,4 +165,59 @@ class NavFragment : Fragment() {
val mainViewModel: NavViewModel by koinNavGraphViewModel(R.id.my_graph)

}
```
```

## ViewModel API - Java Compat

In a similar way, in Java you can inject the ViewModel instance, but using `viewModel()` or `getViewModel` static functions from `ViewModelCompat`:

```java
// import viewModel
import static org.koin.android.viewmodel.compat.ViewModelCompat.viewModel;

// import getViewModel
import static org.koin.android.viewmodel.compat.ViewModelCompat.getViewModel;

public class JavaActivity extends AppCompatActivity {

// lazy ViewModel
private final Lazy<DetailViewModel> viewModelLazy = viewModel(this, DetailViewModel.class);

// directly get the ViewModel instance
private final DetailViewModel viewModel = getViewModel(this, DetailViewModel.class);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);

//...
}
}
```

:::note
ViewModel API is accessible from Koin & Scope instances. But also from [`ViewModelStoreOwner` class](https://github.com/InsertKoinIO/koin/tree/master/koin-projects/koin-androidx-viewmodel/src/main/java/org/koin/androidx/viewmodel/ext/android)
:::

:::info
The Activity sharing its ViewModel injects it with `by viewModel()` or `getViewModel()`. Fragments are reusing the shared ViewModel with `by sharedViewModel()`.
:::

For your Java Fragment, must be used `sharedViewModel` or `getSharedViewModel` from `SharedViewModelCompat`.

```java
// import sharedViewModel static function
import static org.koin.android.viewmodel.compat.SharedViewModelCompat.sharedViewModel;

public class JavaFragment extends Fragment {

private final Lazy<WeatherViewModel> viewModel = sharedViewModel(this, WeatherViewModel.class);

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//...
}
}
```
2 changes: 1 addition & 1 deletion docs/reference/koin-core/dsl-update.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Koin DSL - 3.1.x update
title: Koin Reflection DSL
---

## Compact definition declaration - no more "get()"
Expand Down
19 changes: 11 additions & 8 deletions docs/setup/v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Here are the current versions of Koin:

```groovy
// Current version
koin_version = "3.1.3"
koin_version= "3.1.3"
```

:::info
Due to Jcenter shutdown, the koin project's maven group id was previously `org.koin` and is now `io.insert-koin`. Please check your configuration with modules below.
Expand All @@ -32,9 +33,9 @@ repositories {
#### **Kotlin**

```groovy
// Koin core features
// Koin Core features
implementation "io.insert-koin:koin-core:$koin_version"
// Koin test features
// Koin Rest features
testImplementation "io.insert-koin:koin-test:$koin_version"
```

Expand All @@ -50,15 +51,17 @@ testImplementation "io.insert-koin:koin-test-junit5:$koin_version"
#### **Android**

```groovy
// Koin main features for Android (Scope,ViewModel ...)
// Koin main features for Android
implementation "io.insert-koin:koin-android:$koin_version"
// Koin Java Compatibility
// No more koin-android-viewmodel, koin-android-scope, koin-android-fragment
// Java Compatibility
implementation "io.insert-koin:koin-android-compat:$koin_version"
// Koin for Jetpack WorkManager
// Jetpack WorkManager
implementation "io.insert-koin:koin-androidx-workmanager:$koin_version"
// Koin for Navigation Graph
// Navigation Graph
implementation "io.insert-koin:koin-androidx-navigation:$koin_version"
// Koin for Jetpack Compose
// Jetpack Compose
implementation "io.insert-koin:koin-androidx-compose:$koin_version"
```

Expand Down

0 comments on commit 002f509

Please sign in to comment.