Skip to content

Instantly share code, notes, and snippets.

@harmittaa
Last active November 20, 2019 19:25
Show Gist options
  • Save harmittaa/955b2a4c381c20a5ba26a108f9bf3173 to your computer and use it in GitHub Desktop.
Save harmittaa/955b2a4c381c20a5ba26a108f9bf3173 to your computer and use it in GitHub Desktop.
Koin 2.0 and Retrofit 2.6.0
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
}
}
@ksharma-xyz
Copy link

What class is FragmentViewBinding. I cannot import it.

@harmittaa
Copy link
Author

What class is FragmentViewBinding. I cannot import it.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment