Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
Add preference screen with night mode and announcement settings
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Jun 5, 2021
1 parent 44ca273 commit a069653
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation "androidx.preference:preference-ktx:1.1.1"
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation 'com.afollestad.material-dialogs:bottomsheets:3.3.0'
implementation 'com.afollestad.material-dialogs:input:3.3.0'
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/cyb3rko/cavedroid/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cyb3rko.cavedroid

import android.app.Application
import androidx.appcompat.app.AppCompatDelegate

class App : Application() {

override fun onCreate() {
super.onCreate()
AppCompatDelegate.setDefaultNightMode(getSharedPreferences(SHARED_PREFERENCE, MODE_PRIVATE).getString(NIGHTMODE, AppCompatDelegate
.MODE_NIGHT_FOLLOW_SYSTEM.toString())!!.toInt())
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/com/cyb3rko/cavedroid/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class MainActivity : AppCompatActivity() {

fun receiveLatestAnnouncement() {
val sharedPreferences = getSharedPreferences("Safe", MODE_PRIVATE)
if (!sharedPreferences.getBoolean(SHOW_ANNOUNCEMENTS, true)) return
if (sharedPreferences.getString("name", "") == "") return

GlobalScope.launch {
Expand Down Expand Up @@ -102,7 +103,7 @@ class MainActivity : AppCompatActivity() {
val view = it.getCustomView()
if (messageObject.attachments.isNotEmpty()) {
val drawable = messageObject.attachments.toList()[0]
if (drawable.isImage) {
if (sharedPreferences.getBoolean(ANNOUNCEMENT_IMAGE, true) && drawable.isImage) {
Glide.with(applicationContext)
.load(drawable.url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
Expand Down
68 changes: 68 additions & 0 deletions app/src/main/java/com/cyb3rko/cavedroid/PreferenceFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.cyb3rko.cavedroid

import android.content.SharedPreferences
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference

class PreferenceFragment : PreferenceFragmentCompat() {

private lateinit var mySPR: SharedPreferences

private lateinit var nightModeList: ListPreference
private lateinit var showAnnouncementSwitch: SwitchPreference
private lateinit var announcementImageSwitch: SwitchPreference
private lateinit var analyticsCollectionSwitch: SwitchPreference
private lateinit var crashlyticsCollectionSwitch: SwitchPreference

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
addPreferencesFromResource(R.xml.preferences)
preferenceManager.sharedPreferencesName = SHARED_PREFERENCE
mySPR = preferenceManager.sharedPreferences
nightModeList = findPreference(NIGHTMODE)!!
showAnnouncementSwitch = findPreference(SHOW_ANNOUNCEMENTS)!!
announcementImageSwitch = findPreference(ANNOUNCEMENT_IMAGE)!!
// analyticsCollectionSwitch = findPreference(ANALYTICS_COLLECTION)!!
// crashlyticsCollectionSwitch = findPreference(CRASHLYTICS_COLLECTION)!!

nightModeList.value = mySPR.getString(NIGHTMODE, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM.toString())
showAnnouncementSwitch.isChecked = mySPR.getBoolean(SHOW_ANNOUNCEMENTS, true)
announcementImageSwitch.isChecked = mySPR.getBoolean(ANNOUNCEMENT_IMAGE, true)
// analyticsCollectionSwitch.isChecked = mySPR.getBoolean(ANALYTICS_COLLECTION, true)
// crashlyticsCollectionSwitch.isChecked = mySPR.getBoolean(CRASHLYTICS_COLLECTION, true)
}

override fun onPreferenceTreeClick(preference: Preference?): Boolean {
return when (preference?.key) {
NIGHTMODE -> {
nightModeList.setOnPreferenceChangeListener { _, newValue ->
AppCompatDelegate.setDefaultNightMode(newValue.toString().toInt())
true
}
true
}
// ANALYTICS_COLLECTION -> {
// FirebaseAnalytics.getInstance(requireContext()).setAnalyticsCollectionEnabled(analyticsCollectionSwitch.isChecked)
// true
// }
// CRASHLYTICS_COLLECTION -> {
// FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(crashlyticsCollectionSwitch.isChecked)
// true
// }
// DATA_DELETION -> {
// FirebaseAnalytics.getInstance(requireActivity()).resetAnalyticsData()
// FirebaseCrashlytics.getInstance().deleteUnsentReports()
// Toasty.success(requireContext(), getString("Deletion done"), Toasty.LENGTH_SHORT).show()
// true
// }
else -> false
}
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/cyb3rko/cavedroid/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.cyb3rko.cavedroid

//internal const val PRIVACY_POLICY = "privacy_policy"
//internal const val TERMS_OF_USE = "terms_of_use"

//internal const val ANALYTICS_COLLECTION = "analytics_collection"
internal const val ANNOUNCEMENT_IMAGE = "announcement_image"
//internal const val CONSENT_DATE = "consent_date"
//internal const val CONSENT_TIME = "consent_time"
//internal const val CRASHLYTICS_COLLECTION = "crashlytics_collection"
//internal const val DATA_DELETION = "data_deletion"
//internal const val FIRST_START = "first_start"
internal const val NIGHTMODE = "nightmode"
internal const val SHARED_PREFERENCE = "Safe"
internal const val SHOW_ANNOUNCEMENTS = "show_announcements"

object Utils {
}
Binary file added app/src/main/res/drawable/_ic_settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions app/src/main/res/menu/bottom_nav_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@
android:icon="@drawable/_ic_ranking"
android:title="Rankings" />

<item
android:id="@+id/navigation_preferences"
android:icon="@drawable/_ic_settings"
android:title="Preferences" />

</menu>
6 changes: 6 additions & 0 deletions app/src/main/res/navigation/mobile_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
app:destination="@id/navigation_rankings" />
</fragment>

<fragment
android:id="@+id/navigation_preferences"
android:name="com.cyb3rko.cavedroid.PreferenceFragment"
android:label="Preferences">
</fragment>

<fragment
android:id="@+id/navigation_about_icons"
android:name="com.cyb3rko.cavedroid.AboutIconsFragment"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/about_icons.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<item>https://www.flaticon.com/free-icon/magnifier_64673</item>
</string-array>

<string-array name="ic_settings">
<item>Freepik</item>
<item>flaticon.com</item>
<item>https://www.flaticon.com/premium-icon/gear_484613?term=settings</item>
</string-array>

<string-array name="ic_sold">
<item>Kiranshastry</item>
<item>flaticon.com</item>
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/values/preferences_values.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="night_modes">
<item>System Default</item>
<item>On</item>
<item>Off</item>
</string-array>
<string-array name="night_modes_values">
<item>-1</item>
<item>2</item>
<item>1</item>
</string-array>
</resources>
62 changes: 62 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory
android:title="User Experience"
app:iconSpaceReserved="false">

<ListPreference
android:title="Night Mode"
android:summary="Toggle the dark appearance of the app"
app:iconSpaceReserved="false"
android:defaultValue=""
android:entries="@array/night_modes"
android:entryValues="@array/night_modes_values"
android:key="nightmode"/>

</PreferenceCategory>

<PreferenceCategory
android:title="Announcements"
app:iconSpaceReserved="false">

<SwitchPreference
android:title="Show Announcements"
android:summary="En-/Disable the anouncement dialog to view the latest Cavetale announcement"
android:key="show_announcements"
app:iconSpaceReserved="false" />

<SwitchPreference
android:title="Include image of announcements (if available)"
android:summary="Download and show images of anouncements if any available"
android:key="announcement_image"
app:iconSpaceReserved="false" />

</PreferenceCategory>

<!-- <PreferenceCategory-->
<!-- android:title="Data Collection"-->
<!-- app:iconSpaceReserved="false">-->

<!-- <SwitchPreferenceCompat-->
<!-- android:title="Analytics Data Collection"-->
<!-- android:summary="En-/Disable data collection via Firebase Analytics"-->
<!-- android:key="analytics_collection"-->
<!-- app:iconSpaceReserved="false" />-->

<!-- <SwitchPreferenceCompat-->
<!-- android:title="Crashlytics Data Collection"-->
<!-- android:summary="En-/Disable data collection via Firebase Crashlytics"-->
<!-- android:key="crashlytics_collection"-->
<!-- app:iconSpaceReserved="false" />-->

<!-- <Preference-->
<!-- android:title="Reset Data Collection"-->
<!-- android:summary="Reset your collected data for Analytics and Crashlytics"-->
<!-- android:key="data_deletion"-->
<!-- app:iconSpaceReserved="false" />-->

<!-- </PreferenceCategory>-->

</PreferenceScreen>

0 comments on commit a069653

Please sign in to comment.