Skip to content

Commit

Permalink
Fixed theme changes
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Feb 24, 2023
1 parent 813b388 commit e9233e5
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 14 deletions.
8 changes: 8 additions & 0 deletions app/src/main/java/com/f0x1d/logfox/LogFoxApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package com.f0x1d.logfox
import android.app.Application
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationManagerCompat
import com.f0x1d.logfox.extensions.applyTheme
import com.f0x1d.logfox.extensions.notificationManagerCompat
import com.f0x1d.logfox.utils.preferences.AppPreferences
import com.f0x1d.logfox.utils.view.FontsInterceptor
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.HiltAndroidApp
import io.github.inflationx.viewpump.ViewPump
import kotlinx.coroutines.MainScope
import javax.inject.Inject

@HiltAndroidApp
class LogFoxApp: Application() {
Expand All @@ -22,10 +25,15 @@ class LogFoxApp: Application() {
lateinit var instance: LogFoxApp
}

@Inject
lateinit var appPreferences: AppPreferences

override fun onCreate() {
super.onCreate()
instance = this

applyTheme(appPreferences.nightTheme)

ViewPump.init(
ViewPump.builder()
.addInterceptor(FontsInterceptor(this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package com.f0x1d.logfox.extensions
import android.Manifest
import android.app.ActivityManager
import android.app.NotificationManager
import android.app.UiModeManager
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.widget.Toast
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
Expand All @@ -32,6 +34,7 @@ fun Context.hasPermissionToReadLogs() = ContextCompat.checkSelfPermission(
val Context.notificationManagerCompat get() = NotificationManagerCompat.from(this)
val Context.notificationManager get() = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val Context.activityManager get() = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val Context.uiModeManager get() = getSystemService(Context.UI_MODE_SERVICE) as UiModeManager

fun Context.startLoggingAndService(loggingRepository: LoggingRepository, appPreferences: AppPreferences, force: Boolean = false) {
loggingRepository.startLoggingIfNot()
Expand Down Expand Up @@ -108,4 +111,13 @@ else
fun Context.doIfPermitted(block: NotificationManagerCompat.() -> Unit) = if (hasNotificationsPermission())
block.invoke(notificationManagerCompat)
else
Unit
Unit

fun Context.applyTheme(nightMode: Int, force: Boolean = false) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (force) uiModeManager.setApplicationNightMode(
if (nightMode != 0) nightMode else UiModeManager.MODE_NIGHT_CUSTOM
)
} else
AppCompatDelegate.setDefaultNightMode(if (nightMode != 0) nightMode else AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const val EXIT_APP_INTENT_ID = 3
const val PAUSE_RECORDING_INTENT_ID = 4
const val RESUME_RECORDING_INTENT_ID = 5
const val STOP_RECORDING_INTENT_ID = 6
const val OPEN_APP_INTENT_ID = 7

val pendingIntentFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/com/f0x1d/logfox/service/LoggingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import androidx.core.app.NotificationCompat
import com.f0x1d.logfox.BuildConfig
import com.f0x1d.logfox.LogFoxApp
import com.f0x1d.logfox.R
import com.f0x1d.logfox.extensions.EXIT_APP_INTENT_ID
import com.f0x1d.logfox.extensions.STOP_LOGGING_SERVICE_INTENT_ID
import com.f0x1d.logfox.extensions.activityManager
import com.f0x1d.logfox.extensions.makeServicePendingIntent
import com.f0x1d.logfox.extensions.*
import com.f0x1d.logfox.repository.logging.LoggingRepository
import com.f0x1d.logfox.ui.activity.MainActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.update
import javax.inject.Inject
Expand Down Expand Up @@ -47,6 +45,7 @@ class LoggingService: Service() {
private fun notification() = NotificationCompat.Builder(this, LogFoxApp.LOGGING_STATUS_CHANNEL_ID)
.setContentTitle(getString(R.string.logging))
.setSmallIcon(R.drawable.ic_bug_notification)
.setContentIntent(makeActivityPendingIntent(OPEN_APP_INTENT_ID, MainActivity::class.java))
.addAction(R.drawable.ic_stop, getString(R.string.stop_service), makeServicePendingIntent(STOP_LOGGING_SERVICE_INTENT_ID, LoggingService::class.java) {
action = ACTION_STOP_SERVICE
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.view.WindowCompat
import androidx.viewbinding.ViewBinding
import com.f0x1d.logfox.extensions.snackbar
import com.f0x1d.logfox.utils.preferences.AppPreferences
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.components.SingletonComponent
import io.github.inflationx.viewpump.ViewPumpContextWrapper

Expand All @@ -24,10 +22,6 @@ abstract class BaseActivity<T : ViewBinding>: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
WindowCompat.setDecorFitsSystemWindows(window, false)

EntryPointAccessors.fromApplication(applicationContext, BaseActivityEntryPoint::class.java).appPreferences().nightTheme.also { nightInt ->
AppCompatDelegate.setDefaultNightMode(if (nightInt != 0) nightInt else AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}

super.onCreate(savedInstanceState)

inflateBinding()?.also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SettingsUIFragment: BaseSettingsWrapperFragment() {
appPreferences.nightTheme
) {
appPreferences.nightTheme = it
requireActivity().recreate()
requireActivity().applyTheme(it, true)
}

observeAndUpdateSummaryForList(appPreferences, this@SettingsUIWrappedFragment, 0, filledThemeSettings)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.LogFox" parent="Theme.Material3.Dark.NoActionBar">
<style name="Theme.LogFox" parent="Theme.Material3.DayNight.NoActionBar">
<item name="android:statusBarColor" >@android:color/transparent</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">false</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.LogFox" parent="Theme.Material3.Light.NoActionBar">
<style name="Theme.LogFox" parent="Theme.Material3.DayNight.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
Expand Down

0 comments on commit e9233e5

Please sign in to comment.