AccountRepository and AccountViewModel for accounts
This MR introduces a thread-safe account list in AccountRepository.kt
that adheres to the Model-View-Presenter (MVP) pattern. The AccountViewModel
is responsible for presenting account details. These details are displayed through a Compose view. For dependency injection across platforms, we use Koin. Koin is a pragmatic Kotlin and Kotlin Multiplatform Dependency Injection framework. With Koin, we inject an instance of AccountViewModel
. This ensures that platform-specific integrations are appropriately respected.
The primary objectives of this implementation are:
-
Model: Centralized data access within
AccountRepository
. -
Presenter: The
AccountViewModel(repository: AccountRepository)
handles data and state management. It utilizes encapsulated observables (StateFlow) to update data and drive UI changes. -
View: Compose Multiplatform is utilized to observe the
AccnountViewModel
, offering a reactive UI experience. As the representation layer in the MVP pattern, Compose ensures the UI is always in sync with the underlying data state.
Tested on:
-
Android -
JVM (Mac m1) -
iOS
Reference implementation: KaMPKit #305
- Add support for Compose multiplatform.
Account recreation
This code in AccountRepository
on the mp_viewmodel_repository
branch:
Is copied from WallyApp
on the mp
branch
Because the Account recreation code in WallyApp
:
- depends on the Android app lifecycle, specifically
fun onCreate()
- Is in an Android-only module ( Unreachable for iOS)
In the highlighted case of Account recreation, initially, the code was encapsulated within the WallyApp
on the mp
branch, heavily reliant on the Android app lifecycle, specifically the fun onCreate()
method. This setup was housed in an Android-only module, rendering it unreachable for iOS and JVM, thus posing a limitation for a multiplatform approach.
Transitioning towards a Kotlin Multiplatform (KMP) project setup, there's an architectural shift witnessed with the introduction of AccountViewModel/AccountRepository
in the mp_viewmodel_repository
branch. This new setup embarks on a journey towards a more platform-agnostic architecture, aiding in the systematic porting of Account
and Wally
modules to a multiplatform paradigm, making the codebase more organized and less dependent on platform-specific lifecycles.
Koin
Koin, with its dependency injection prowess, can significantly streamline this transition, offering the following benefits:
-
Decoupling Dependencies:
- Koin aids in detaching the dependencies within
AccountRepository
and other related components from the Android's lifecycle. This decoupling promotes modularity and facilitates a smoother transition in porting the code across platforms like iOS, making the codebase more adaptable and manageable.
- Koin aids in detaching the dependencies within
-
Injecting Platform-Specific Implementations:
- Koin’s capability to provide the right implementations of dependencies for each platform is a boon in addressing the platform-specific challenges faced during Account recreation. This feature is instrumental in supplying the necessary dependencies accurately irrespective of the platform, thus aiding in the envisioned long-term solution for porting
Account
andWally
to a multiplatform architecture.
- Koin’s capability to provide the right implementations of dependencies for each platform is a boon in addressing the platform-specific challenges faced during Account recreation. This feature is instrumental in supplying the necessary dependencies accurately irrespective of the platform, thus aiding in the envisioned long-term solution for porting
-
Ease of Management:
- Dependency management and configuration are simplified with Koin. It offers a straightforward way to declare and manage dependencies, supporting a systematic and organized transition towards a multiplatform architecture that is less reliant on platform-specific lifecycles. This ease of management is conducive for developers to focus more on the logic and less on the boilerplate code related to dependency injection.
In essence, Koin acts as a catalyst in migrating towards a robust architectural setup conducive for a multiplatform approach. It addresses the core challenges posed by platform-specific dependencies, aiding in creating a more structured and less error-prone environment for the Account
module as it transitions towards a multiplatform realm.
Integrating a dependency injection framework like Koin in a Kotlin Multiplatform project can significantly streamline the process of managing dependencies across different platforms. As projects grow in complexity, having a reliable way to handle dependencies becomes crucial to ensure code maintainability, testability, and scalability. Koin, being a pure Kotlin library, blends seamlessly with Kotlin Multiplatform projects, providing a simple and efficient way to implement dependency injection. The table below encapsulates the pros and cons associated with introducing Koin to such projects, offering insights into what developers might expect in terms of benefits and challenges. Each point is substantiated with references to source materials for a more in-depth understanding
Pros | Cons |
---|---|
Simplicity Koin provides a straightforward and simple way to implement dependency injection, crucial for managing dependencies in a project. |
Initial Limited Options Before Koin, the options for adding dependency injection to Kotlin Multiplatform projects were limited. |
Kotlin-native Being a pure Kotlin library, Koin is easy to integrate into Kotlin projects and is designed to work seamlessly with Kotlin code. |
Learning Curve There might be a learning curve for developers new to Koin or dependency injection frameworks in general. |
Platform-Specific Support Koin aids in implementing different classes for each platform in a Kotlin Multiplatform project, necessary for addressing platform-specific requirements. |
Technical Challenges There may be compilation issues or other technical challenges while integrating Koin in Kotlin Multiplatform projects. |
Multiplatform Support Koin has shown support for Kotlin Multiplatform projects, allowing for dependency injection in projects that target multiple platforms. |
Additional Dependency Adding an external library like Koin introduces an additional dependency to the project, potentially increasing the project's complexity and maintenance requirements. |
Community Support There's a growing community and ecosystem around Koin, especially with the release of Koin Multiplatform version, indicative of ongoing support and development. |
Performance Overhead Dependency injection frameworks may introduce some performance overhead, although this might be negligible in most cases. |
Documentation Availability of examples and documentation to guide developers on how to use Koin in Kotlin Multiplatform projects. |
Alternatives Availability Other frameworks like Dagger or Hilt might offer different features or methodologies for handling dependency injection, possibly providing more suitable or feature-rich options for certain projects. |