Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 45c6efa

Browse files
committed
implement initial IsolationFlagsFragment into SettingsTorFragment
1 parent ea1d440 commit 45c6efa

File tree

2 files changed

+155
-9
lines changed

2 files changed

+155
-9
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package io.matthewnelson.sampleapp.ui.fragments.settings.tor
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.widget.Button
8+
import android.widget.LinearLayout
9+
import androidx.activity.OnBackPressedCallback
10+
import androidx.constraintlayout.widget.ConstraintLayout
11+
import androidx.core.view.marginBottom
12+
import androidx.core.view.marginEnd
13+
import androidx.core.view.marginStart
14+
import androidx.core.view.marginTop
15+
import androidx.fragment.app.Fragment
16+
import io.matthewnelson.sampleapp.R
17+
import io.matthewnelson.topl_service.service.components.onionproxy.ServiceTorSettings
18+
19+
20+
class IsolationFlagsFragment(
21+
private val bottomMarginAdjust: Int,
22+
private val flagType: String,
23+
private val serviceTorSettings: ServiceTorSettings
24+
) : Fragment() {
25+
26+
companion object {
27+
const val SOCKS_FLAGS = "SOCKS_FLAGS"
28+
}
29+
30+
private val backPressHandler = BackPressHandler(true)
31+
32+
private lateinit var layoutTop: LinearLayout
33+
private lateinit var layoutBottom: LinearLayout
34+
private lateinit var layoutEnd: LinearLayout
35+
private lateinit var layoutStart: LinearLayout
36+
private lateinit var layoutConstraintInner: ConstraintLayout
37+
38+
override fun onCreateView(
39+
inflater: LayoutInflater,
40+
container: ViewGroup?,
41+
savedInstanceState: Bundle?
42+
): View? {
43+
activity?.apply {
44+
onBackPressedDispatcher.addCallback(viewLifecycleOwner, backPressHandler)
45+
}
46+
return inflater.inflate(R.layout.fragment_isolation_flags, container, false)
47+
}
48+
49+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
50+
super.onViewCreated(view, savedInstanceState)
51+
findViews(view)
52+
setMargins()
53+
setClickListeners()
54+
enableParentViews(false)
55+
}
56+
57+
override fun onDestroyView() {
58+
enableParentViews(true)
59+
super.onDestroyView()
60+
}
61+
62+
private fun findViews(view: View) {
63+
layoutTop = view.findViewById(R.id.isolation_flags_space_top)
64+
layoutBottom = view.findViewById(R.id.isolation_flags_layout_bottom)
65+
layoutEnd = view.findViewById(R.id.isolation_flags_layout_end)
66+
layoutStart = view.findViewById(R.id.isolation_flags_layout_start)
67+
layoutConstraintInner = view.findViewById(R.id.isolation_flags_layout_constraint_inner)
68+
}
69+
70+
private fun setMargins() {
71+
val marginSize = layoutConstraintInner.marginTop
72+
73+
val layoutParams = layoutConstraintInner.layoutParams as ConstraintLayout.LayoutParams
74+
layoutParams.apply {
75+
this.setMargins(marginSize, marginSize, marginSize, marginSize + bottomMarginAdjust)
76+
}
77+
}
78+
79+
private fun setClickListeners() {
80+
layoutTop.setOnClickListener {
81+
removeFragment()
82+
}
83+
layoutBottom.setOnClickListener {
84+
removeFragment()
85+
}
86+
layoutEnd.setOnClickListener {
87+
removeFragment()
88+
}
89+
layoutStart.setOnClickListener {
90+
removeFragment()
91+
}
92+
}
93+
94+
private fun removeFragment() {
95+
// TODO: Save Data
96+
parentFragmentManager.beginTransaction().apply {
97+
remove(this@IsolationFlagsFragment)
98+
commit()
99+
}
100+
}
101+
102+
private fun enableParentViews(enable: Boolean) {
103+
parentFragment?.view?.apply {
104+
val layoutSecondary = findViewById<ConstraintLayout>(R.id.settings_tor_layout_constraint_scroll)
105+
for (i in 0 until layoutSecondary.childCount) {
106+
val child = layoutSecondary.getChildAt(i)
107+
child.isEnabled = enable
108+
}
109+
findViewById<Button>(R.id.settings_tor_button_save).isEnabled = enable
110+
}
111+
}
112+
113+
private inner class BackPressHandler(enable: Boolean): OnBackPressedCallback(enable) {
114+
115+
override fun handleOnBackPressed() {
116+
removeFragment()
117+
}
118+
119+
}
120+
}

sampleapp/src/main/java/io/matthewnelson/sampleapp/ui/fragments/settings/tor/SettingsTorFragment.kt

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,13 @@
6767
package io.matthewnelson.sampleapp.ui.fragments.settings.tor
6868

6969
import android.os.Bundle
70-
import androidx.fragment.app.Fragment
7170
import android.view.LayoutInflater
7271
import android.view.View
7372
import android.view.ViewGroup
74-
import android.widget.*
75-
import androidx.annotation.NavigationRes
76-
import androidx.navigation.NavController
77-
import androidx.navigation.NavDestination
73+
import android.view.ViewTreeObserver.OnGlobalLayoutListener
74+
import android.widget.Button
75+
import androidx.core.view.marginBottom
76+
import androidx.fragment.app.Fragment
7877
import androidx.navigation.fragment.findNavController
7978
import io.matthewnelson.sampleapp.R
8079
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashMessage
@@ -83,7 +82,6 @@ import io.matthewnelson.sampleapp.ui.fragments.settings.CloseKeyBoardNavListener
8382
import io.matthewnelson.sampleapp.ui.fragments.settings.tor.components.DnsPortOption
8483
import io.matthewnelson.sampleapp.ui.fragments.settings.tor.components.HttpPortOption
8584
import io.matthewnelson.sampleapp.ui.fragments.settings.tor.components.SocksPortOption
86-
import io.matthewnelson.sampleapp.ui.fragments.settings.tor.components.TransPortOption
8785
import io.matthewnelson.topl_service.TorServiceController
8886
import io.matthewnelson.topl_service.service.components.onionproxy.ServiceTorSettings
8987

@@ -99,7 +97,10 @@ class SettingsTorFragment : Fragment() {
9997
private lateinit var socksPortOption: SocksPortOption
10098
private lateinit var httpPortOption: HttpPortOption
10199
private lateinit var dnsPortOption: DnsPortOption
102-
private lateinit var transPortOption: TransPortOption
100+
// private lateinit var transPortOption: TransPortOption
101+
102+
private lateinit var buttonSocksFlags: Button
103+
private var saveButtonHeight = 0
103104

104105
private lateinit var buttonSave: Button
105106

@@ -122,19 +123,31 @@ class SettingsTorFragment : Fragment() {
122123
findNavController().addOnDestinationChangedListener(CloseKeyBoardNavListener(view))
123124

124125
findViews(view)
125-
setButtonClickListener()
126+
setButtonClickListener(view)
126127
}
127128

128129
override fun onDestroyView() {
129130
super.onDestroyView()
130131
}
131132

132133
private fun findViews(view: View) {
134+
buttonSocksFlags = view.findViewById(R.id.settings_tor_button_socks_isolation_flags)
133135
buttonSave = view.findViewById(R.id.settings_tor_button_save)
136+
137+
val viewTreeObserver = view.viewTreeObserver
138+
if (viewTreeObserver.isAlive) {
139+
viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
140+
override fun onGlobalLayout() {
141+
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
142+
saveButtonHeight = buttonSave.height + buttonSave.marginBottom
143+
}
144+
})
145+
}
134146
}
135147

136-
private fun setButtonClickListener() {
148+
private fun setButtonClickListener(view: View) {
137149
buttonSave.setOnClickListener {
150+
CloseKeyBoardNavListener.closeKeyboard(view)
138151
socksPortOption.saveSocksPort() ?: return@setOnClickListener
139152
httpPortOption.saveHttpPort() ?: return@setOnClickListener
140153
dnsPortOption.saveDnsPort() ?: return@setOnClickListener
@@ -144,5 +157,18 @@ class SettingsTorFragment : Fragment() {
144157
DashMessage("Settings Saved\nTor may need to be restarted", R.drawable.dash_message_color_green, 4_000L)
145158
)
146159
}
160+
buttonSocksFlags.setOnClickListener {
161+
childFragmentManager.beginTransaction().apply {
162+
add(
163+
R.id.settings_tor_fragment_container,
164+
IsolationFlagsFragment(
165+
saveButtonHeight,
166+
IsolationFlagsFragment.SOCKS_FLAGS,
167+
serviceTorSettings
168+
)
169+
)
170+
commit()
171+
}
172+
}
147173
}
148174
}

0 commit comments

Comments
 (0)