Skip to content

Commit d04e11d

Browse files
fixed FB-RTDB app check
1 parent 90e4753 commit d04e11d

4 files changed

Lines changed: 69 additions & 71 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ dependencies {
7878
implementation 'com.google.firebase:firebase-analytics'
7979
implementation 'com.google.firebase:firebase-database'
8080
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.11.0'
81-
implementation 'com.google.firebase:firebase-appcheck'
82-
implementation 'com.google.firebase:firebase-appcheck-debug'
81+
implementation 'com.google.firebase:firebase-auth'
8382

8483
// Coil for image loading
8584
implementation("io.coil-kt.coil3:coil-compose:3.5.0")

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
android:label="@string/app_name" />
2323

2424
<application
25-
android:name=".BetterNothingApplication"
2625
android:enableOnBackInvokedCallback="true"
2726
android:icon="@mipmap/ic_launcher"
2827
android:label="@string/app_name"

app/src/main/java/com/better/nothing/music/vizualizer/BetterNothingApplication.kt

Lines changed: 0 additions & 20 deletions
This file was deleted.

app/src/main/java/com/better/nothing/music/vizualizer/ui/MainActivity.kt

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ import androidx.compose.ui.unit.sp
105105
import androidx.compose.foundation.layout.fillMaxWidth
106106
import android.util.Base64
107107
import com.google.firebase.database.FirebaseDatabase
108+
import com.google.firebase.auth.FirebaseAuth
108109
import androidx.compose.foundation.layout.size
109110
import androidx.compose.foundation.layout.height
110111
import androidx.compose.foundation.layout.Spacer
@@ -320,51 +321,10 @@ internal class MainViewModel(application: Application) : AndroidViewModel(applic
320321
}
321322
_userId.value = uId
322323

323-
viewModelScope.launch {
324-
try {
325-
val profile = userRepository.getUserProfile(uId)
326-
if (profile != null) {
327-
_userProfile.value = profile
328-
_userNickname.value = profile.displayName
329-
} else {
330-
// Create initial profile
331-
val newProfile = UserProfile(
332-
userId = uId,
333-
displayName = _userNickname.value,
334-
totalVisualizedTime = _totalVisualizedTime.value
335-
)
336-
userRepository.saveUserProfile(newProfile)
337-
_userProfile.value = newProfile
338-
}
339-
} catch (e: Exception) {
340-
Log.e("MainViewModel", "Failed to load/create profile", e)
341-
}
342-
}
343-
344-
FirebaseDatabase.getInstance("https://bnmv-67120-default-rtdb.europe-west1.firebasedatabase.app")
345-
.getReference("config/dev_password")
346-
.get()
347-
.addOnSuccessListener { snapshot ->
348-
_devPassword.value = snapshot.getValue(String::class.java)
349-
}
350-
351324
// Track app openings and show thanks messages
352325
val openCount = prefs.getInt("app_open_count", 0) + 1
353326
prefs.edit().putInt("app_open_count", openCount).apply()
354327

355-
viewModelScope.launch {
356-
announcementRepository.getLatestAnnouncement().collect { announcement ->
357-
_latestAnnouncement.value = announcement
358-
if (announcement != null) {
359-
val sharedPrefs = ctx.getSharedPreferences("viz_prefs", Context.MODE_PRIVATE)
360-
val lastSeenId = sharedPrefs.getString("last_seen_announcement_id", "")
361-
if (announcement.id.toString() != lastSeenId) {
362-
_showAnnouncementModal.value = true
363-
}
364-
}
365-
}
366-
}
367-
368328
viewModelScope.launch {
369329
if (BuildConfig.DEBUG) {
370330
// In debug, show start-up messages every time
@@ -1894,9 +1854,6 @@ internal class MainViewModel(application: Application) : AndroidViewModel(applic
18941854
_configVersion.value = configVersion
18951855
commitPresetInfos(infos)
18961856
val prefs = ctx.getSharedPreferences("viz_prefs", Context.MODE_PRIVATE)
1897-
_autoDeviceEnabled.value = prefs.getBoolean("auto_device_enabled", true)
1898-
_glyphTabEnabled.value = prefs.getBoolean("glyph_tab_enabled", true)
1899-
_hapticsTabEnabled.value = prefs.getBoolean("haptics_tab_enabled", true)
19001857

19011858
_idleBreathingEnabled.value = prefs.getBoolean("idle_breathing_enabled", false)
19021859
_idlePattern.value = prefs.getString("idle_pattern", "pulse") ?: "pulse"
@@ -1942,14 +1899,59 @@ internal class MainViewModel(application: Application) : AndroidViewModel(applic
19421899
_flashlightSmoothing.value = prefs.getFloat("flashlight_smoothing", 0.7f)
19431900
_flashlightGamma.value = prefs.getFloat("flashlight_gamma", 2.2f)
19441901
_flashlightBeatSensitivity.value = prefs.getFloat("flashlight_beat_sensitivity", 1.0f)
1902+
}
19451903

1946-
checkRemoteConfigVersion()
1947-
if (!BuildConfig.DEBUG) {
1948-
checkAppUpdate()
1904+
startRunningStatePoller()
1905+
}
1906+
}
1907+
1908+
fun initDatabase() {
1909+
val uId = _userId.value ?: return
1910+
1911+
viewModelScope.launch {
1912+
try {
1913+
val profile = userRepository.getUserProfile(uId)
1914+
if (profile != null) {
1915+
_userProfile.value = profile
1916+
_userNickname.value = profile.displayName
1917+
} else {
1918+
// Create initial profile
1919+
val newProfile = UserProfile(
1920+
userId = uId,
1921+
displayName = _userNickname.value,
1922+
totalVisualizedTime = _totalVisualizedTime.value
1923+
)
1924+
userRepository.saveUserProfile(newProfile)
1925+
_userProfile.value = newProfile
19491926
}
1927+
} catch (e: Exception) {
1928+
Log.e("MainViewModel", "Failed to load/create profile", e)
19501929
}
1930+
}
19511931

1952-
startRunningStatePoller()
1932+
FirebaseDatabase.getInstance("https://bnmv-67120-default-rtdb.europe-west1.firebasedatabase.app")
1933+
.getReference("config/dev_password")
1934+
.get()
1935+
.addOnSuccessListener { snapshot ->
1936+
_devPassword.value = snapshot.getValue(String::class.java)
1937+
}
1938+
1939+
viewModelScope.launch {
1940+
announcementRepository.getLatestAnnouncement().collect { announcement ->
1941+
_latestAnnouncement.value = announcement
1942+
if (announcement != null) {
1943+
val sharedPrefs = ctx.getSharedPreferences("viz_prefs", Context.MODE_PRIVATE)
1944+
val lastSeenId = sharedPrefs.getString("last_seen_announcement_id", "")
1945+
if (announcement.id.toString() != lastSeenId) {
1946+
_showAnnouncementModal.value = true
1947+
}
1948+
}
1949+
}
1950+
}
1951+
1952+
checkRemoteConfigVersion()
1953+
if (!BuildConfig.DEBUG) {
1954+
checkAppUpdate()
19531955
}
19541956
}
19551957

@@ -2258,6 +2260,20 @@ class MainActivity : ComponentActivity() {
22582260
override fun onCreate(savedInstanceState: Bundle?) {
22592261
super.onCreate(savedInstanceState)
22602262

2263+
val auth = FirebaseAuth.getInstance()
2264+
if (auth.currentUser == null) {
2265+
auth.signInAnonymously()
2266+
.addOnSuccessListener {
2267+
initDatabase()
2268+
}
2269+
.addOnFailureListener { e ->
2270+
Log.e("BetterViz", "Anonymous sign-in failed", e)
2271+
initDatabase()
2272+
}
2273+
} else {
2274+
initDatabase()
2275+
}
2276+
22612277
setContent {
22622278
val selectedTheme by viewModel.selectedTheme.collectAsStateWithLifecycle()
22632279
val selectedFont by viewModel.selectedFont.collectAsStateWithLifecycle()
@@ -2689,6 +2705,10 @@ class MainActivity : ComponentActivity() {
26892705
}
26902706
}
26912707

2708+
private fun initDatabase() {
2709+
viewModel.initDatabase()
2710+
}
2711+
26922712
override fun onNewIntent(intent: Intent) {
26932713
super.onNewIntent(intent)
26942714
setIntent(intent)

0 commit comments

Comments
 (0)