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

ViewBinding support out of box #76

Closed
Tracked by #8
RezMike opened this issue Nov 19, 2020 · 0 comments · Fixed by #92
Closed
Tracked by #8

ViewBinding support out of box #76

RezMike opened this issue Nov 19, 2020 · 0 comments · Fixed by #92
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@RezMike
Copy link

RezMike commented Nov 19, 2020

VBActivity:

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import dev.icerock.moko.mvvm.viewmodel.ViewModel

abstract class VBActivity<VB : Any, VM : ViewModel> : AppCompatActivity() {

    protected lateinit var viewModel: VM
    protected lateinit var binding: VB

    protected abstract val viewModelClass: Class<VM>

    protected abstract fun viewModelFactory(): ViewModelProvider.Factory
    protected abstract fun viewBinding(layoutInflater: LayoutInflater): VB
    protected abstract fun VB.root(): View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel = ViewModelProvider(this, viewModelFactory())[viewModelClass]
        binding = viewBinding(layoutInflater)

        setContentView(binding.root())
    }
}

VBEventsActivity:

import android.os.Bundle
import dev.icerock.moko.mvvm.dispatcher.EventsDispatcherOwner
import dev.icerock.moko.mvvm.viewmodel.ViewModel

abstract class VBEventsActivity<VB : Any, VM, Listener : Any> :
    VBActivity<VB, VM>() where VM: ViewModel, VM : EventsDispatcherOwner<Listener> {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        @Suppress("UNCHECKED_CAST")
        viewModel.eventsDispatcher.bind(this, this as Listener)
    }
}
@Alex009 Alex009 added the enhancement New feature or request label Nov 19, 2020
@Alex009 Alex009 added this to the 0.9.0 milestone Nov 19, 2020
@Alex009 Alex009 self-assigned this Jan 9, 2021
Alex009 added a commit that referenced this issue Jan 9, 2021
@Alex009 Alex009 linked a pull request Jan 10, 2021 that will close this issue
Alex009 added a commit that referenced this issue Jan 10, 2021
@Alex009 Alex009 closed this as completed Jan 10, 2021
@Alex009 Alex009 mentioned this issue Jan 10, 2021
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

Successfully merging a pull request may close this issue.

2 participants