Skip to content

Commit

Permalink
Add darkmode
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Jan 27, 2021
1 parent d9ae29d commit 85e17f7
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cyb3rko.logviewerforopenhab

import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.ActivityInfo
Expand All @@ -14,6 +13,7 @@ import android.text.style.UnderlineSpan
import android.view.View
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.widget.Toolbar
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.NavController
Expand Down Expand Up @@ -47,6 +47,7 @@ class MainActivity : AppCompatActivity() {
mySPR = getSharedPreferences(SHARED_PREFERENCE, 0)
editor = mySPR.edit()
editor.apply()
AppCompatDelegate.setDefaultNightMode(mySPR.getString(NIGHTMODE, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM.toString())!!.toInt())
requestedOrientation = mySPR.getString(ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED.toString())?.toInt()!!

setContentView(R.layout.activity_main)
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/cyb3rko/logviewerforopenhab/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ internal const val TERMS_OF_USE = "terms_of_use"
internal const val SHARED_PREFERENCE = "Safe2"
internal const val ANALYTICS_COLLECTION = "analytics_collection"
internal const val AUTO_START = "auto_start"
internal const val AUTO_UPDATE = "auto_update"
internal const val CONSENT_DATE = "consent_date"
internal const val CONSENT_TIME = "consent_time"
internal const val CONNECT_CHECK = "connect_check"
Expand All @@ -30,8 +29,8 @@ internal const val FIRST_START = "first_start"
internal const val FIRST_START_WEB = "first_start_web"
internal const val HOSTNAME_CHECK = "hostname_check"
internal const val HOSTNAME_STRING = "hostname_string"
internal const val NEWEST_VERSION = "newest_version"
internal const val LINK = "link"
internal const val NIGHTMODE = "nightmode"
internal const val ORIENTATION = "orientation"
internal const val PORT_CHECK = "port_check"
internal const val PORT_INT = "port_int"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cyb3rko.logviewerforopenhab.fragments

import android.content.Intent
import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
Expand All @@ -18,7 +19,15 @@ class AboutFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

val nightMode = when (requireContext().resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) {
Configuration.UI_MODE_NIGHT_YES -> true
Configuration.UI_MODE_NIGHT_NO -> false
Configuration.UI_MODE_NIGHT_UNDEFINED -> false
else -> false
}

return AboutPage(context)
.enableDarkMode(nightMode)
.setImage(R.mipmap.ic_launcher_foreground)
.setDescription(getString(R.string.about_description))
.addItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.ActivityInfo
import android.os.Bundle
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import com.cyb3rko.logviewerforopenhab.*
import com.cyb3rko.logviewerforopenhab.ANALYTICS_COLLECTION
import com.cyb3rko.logviewerforopenhab.AUTO_UPDATE
import com.cyb3rko.logviewerforopenhab.CONNECTION_OVERVIEW_ENABLED
import com.cyb3rko.logviewerforopenhab.CRASHLYTICS_COLLECTION
import com.cyb3rko.logviewerforopenhab.DATA_DELETION
Expand All @@ -22,14 +22,15 @@ import com.cyb3rko.logviewerforopenhab.showConnections
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.crashlytics.FirebaseCrashlytics
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.activity_main.*

class PreferenceFragment : PreferenceFragmentCompat() {

private lateinit var analyticsCollectionSwitch: SwitchPreferenceCompat
private lateinit var autoUpdateSwitch: SwitchPreferenceCompat
private lateinit var connectionOverviewSwitch: SwitchPreferenceCompat
private lateinit var crashlyticsCollectionSwitch: SwitchPreferenceCompat
private lateinit var mySPR: SharedPreferences
private lateinit var nightModeList: ListPreference
private lateinit var orientationList: ListPreference

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
Expand All @@ -38,11 +39,13 @@ class PreferenceFragment : PreferenceFragmentCompat() {
mySPR = preferenceManager.sharedPreferences
orientationList = findPreference(ORIENTATION)!!
connectionOverviewSwitch = findPreference(CONNECTION_OVERVIEW_ENABLED)!!
nightModeList = findPreference(NIGHTMODE)!!
analyticsCollectionSwitch = findPreference(ANALYTICS_COLLECTION)!!
crashlyticsCollectionSwitch = findPreference(CRASHLYTICS_COLLECTION)!!

orientationList.value = mySPR.getString(ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED.toString())
connectionOverviewSwitch.isChecked = mySPR.getBoolean(CONNECTION_OVERVIEW_ENABLED, true)
nightModeList.value = mySPR.getString(NIGHTMODE, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM.toString())
analyticsCollectionSwitch.isChecked = mySPR.getBoolean(ANALYTICS_COLLECTION, true)
crashlyticsCollectionSwitch.isChecked = mySPR.getBoolean(CRASHLYTICS_COLLECTION, true)
}
Expand All @@ -69,6 +72,15 @@ class PreferenceFragment : PreferenceFragmentCompat() {
}
true
}
NIGHTMODE -> {
nightModeList.setOnPreferenceChangeListener { _, newValue ->
val navView = requireActivity().nav_view
navView.setCheckedItem(navView.checkedItem?.itemId?.minus(1)!!)
AppCompatDelegate.setDefaultNightMode(newValue.toString().toInt())
true
}
true
}
ANALYTICS_COLLECTION -> {
FirebaseAnalytics.getInstance(requireContext()).setAnalyticsCollectionEnabled(analyticsCollectionSwitch.isChecked)
true
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
<string name="settings_orientation_summary">Toggle the orientation of the app</string>
<string name="settings_connection_overview_title">Connection Overview</string>
<string name="settings_connection_overview_summary">En-/Disable the overview of recent connections in the Navigation Drawer</string>
<string name="settings_auto_update_title">Auto Update</string>
<string name="settings_auto_update_summary">En-/Disable checking for updates and optionally downloading the newewst version</string>
<string name="settings_nightmode_title">Night Mode</string>
<string name="settings_nightmode_summary">Toggle the dark appearance of the app (logview excluded)</string>
<string name="settings_analytics_title">Analytics Data Collection</string>
<string name="settings_analytics_summary">En-/Disable data collection via Firebase Analytics</string>
<string name="settings_crashlytics_title">Crashlytics Data Collection</string>
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/values/night_modes.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>
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
<string name="settings_orientation_summary">Toggle the orientation of the app</string>
<string name="settings_connection_overview_title">Connection Overview</string>
<string name="settings_connection_overview_summary">En-/Disable the overview of recent connections in the Navigation Drawer</string>
<string name="settings_auto_update_title">Auto Update</string>
<string name="settings_auto_update_summary">En-/Disable checking for updates and optionally downloading the newewst version</string>
<string name="settings_nightmode_title">Night Mode</string>
<string name="settings_nightmode_summary">Toggle the dark appearance of the app (logview excluded)</string>
<string name="settings_analytics_title">Analytics Data Collection</string>
<string name="settings_analytics_summary">En-/Disable data collection via Firebase Analytics</string>
<string name="settings_crashlytics_title">Crashlytics Data Collection</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
android:defaultValue="true"
android:key="connection_overview_enabled" />

<ListPreference
android:title="@string/settings_nightmode_title"
android:summary="@string/settings_nightmode_summary"
app:iconSpaceReserved="false"
android:defaultValue=""
android:entries="@array/night_modes"
android:entryValues="@array/night_modes_values"
android:key="nightmode"/>

</PreferenceCategory>

<PreferenceCategory
Expand Down

0 comments on commit 85e17f7

Please sign in to comment.