-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathMainActivity.kt
135 lines (118 loc) · 5.21 KB
/
MainActivity.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.trendyol.medusa
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SwitchCompat
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.trendyol.medusalib.navigator.MultipleStackNavigator
import com.trendyol.medusalib.navigator.Navigator
import com.trendyol.medusalib.navigator.NavigatorConfiguration
import com.trendyol.medusalib.navigator.transaction.NavigatorTransaction
class MainActivity : AppCompatActivity(), Navigator.NavigatorListener {
lateinit var navigation: BottomNavigationView
private val rootFragmentProvider: List<() -> Fragment> = listOf(
{ FragmentGenerator.generateNewFragment() },
{ FragmentGenerator.generateNewFragment() },
{ FragmentGenerator.generateNewFragment() }
)
private val newListOfFragments: List<() -> Fragment> = listOf(
{ FragmentGenerator.generateBrandNewFragments() },
{ FragmentGenerator.generateBrandNewFragments() },
{ FragmentGenerator.generateBrandNewFragments() }
)
val multipleStackNavigator: MultipleStackNavigator =
MultipleStackNavigator(
supportFragmentManager,
R.id.fragmentContainer,
rootFragmentProvider,
navigatorListener = this,
navigatorConfiguration = NavigatorConfiguration(1, true, NavigatorTransaction.SHOW_HIDE))
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
multipleStackNavigator.switchTab(0)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
multipleStackNavigator.switchTab(1)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_notifications -> {
multipleStackNavigator.switchTab(2)
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation = findViewById<View>(R.id.navigation) as BottomNavigationView
multipleStackNavigator.preloadFragment(
fragment = SamplePreloadFragment.newInstance(),
fragmentTag = SamplePreloadFragment.TAG
)
multipleStackNavigator.initialize(savedInstanceState)
val restartRootFragmentCheckBox = findViewById<View>(R.id.restartSwitch) as SwitchCompat
findViewById<Button>(R.id.resetCurrentTab).setOnClickListener {
multipleStackNavigator.resetCurrentTab(restartRootFragmentCheckBox.isChecked)
}
findViewById<Button>(R.id.resetXTab).setOnClickListener {
multipleStackNavigator.reset(1, restartRootFragmentCheckBox.isChecked)
}
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
findViewById<Button>(R.id.reset).setOnClickListener { multipleStackNavigator.reset() }
findViewById<Button>(R.id.resetWithNewSet).setOnClickListener {
multipleStackNavigator.resetWithFragmentProvider(newListOfFragments)
}
findViewById<Button>(R.id.startWithGroup).setOnClickListener {
multipleStackNavigator.start(FragmentGenerator.generateNewFragment(), "group1")
}
findViewById<Button>(R.id.resetGroup).setOnClickListener {
multipleStackNavigator.clearGroup("group1")
}
findViewById<Button>(R.id.startPreloadedFragment).setOnClickListener {
multipleStackNavigator.startPreloadedFragment(SamplePreloadFragment.newInstance(), SamplePreloadFragment.TAG)
}
multipleStackNavigator.observeDestinationChanges(this) {
Log.d("Destination Changed", "${it.javaClass.name} - ${it.tag}")
}
}
override fun onBackPressed() {
if (multipleStackNavigator.canGoBack()) {
multipleStackNavigator.goBack()
} else {
super.onBackPressed()
}
}
override fun onTabChanged(tabIndex: Int) {
when (tabIndex) {
0 -> navigation.selectedItemId = R.id.navigation_home
1 -> navigation.selectedItemId = R.id.navigation_dashboard
2 -> navigation.selectedItemId = R.id.navigation_notifications
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.navigation_java_sample -> {
startActivity(Intent(this, MainActivity2::class.java))
return true
}
}
return super.onOptionsItemSelected(item)
}
override fun onSaveInstanceState(outState: Bundle) {
multipleStackNavigator.onSaveInstanceState(outState)
super.onSaveInstanceState(outState)
}
}