Skip to content

Instantly share code, notes, and snippets.

@dmitriy-chernysh
Last active November 24, 2022 23:02
Show Gist options
  • Save dmitriy-chernysh/c036942c5954a78925f4acf56122bd0a to your computer and use it in GitHub Desktop.
Save dmitriy-chernysh/c036942c5954a78925f4acf56122bd0a to your computer and use it in GitHub Desktop.
File Templates. Android Studio. Kotlin. Fragment and ViewModel
/*
How to add this template to Android Studio:
1. Settings -> Edditor -> File and Code Templated;
2. Open "Files" tab;
3. Click icon "Plus" and paste the code below;
4. "Enable Live Templates" should be turned off.
*/
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import com.cdvdev.videoinventory.R
import com.mobiledevpro.commons.fragment.BaseFragment
import org.koin.androidx.scope.lifecycleScope
import org.koin.androidx.viewmodel.scope.viewModel
#parse("File Header.java")
//BaseFragment class is a part of java library https://github.com/mobile-dev-pro/commons-ui
//See BaseFragment class here
//https://github.com/mobile-dev-pro/commons/blob/8b65335e549ce160063f33d427e69b82c15ee909/src/main/java/com/mobiledevpro/commons/fragment/BaseFragment.java
class ${FRAGMENT_NAME}Fragment : BaseFragment() {
private val args: ${NAME}Args by navArgs()
private val viewModel${FRAGMENT_NAME}: ${FRAGMENT_NAME}ViewModel
by lifecycleScope.viewModel(this)
override fun getLayoutResId() = 0 //R.layout.fragment_..... Layout with data binding
override fun getAppBarTitle() = 0 //R.string.app_title_.....
override fun getHomeAsUpIndicatorIcon() = R.drawable.ic_arrow_back_white_24dp
override fun initPresenters() {
//add lifecycle observer to presenter
lifecycle.addObserver(viewModel${FRAGMENT_NAME})
}
override fun populateView(layoutView: View?, savedInstanceState: Bundle?): View {
//databinding
val binding = Fragment${FRAGMENT_NAME}Binding.bind(layoutView)
.apply {
viewModel = viewModel${FRAGMENT_NAME}
}
binding.lifecycleOwner = viewLifecycleOwner
observeLiveData()
return binding.root
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
var isGranted = false
for (result in grantResults) {
if (result == PackageManager.PERMISSION_GRANTED) {
isGranted = true
} else {
isGranted = false
break
}
}
when (requestCode) {
//check request codes here and do something
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
//check request codes here and do something
else -> super.onActivityResult(requestCode, resultCode, data)
}
}
private fun observeLiveData() {
//listen LiveData changes here
}
}
/*
How to add this template to Android Studio:
1. Settings -> Edditor -> File and Code Templated;
2. Open "Files" tab;
3. Click icon "Plus" and paste the code below;
4. "Enable Live Templates" should be turned off.
*/
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.cdvdev.videoinventory.common.BaseViewModel
#parse("File Header.java")
class ${NAME} : BaseViewModel(), LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStartView() {
//do something on start view if it's needed
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStopView() {
//do something on stop view if it's needed
}
}
//You should use RxJava2
abstract class BaseViewModel : ViewModel() {
private var disposable: CompositeDisposable = CompositeDisposable()
val subscriptions: CompositeDisposable
get() {
if (disposable.isDisposed) disposable = CompositeDisposable()
return disposable
}
fun clearSubscriptions() {
disposable.dispose()
}
override fun onCleared() {
disposable.dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment