Skip to content

Commit 28f8636

Browse files
HatsuneMikuUwUNkBe
authored andcommitted
add banner home controller
1 parent 97768c0 commit 28f8636

8 files changed

Lines changed: 63 additions & 2 deletions

File tree

app/src/main/java/io/nekohasekai/sagernet/database/DataStore.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ object DataStore : OnPreferenceDataStoreChangeListener {
9696
var dpiValue: Int
9797
get() = configurationStore.getInt("dpi_value", 0)
9898
set(value) = configurationStore.putInt("dpi_value", value)
99+
var showBannerLayout by configurationStore.boolean("show_banner_layout") { true }
99100

100101
var appTLSVersion by configurationStore.string(Key.APP_TLS_VERSION)
101102
var enableClashAPI by configurationStore.boolean(Key.ENABLE_CLASH_API)

app/src/main/java/io/nekohasekai/sagernet/ui/ConfigurationFragment.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ class ConfigurationFragment @JvmOverloads constructor(
134134
lateinit var tabLayout: TabLayout
135135
lateinit var groupPager: ViewPager2
136136

137+
private var bannerLayoutListener: OnPreferenceDataStoreChangeListener? = null
138+
137139
val alwaysShowAddress by lazy { DataStore.alwaysShowAddress }
138140

139141
fun getCurrentGroupFragment(): GroupFragment? {
@@ -178,6 +180,8 @@ class ConfigurationFragment @JvmOverloads constructor(
178180
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
179181
super.onViewCreated(view, savedInstanceState)
180182

183+
setupBannerLayoutController()
184+
181185
if (!select) {
182186
toolbar.inflateMenu(R.menu.add_profile_menu)
183187
toolbar.setOnMenuItemClickListener(this)
@@ -264,6 +268,15 @@ class ConfigurationFragment @JvmOverloads constructor(
264268
}
265269
}
266270

271+
override fun onDestroyView() {
272+
bannerLayoutListener?.let {
273+
DataStore.configurationStore.unregisterChangeListener(it)
274+
}
275+
bannerLayoutListener = null
276+
277+
super.onDestroyView()
278+
}
279+
267280
override fun onDestroy() {
268281
DataStore.profileCacheStore.unregisterChangeListener(this)
269282

@@ -1757,5 +1770,26 @@ class ConfigurationFragment @JvmOverloads constructor(
17571770
searchView.onActionViewCollapsed()
17581771
searchView.clearFocus()
17591772
}
1773+
1774+
private fun setupBannerLayoutController() {
1775+
val linear = requireView().findViewById<View>(R.id.banner_home)
1776+
linear?.visibility = if (DataStore.showBannerLayout) View.VISIBLE else View.GONE
1777+
1778+
if (bannerLayoutListener == null) {
1779+
bannerLayoutListener = object : OnPreferenceDataStoreChangeListener {
1780+
override fun onPreferenceDataStoreChanged(store: PreferenceDataStore, key: String) {
1781+
if (key == "show_banner_layout") {
1782+
val show = DataStore.showBannerLayout
1783+
1784+
if (!isAdded) return
17601785

1786+
requireActivity().runOnUiThread {
1787+
linear?.visibility = if (show) View.VISIBLE else View.GONE
1788+
}
1789+
}
1790+
}
1791+
}
1792+
DataStore.configurationStore.registerChangeListener(bannerLayoutListener!!)
1793+
}
1794+
}
17611795
}

app/src/main/java/io/nekohasekai/sagernet/ui/ThemeSettingsPreferenceFragment.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,16 @@ class ThemeSettingsPreferenceFragment : PreferenceFragmentCompat() {
203203
}
204204
true
205205
}
206+
207+
// Banner home show or hide
208+
val layoutController: SwitchPreference? = findPreference("show_banner_layout")
209+
layoutController?.apply {
210+
isChecked = DataStore.showBannerLayout
211+
setOnPreferenceChangeListener { _: Preference, newValue: Any ->
212+
val show = newValue as Boolean
213+
DataStore.showBannerLayout = show
214+
true
215+
}
216+
}
206217
}
207218
}

app/src/main/res/layout/layout_group_list.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
android:layout_height="wrap_content"
1111
layout="@layout/layout_appbar" />
1212
<LinearLayout
13+
android:id="@+id/banner_home"
1314
android:background="?attr/colorPrimary"
1415
android:paddingBottom="16.0dip"
1516
android:clipToPadding="false"
@@ -193,4 +194,4 @@
193194
</LinearLayout>
194195
</com.google.android.material.card.MaterialCardView>
195196
</FrameLayout>
196-
</LinearLayout>
197+
</LinearLayout>

app/src/main/res/values-in/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ Tolong kasih izinnya di Settingan Sistem."</string>
525525
<string name="reset_dpi_title">Reset DPI</string>
526526
<string name="reset_dpi_message">Kamu mau balikin DPI ke bawaan sistem (%1$d)?</string>
527527
<string name="reset">Reset</string>
528+
<string name="banner_home_title">Tampilin banner beranda</string>
529+
<string name="banner_home_summary">Sembunyiin atau tampilin banner di beranda</string>
528530

529531
<string name="uwu_about_miku_title">Siapa itu Hatsune Miku?</string>
530532
<string name="uwu_about_miku_summary">Hatsune Miku itu penyanyi virtual berbasis Vocaloid yang dirilis sama Crypton Future Media, kode namanya CV01. Dikenal gara-gara rambut twintail panjang warna biru kehijauan sama suaranya yang jernih, dia itu cewek 16 tahun dan idola virtual yang terkenal sedunia. Dia aktif di musik, konser, dan anime, plus disayang banget sama fans-fansnya.</string>

app/src/main/res/values-zh-rCN/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@
521521
<string name="reset_dpi_title">重置 DPI</string>
522522
<string name="reset_dpi_message">是否要将 DPI 恢复为系统默认值 (%1$d)?</string>
523523
<string name="reset">重置</string>
524+
<string name="banner_home_title">显示首页横幅</string>
525+
<string name="banner_home_summary">隐藏或显示首页横幅</string>
524526

525527
<!-- uwu -->
526528
<string name="uwu_about_miku_title">谁是 Hatsune Miku?</string>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,8 @@ Please grant this permission in System Settings."</string>
620620
<string name="reset_dpi_title">Reset DPI</string>
621621
<string name="reset_dpi_message">Do you want to return the DPI to the system default (%1$d)?</string>
622622
<string name="reset">Reset</string>
623+
<string name="banner_home_title">Show home banner</string>
624+
<string name="banner_home_summary">Hide or show banner on homepage</string>
623625

624626
<!-- uwu -->
625627
<string name="uwu_about_miku_title">Who is Hatsune Miku?</string>

app/src/main/res/xml/theme_preferences.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,21 @@
8181
android:defaultValue="true"
8282
app:icon="@drawable/ic_audio" />
8383
<com.takisoft.preferencex.SimpleMenuPreference
84-
android:layout="@layout/uwu_bot"
84+
android:layout="@layout/uwu_mid"
8585
app:defaultValue=""
8686
app:entries="@array/language_entry"
8787
app:entryValues="@array/language_value"
8888
app:icon="@drawable/ic_baseline_translate_24"
8989
app:isPreferenceVisible="true"
9090
app:key="app_language"
9191
app:title="@string/language" />
92+
<SwitchPreference
93+
android:layout="@layout/uwu_bot_switch"
94+
android:title="@string/banner_home_title"
95+
android:key="show_banner_layout"
96+
android:summary="@string/banner_home_summary"
97+
android:widgetLayout="@layout/uwu_switch"
98+
android:defaultValue="true"
99+
app:icon="@drawable/ic_toggle_switch" />
92100
</PreferenceCategory>
93101
</PreferenceScreen>

0 commit comments

Comments
 (0)