-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseActivity.java
More file actions
88 lines (74 loc) · 2.99 KB
/
Copy pathBaseActivity.java
File metadata and controls
88 lines (74 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.gratus.workoutrepo;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
public class BaseActivity extends AppCompatActivity {
public static final String PREFS_NAME = "WorkoutRepoAppSettings";
public static final String PREF_USE_DIALOG = "use_dialog_over_bottomsheet";
protected static final String THEME_KEY = "SelectedTheme";
@Override
protected void onCreate(Bundle savedInstanceState) {
applyTheme();
super.onCreate(savedInstanceState);
// Edge-to-edge setup — safe to do before setContentView
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
getWindow().setNavigationBarColor(Color.TRANSPARENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getWindow().setNavigationBarContrastEnforced(false);
}
}
/**
* Call this from child activities after setContentView(),
* passing the view that should receive system bar padding.
*/
protected void applySystemBarInsets(int viewId) {
View target = findViewById(viewId);
if (target == null) return;
ViewCompat.setOnApplyWindowInsetsListener(target, (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
/**
* Apply the saved theme or default to 'auto'.
*/
protected void applyTheme() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
String theme = prefs.getString(THEME_KEY, "auto"); // Default to 'auto'
switch (theme) {
case "light":
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
case "dark":
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case "auto":
default:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
break;
}
}
/**
* Set the theme and save the selection to SharedPreferences.
*/
protected void setThemeAndSave(String theme) {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
prefs.edit().putString(THEME_KEY, theme).apply();
// Log the saved theme
System.out.println("Saved theme: " + theme);
// Apply the new theme
applyTheme();
// Restart the activity to reflect the theme change
recreate();
}
}