Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality for providing values (DI) #12

Open
y9vad9 opened this issue Dec 19, 2022 · 0 comments
Open

Add functionality for providing values (DI) #12

y9vad9 opened this issue Dec 19, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@y9vad9
Copy link
Owner

y9vad9 commented Dec 19, 2022

Problem

I don't think it's necessary to add something to ViewScope<T> every time we need to use some common values. Also, there should be a way to provide instances as DI does.

Solution

I think in this case we should divide types of instances we provide. So, I propose next types of instances:

  • static – initializes on application init and never changes (for example, ApplicationInstance)
  • local – initializes in specific scope (for example, LocalContext that is unique for each view)
  • mutable – can be changed while program runs (for example, AccountInstance where account can change due to log in / log out)

Providing instances

To provide instance, we use provider function, for example:

class Account(val name: String)
val AccountInstance = instance<Account?> { error("Not yet initialized") }

internal fun ViewGroupScope<*>.AppView() {
    provider(AccountInstance provides Account("test")) {
        AccountView()
    }
}

Getting instances

To get instance, we just call corresponding instance in ViewScope<T>:

internal fun ViewGroupScope<*>.AccountView() {
    frameLayout(frameLayoutParams().maxSize()) {
        AccountInstance.state.constructOnEach { account ->
            if (account != null) {
                textView {
                    text("Hello, ${account.name}!")
                }
            } else {
                button(layoutParams().width(64.dp)) {
                    text("Sign in")
                    gravity(Gravity.CENTER)
                }
            }
        }
    }
}

It's important to have ViewScope<T>, because it stores instanceProvider: InstanceProvder.

@y9vad9 y9vad9 added the enhancement New feature or request label Dec 19, 2022
@y9vad9 y9vad9 added this to the 1.0.0-prototype1 milestone Dec 19, 2022
@y9vad9 y9vad9 self-assigned this Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant