mizo0203/nature-remo-sample-android-kotlin

View on GitHub
app/src/main/java/com/mizo0203/natureremoapisample/ViewModelFactory.kt

Summary

Maintainability
A
0 mins
Test Coverage
/*
 *  Copyright 2017 Google Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.mizo0203.natureremoapisample

import android.annotation.SuppressLint
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.mizo0203.natureremoapisample.data.source.NatureRemoDataSource

/**
 * A creator is used to inject the product ID into the ViewModel
 *
 *
 * This creator is to showcase how to inject dependencies into ViewModels. It's not
 * actually necessary in this case, as the product ID can be passed in a public method.
 */
class ViewModelFactory private constructor(
        private val natureRemoDataSource: NatureRemoDataSource
) : ViewModelProvider.NewInstanceFactory() {

    override fun <T : ViewModel> create(modelClass: Class<T>) =
            with(modelClass) {
                when {
                    isAssignableFrom(MainViewModel::class.java) ->
                        MainViewModel(natureRemoDataSource)
                    else ->
                        throw IllegalArgumentException("Unknown ViewModel class: ${modelClass.name}")
                }
            } as T

    companion object {

        @SuppressLint("StaticFieldLeak")
        @Volatile private var INSTANCE: ViewModelFactory? = null

        fun getInstance() =
                INSTANCE
                        ?: synchronized(ViewModelFactory::class.java) {
                            INSTANCE
                                    ?: ViewModelFactory(
                                            Injection.provideTasksRepository())
                            .also { INSTANCE = it }
                }


        @Suppress("unused")
        @VisibleForTesting fun destroyInstance() {
            INSTANCE = null
        }
    }
}