Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. import android.content.Intent
  2. import android.os.Bundle
  3. import android.view.View
  4. import android.widget.Toast
  5. import androidx.appcompat.app.AppCompatActivity
  6. import androidx.appcompat.app.AppCompatDialogFragment
  7. import androidx.fragment.app.Fragment
  8. import androidx.fragment.app.FragmentManager
  9. import androidx.lifecycle.ViewModel
  10. import androidx.lifecycle.ViewModelProvider
  11. import androidx.lifecycle.ViewModelProviders
  12.  
  13. /**
  14. * @author Dhruvaraj Nagarajan
  15. */
  16.  
  17. // viewmodel
  18. inline fun <reified T : ViewModel> AppCompatActivity.getViewModel(viewModelFactory: ViewModelProvider.Factory? = null): T =
  19. ViewModelProviders.of(this, viewModelFactory)[T::class.java]
  20.  
  21. inline fun <reified T : ViewModel> Fragment.getActivityViewModel(viewModelFactory: ViewModelProvider.Factory? = null): T =
  22. ViewModelProviders.of(activity!!, viewModelFactory)[T::class.java]
  23.  
  24. inline fun <reified T : ViewModel> Fragment.getFragmentViewModel(viewModelFactory: ViewModelProvider.Factory? = null): T =
  25. ViewModelProviders.of(this, viewModelFactory)[T::class.java]
  26.  
  27. // activity navigation
  28. inline fun <reified T : AppCompatActivity> AppCompatActivity.startActivity(init: Intent.() -> Unit = {}) {
  29. val intent = Intent(this, T::class.java)
  30. intent.init()
  31. startActivity(intent)
  32. }
  33.  
  34. inline fun <reified T : AppCompatActivity> AppCompatActivity.startActivityForResult(
  35. RESULT_CODE: Int,
  36. init: Intent.() -> Unit
  37. ) {
  38. val intent = Intent(this, T::class.java)
  39. intent.init()
  40. startActivityForResult(intent, RESULT_CODE)
  41. }
  42.  
  43. inline fun <reified T : AppCompatActivity> Fragment.startActivity(init: Intent.() -> Unit = {}) {
  44. val intent = Intent(context, T::class.java)
  45. intent.init()
  46. startActivity(intent)
  47. }
  48.  
  49. inline fun <reified T : AppCompatActivity> Fragment.startActivityForResult(
  50. RESULT_CODE: Int,
  51. init: Intent.() -> Unit
  52. ) {
  53. val intent = Intent(context, T::class.java)
  54. intent.init()
  55. startActivityForResult(intent, RESULT_CODE)
  56. }
  57.  
  58. // fragment navigation
  59. fun AppCompatActivity.addFragmentToOrigin(layoutId: Int, fragment: Fragment) {
  60. addFragmentToPosition(layoutId, fragment, 0)
  61. }
  62.  
  63. fun AppCompatActivity.addFragmentToTop(layoutId: Int, fragment: Fragment, init: Bundle.() -> Unit = {}) {
  64. initFragment(fragment, init)
  65. val fm = supportFragmentManager
  66. fm.beginTransaction().add(layoutId, fragment, fragment.tag).commit()
  67. }
  68.  
  69. fun AppCompatActivity.addFragmentToPosition(
  70. layoutId: Int,
  71. fragment: Fragment,
  72. position: Int,
  73. init: Bundle.() -> Unit = {}
  74. ) {
  75. initFragment(fragment, init)
  76. val fm = supportFragmentManager
  77. clearBackStack(fm, position)
  78. fm.beginTransaction().add(layoutId, fragment, fragment.tag).commit()
  79. }
  80.  
  81. fun AppCompatActivity.replaceFragmentAtOrigin(layoutId: Int, fragment: Fragment) {
  82. replaceFragmentAtPosition(layoutId, fragment, 0)
  83. }
  84.  
  85. fun AppCompatActivity.replaceFragmentAtTop(layoutId: Int, fragment: Fragment, init: Bundle.() -> Unit = {}) {
  86. initFragment(fragment, init)
  87. val fm = supportFragmentManager
  88. fm.beginTransaction().add(layoutId, fragment, fragment.tag).commit()
  89. }
  90.  
  91. fun AppCompatActivity.replaceFragmentAtPosition(
  92. layoutId: Int,
  93. fragment: Fragment,
  94. position: Int,
  95. init: Bundle.() -> Unit = {}
  96. ) {
  97. initFragment(fragment, init)
  98. val fm = supportFragmentManager
  99. clearBackStack(fm, position)
  100. fm.beginTransaction().replace(layoutId, fragment, fragment.tag).commit()
  101. }
  102.  
  103. private fun initFragment(fragment: Fragment, init: Bundle.() -> Unit = {}) {
  104. if (init != {}) {
  105. val bundle = Bundle()
  106. bundle.init()
  107. fragment.arguments = bundle
  108. }
  109. }
  110.  
  111. fun AppCompatActivity.showBottomSheet(
  112. fragment: AppCompatDialogFragment,
  113. init: Bundle.() -> Unit = {}
  114. ) {
  115. if (init != {}) {
  116. val bundle = Bundle()
  117. bundle.init()
  118. fragment.arguments = bundle
  119. }
  120. fragment.show(supportFragmentManager, fragment.tag)
  121. }
  122.  
  123. fun Fragment.showBottomSheet(
  124. fragment: AppCompatDialogFragment,
  125. init: Bundle.() -> Unit = {}
  126. ) {
  127. if (init != {}) {
  128. val bundle = Bundle()
  129. bundle.init()
  130. fragment.arguments = bundle
  131. }
  132. fragment.show(childFragmentManager, fragment.tag)
  133. }
  134.  
  135. private fun clearBackStack(fm: FragmentManager, lastPopPositionInclusive: Int) {
  136. val count = fm.backStackEntryCount
  137. if (lastPopPositionInclusive < count - 1)
  138. for (i in count - 1 downTo lastPopPositionInclusive) {
  139. fm.popBackStackImmediate()
  140. }
  141. }
  142.  
  143. // view modifiers
  144. fun View.toggleVisibility(show: Boolean) {
  145. this.apply {
  146. if (show && visibility != View.VISIBLE) {
  147. visibility = View.VISIBLE
  148. } else if (!show && visibility != View.GONE) {
  149. visibility = View.GONE
  150. }
  151. }
  152. }
  153.  
  154. fun View.show() = toggleVisibility(true)
  155.  
  156. fun View.hide() = toggleVisibility(false)
  157.  
  158. // prompts
  159. fun AppCompatActivity.showToast(message: String?) {
  160. Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
  161. }
  162.  
  163. fun Fragment.showToast(message: String?) {
  164. Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement