Last active
November 20, 2019 19:25
-
-
Save harmittaa/955b2a4c381c20a5ba26a108f9bf3173 to your computer and use it in GitHub Desktop.
Koin 2.0 and Retrofit 2.6.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.annotation.SuppressLint | |
import android.os.Bundle | |
import android.util.Log | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.databinding.DataBindingUtil | |
import androidx.fragment.app.Fragment | |
import androidx.lifecycle.Observer | |
import com.github.harmittaa.koinexample.R | |
import com.github.harmittaa.koinexample.databinding.FragmentViewBinding | |
import com.github.harmittaa.koinexample.model.Weather | |
import org.koin.androidx.viewmodel.ext.android.viewModel | |
import org.koin.dsl.module | |
val fragmentModule = module { | |
factory { ExampleFragment() } | |
} | |
class ExampleFragment : Fragment() { | |
private val exampleViewModel: ExampleViewModel by viewModel() | |
private lateinit var binding: FragmentViewBinding | |
@SuppressLint("SetTextI18n") | |
private val observer = Observer<Weather> { | |
binding.fragmentInfo.text = "Temperature at ${it.name} is ${it.temp.temp} celcius" | |
} | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
super.onCreateView(inflater, container, savedInstanceState) | |
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_view, container, false) | |
exampleViewModel.weather.observe(this, observer) | |
return binding.root | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's automatically generated ViewBinding for the fragment. https://developer.android.com/topic/libraries/view-binding
You can ignore it and lines 22 & 31 & 32 & 26 if you wish and use instead kotlin synthetic or normal findViewById.