Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. class MainActivity : AppCompatActivity() {
  2.  
  3. val disposable = CompositeDisposable()
  4.  
  5. override fun onCreate(savedInstanceState: Bundle?) {
  6. super.onCreate(savedInstanceState)
  7. setContentView(R.layout.activity_main)
  8. button.setOnClickListener {
  9. val f = TestPopup()
  10. usingRxJava(f)
  11. //usingLiveData(f)
  12. }
  13. }
  14.  
  15. private fun usingRxJava(f: TestPopup) {
  16. val subject = SingleSubject.create<String>()
  17. f.show(supportFragmentManager, "TAG")
  18. button.post {
  19. f.dialog.setOnDismissListener {
  20. val str = f.arguments?.getString(TestPopup.TEST_KEY) ?: ""
  21. subject.onSuccess(str)
  22. }
  23. }
  24. subject.subscribe({
  25. Toast.makeText(this, "Accept : $it", Toast.LENGTH_SHORT).show()
  26. }, {
  27.  
  28. }).addTo(disposable)
  29. }
  30.  
  31. private fun usingLiveData(f: TestPopup) {
  32. val liveData = MutableLiveData<String>()
  33. f.show(supportFragmentManager, "TAG")
  34. button.post {
  35. f.dialog.setOnDismissListener {
  36. val str = f.arguments?.getString(TestPopup.TEST_KEY) ?: ""
  37. liveData.postValue(str)
  38. }
  39. }
  40. liveData.observe(this, Observer {
  41. Toast.makeText(this, "Accept : $it", Toast.LENGTH_SHORT).show()
  42. })
  43. }
  44.  
  45. override fun onDestroy() {
  46. disposable.dispose()
  47. super.onDestroy()
  48. }
  49. }
  50.  
  51. class TestPopup : DialogFragment() {
  52.  
  53. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  54. return inflater.inflate(R.layout.dialog_test, container, false)
  55. }
  56.  
  57. override fun onActivityCreated(savedInstanceState: Bundle?) {
  58. super.onActivityCreated(savedInstanceState)
  59. button_test.setOnClickListener {
  60. val arg = Bundle()
  61. arg.putString(TEST_KEY, edit_test.text.toString())
  62. arguments = arg
  63. dismiss()
  64. }
  65. }
  66.  
  67. companion object {
  68. const val TEST_KEY = "KEY"
  69. }
  70. }
  71.  
  72. override fun onActivityCreated(savedInstanceState: Bundle?) {
  73. super.onActivityCreated(savedInstanceState)
  74. button_test.setOnClickListener {
  75. val input = edit_test.text.toString()
  76. (activity as MyListener).inputComplete(input)
  77. dismiss()
  78. }
  79. }
  80.  
  81. class MainActivity : AppCompatActivity(), TestPopup.MyListener {
  82.  
  83. override fun inputComplete(input: String) {
  84. Toast.makeText(this, "Accept : $input", Toast.LENGTH_SHORT).show()
  85. }
  86. }
  87.  
  88. //MainViewModel.kt
  89. class MainViewModel: ViewModel() {
  90.  
  91. val dialogText = PublishProcessor.create<String>()
  92.  
  93. fun postNewDialogText(text: String) {
  94. dialogText.onNext(text)
  95. }
  96. }
  97.  
  98. // Activity
  99. val disposable = CompositeDisposable()
  100.  
  101. override fun onCreate(savedInstanceState: Bundle?) {
  102. super.onCreate(savedInstanceState)
  103. setContentView(R.layout.activity_main)
  104.  
  105. val viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
  106.  
  107. viewModel.dialogText.subscribe {
  108. Toast.makeText(this, "Accept : $it", Toast.LENGTH_SHORT).show()
  109. }.addTo(disposable)
  110.  
  111. button.setOnClickListener {
  112. TestPopup().show(supportFragmentManager, "TAG")
  113. // usingRxJava(f)
  114. // usingLiveData(f)
  115. }
  116. }
  117.  
  118. override fun onDestroy() {
  119. disposable.dispose()
  120. super.onDestroy()
  121. }
  122.  
  123. // Dialog Fragment
  124. override fun onActivityCreated(savedInstanceState: Bundle?) {
  125. super.onActivityCreated(savedInstanceState)
  126.  
  127. // Important!! use activity when getting the viewmodel.
  128. val viewModel = ViewModelProviders.of(requireActivity()).get(MainViewModel::class.java)
  129.  
  130. button_test.setOnClickListener {
  131. viewModel.postNewDialogText(edit_test.text.toString())
  132. dismiss()
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement