-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathNavControllerSample.kt
90 lines (83 loc) · 3.08 KB
/
NavControllerSample.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package io.ak1.pixsample.samples
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.get
import io.ak1.pix.helpers.PixBus
import io.ak1.pix.helpers.PixEventCallback.Status.BACK_PRESSED
import io.ak1.pix.helpers.PixEventCallback.Status.SUCCESS
import io.ak1.pix.helpers.setupScreen
import io.ak1.pix.helpers.showStatusBar
import io.ak1.pix.utility.ARG_PARAM_PIX
import io.ak1.pixsample.R
import io.ak1.pixsample.commons.Adapter
import io.ak1.pixsample.custom.fragmentBody
import io.ak1.pixsample.databinding.ActivityNavControllerSampleBinding
import io.ak1.pixsample.options
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
/**
* Created By Akshay Sharma on 20,June,2021
* https://ak1.io
*/
class NavControllerSample : AppCompatActivity() {
private lateinit var navController: NavController
private lateinit var binding: ActivityNavControllerSampleBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityNavControllerSampleBinding.inflate(layoutInflater)
setContentView(binding.root)
supportActionBar?.hide()
setupScreen()
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController
PixBus.results {
if (it.status == SUCCESS) {
showStatusBar()
navController.navigateUp()
}
}
}
override fun onBackPressed() {
if (navController.currentDestination == navController.graph[R.id.CameraFragment]) {
PixBus.onBackPressedEvent()
} else {
super.onBackPressed()
}
}
}
class NavResultsFragment : Fragment() {
private val recyclerViewAdapter = Adapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
PixBus.results(coroutineScope = CoroutineScope(Dispatchers.Main)) {
when (it.status) {
SUCCESS -> {
recyclerViewAdapter.apply {
this.list.clear()
this.list.addAll(it.data)
notifyDataSetChanged()
}
}
BACK_PRESSED -> {
requireActivity().onBackPressed()
}
}
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View = fragmentBody(requireActivity(), recyclerViewAdapter) {
var bundle = bundleOf(ARG_PARAM_PIX to options)
findNavController().navigate(R.id.action_ResultsFragment_to_CameraFragment, bundle)
}
}