Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Intent
- import android.os.Bundle
- import android.view.View
- import android.widget.Toast
- import androidx.appcompat.app.AppCompatActivity
- import androidx.appcompat.app.AppCompatDialogFragment
- import androidx.fragment.app.Fragment
- import androidx.fragment.app.FragmentManager
- import androidx.lifecycle.ViewModel
- import androidx.lifecycle.ViewModelProvider
- import androidx.lifecycle.ViewModelProviders
- /**
- * @author Dhruvaraj Nagarajan
- */
- // viewmodel
- inline fun <reified T : ViewModel> AppCompatActivity.getViewModel(viewModelFactory: ViewModelProvider.Factory? = null): T =
- ViewModelProviders.of(this, viewModelFactory)[T::class.java]
- inline fun <reified T : ViewModel> Fragment.getActivityViewModel(viewModelFactory: ViewModelProvider.Factory? = null): T =
- ViewModelProviders.of(activity!!, viewModelFactory)[T::class.java]
- inline fun <reified T : ViewModel> Fragment.getFragmentViewModel(viewModelFactory: ViewModelProvider.Factory? = null): T =
- ViewModelProviders.of(this, viewModelFactory)[T::class.java]
- // activity navigation
- inline fun <reified T : AppCompatActivity> AppCompatActivity.startActivity(init: Intent.() -> Unit = {}) {
- val intent = Intent(this, T::class.java)
- intent.init()
- startActivity(intent)
- }
- inline fun <reified T : AppCompatActivity> AppCompatActivity.startActivityForResult(
- RESULT_CODE: Int,
- init: Intent.() -> Unit
- ) {
- val intent = Intent(this, T::class.java)
- intent.init()
- startActivityForResult(intent, RESULT_CODE)
- }
- inline fun <reified T : AppCompatActivity> Fragment.startActivity(init: Intent.() -> Unit = {}) {
- val intent = Intent(context, T::class.java)
- intent.init()
- startActivity(intent)
- }
- inline fun <reified T : AppCompatActivity> Fragment.startActivityForResult(
- RESULT_CODE: Int,
- init: Intent.() -> Unit
- ) {
- val intent = Intent(context, T::class.java)
- intent.init()
- startActivityForResult(intent, RESULT_CODE)
- }
- // fragment navigation
- fun AppCompatActivity.addFragmentToOrigin(layoutId: Int, fragment: Fragment) {
- addFragmentToPosition(layoutId, fragment, 0)
- }
- fun AppCompatActivity.addFragmentToTop(layoutId: Int, fragment: Fragment, init: Bundle.() -> Unit = {}) {
- initFragment(fragment, init)
- val fm = supportFragmentManager
- fm.beginTransaction().add(layoutId, fragment, fragment.tag).commit()
- }
- fun AppCompatActivity.addFragmentToPosition(
- layoutId: Int,
- fragment: Fragment,
- position: Int,
- init: Bundle.() -> Unit = {}
- ) {
- initFragment(fragment, init)
- val fm = supportFragmentManager
- clearBackStack(fm, position)
- fm.beginTransaction().add(layoutId, fragment, fragment.tag).commit()
- }
- fun AppCompatActivity.replaceFragmentAtOrigin(layoutId: Int, fragment: Fragment) {
- replaceFragmentAtPosition(layoutId, fragment, 0)
- }
- fun AppCompatActivity.replaceFragmentAtTop(layoutId: Int, fragment: Fragment, init: Bundle.() -> Unit = {}) {
- initFragment(fragment, init)
- val fm = supportFragmentManager
- fm.beginTransaction().add(layoutId, fragment, fragment.tag).commit()
- }
- fun AppCompatActivity.replaceFragmentAtPosition(
- layoutId: Int,
- fragment: Fragment,
- position: Int,
- init: Bundle.() -> Unit = {}
- ) {
- initFragment(fragment, init)
- val fm = supportFragmentManager
- clearBackStack(fm, position)
- fm.beginTransaction().replace(layoutId, fragment, fragment.tag).commit()
- }
- private fun initFragment(fragment: Fragment, init: Bundle.() -> Unit = {}) {
- if (init != {}) {
- val bundle = Bundle()
- bundle.init()
- fragment.arguments = bundle
- }
- }
- fun AppCompatActivity.showBottomSheet(
- fragment: AppCompatDialogFragment,
- init: Bundle.() -> Unit = {}
- ) {
- if (init != {}) {
- val bundle = Bundle()
- bundle.init()
- fragment.arguments = bundle
- }
- fragment.show(supportFragmentManager, fragment.tag)
- }
- fun Fragment.showBottomSheet(
- fragment: AppCompatDialogFragment,
- init: Bundle.() -> Unit = {}
- ) {
- if (init != {}) {
- val bundle = Bundle()
- bundle.init()
- fragment.arguments = bundle
- }
- fragment.show(childFragmentManager, fragment.tag)
- }
- private fun clearBackStack(fm: FragmentManager, lastPopPositionInclusive: Int) {
- val count = fm.backStackEntryCount
- if (lastPopPositionInclusive < count - 1)
- for (i in count - 1 downTo lastPopPositionInclusive) {
- fm.popBackStackImmediate()
- }
- }
- // view modifiers
- fun View.toggleVisibility(show: Boolean) {
- this.apply {
- if (show && visibility != View.VISIBLE) {
- visibility = View.VISIBLE
- } else if (!show && visibility != View.GONE) {
- visibility = View.GONE
- }
- }
- }
- fun View.show() = toggleVisibility(true)
- fun View.hide() = toggleVisibility(false)
- // prompts
- fun AppCompatActivity.showToast(message: String?) {
- Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
- }
- fun Fragment.showToast(message: String?) {
- Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement