Skip to content

Commit eb58bb7

Browse files
authored
Merge pull request #124 from harrydbarnes/fix-onboarding-lint-cleanup-14372390311242845095
Fix onboarding lint cleanup 14372390311242845095
2 parents 6bb5d6a + fc3b836 commit eb58bb7

File tree

9 files changed

+73
-202
lines changed

9 files changed

+73
-202
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ local.properties
1313
*.jks
1414
*.keystore
1515
android-sdk/
16+
17+
# Logs
18+
*.log

app/src/main/AndroidManifest.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
34

45
<uses-permission android:name="android.permission.INTERNET" />
56
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
@@ -41,6 +42,17 @@
4142
</intent-filter>
4243
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" />
4344
</receiver>
45+
46+
<provider
47+
android:name="androidx.startup.InitializationProvider"
48+
android:authorities="${applicationId}.androidx-startup"
49+
android:exported="false"
50+
tools:node="merge">
51+
<meta-data
52+
android:name="androidx.work.WorkManagerInitializer"
53+
android:value="androidx.startup"
54+
tools:node="remove" />
55+
</provider>
4456
</application>
4557

4658
</manifest>

app/src/main/java/com/example/theloop/OnboardingActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import androidx.activity.ComponentActivity
66
import androidx.activity.compose.setContent
77
import com.example.theloop.ui.screens.OnboardingScreen
88
import com.example.theloop.ui.theme.TheLoopTheme
9+
import dagger.hilt.android.AndroidEntryPoint
910

11+
@AndroidEntryPoint
1012
class OnboardingActivity : ComponentActivity() {
1113
override fun onCreate(savedInstanceState: Bundle?) {
1214
super.onCreate(savedInstanceState)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.example.theloop
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.viewModelScope
5+
import com.example.theloop.data.repository.UserPreferencesRepository
6+
import dagger.hilt.android.lifecycle.HiltViewModel
7+
import kotlinx.coroutines.flow.MutableStateFlow
8+
import kotlinx.coroutines.flow.StateFlow
9+
import kotlinx.coroutines.flow.asStateFlow
10+
import kotlinx.coroutines.launch
11+
import javax.inject.Inject
12+
13+
@HiltViewModel
14+
class OnboardingViewModel @Inject constructor(
15+
private val userPreferencesRepository: UserPreferencesRepository
16+
) : ViewModel() {
17+
18+
private val _name = MutableStateFlow("")
19+
val name: StateFlow<String> = _name.asStateFlow()
20+
21+
fun onNameChange(newName: String) {
22+
_name.value = newName
23+
}
24+
25+
fun saveName() {
26+
viewModelScope.launch {
27+
userPreferencesRepository.saveUserName(name.value)
28+
}
29+
}
30+
31+
fun completeOnboarding() {
32+
viewModelScope.launch {
33+
userPreferencesRepository.completeOnboarding()
34+
}
35+
}
36+
}

app/src/main/java/com/example/theloop/data/repository/UserPreferencesRepository.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import android.content.SharedPreferences
55
import com.example.theloop.R
66
import com.example.theloop.utils.AppConstants
77
import dagger.hilt.android.qualifiers.ApplicationContext
8+
import kotlinx.coroutines.Dispatchers
9+
import kotlinx.coroutines.withContext
810
import kotlinx.coroutines.channels.awaitClose
911
import kotlinx.coroutines.flow.Flow
1012
import kotlinx.coroutines.flow.callbackFlow
@@ -62,6 +64,14 @@ class UserPreferencesRepository @Inject constructor(
6264
prefs.edit().putString(AppConstants.KEY_SUMMARY_CACHE, summary).apply()
6365
}
6466

67+
suspend fun saveUserName(name: String) = withContext(Dispatchers.IO) {
68+
prefs.edit().putString(AppConstants.KEY_USER_NAME, name).commit()
69+
}
70+
71+
suspend fun completeOnboarding() = withContext(Dispatchers.IO) {
72+
prefs.edit().putBoolean(AppConstants.KEY_ONBOARDING_COMPLETED, true).commit()
73+
}
74+
6575
fun hasLocation(): Boolean {
6676
return prefs.contains(AppConstants.KEY_LATITUDE) && prefs.contains(AppConstants.KEY_LONGITUDE)
6777
}

app/src/main/java/com/example/theloop/ui/components/WeatherCard.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import androidx.compose.ui.Modifier
99
import androidx.compose.ui.res.painterResource
1010
import androidx.compose.ui.res.stringResource
1111
import androidx.compose.ui.unit.dp
12+
import kotlin.math.roundToInt
1213
import com.example.theloop.models.WeatherResponse
1314
import com.example.theloop.utils.AppUtils
1415
import com.example.theloop.R

app/src/main/java/com/example/theloop/ui/screens/OnboardingScreen.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ import androidx.compose.ui.Alignment
1414
import androidx.compose.ui.Modifier
1515
import androidx.compose.ui.platform.LocalContext
1616
import androidx.compose.ui.unit.dp
17+
import androidx.hilt.navigation.compose.hiltViewModel
1718
import androidx.health.connect.client.HealthConnectClient
1819
import androidx.health.connect.client.permission.HealthPermission
1920
import androidx.health.connect.client.records.StepsRecord
20-
import com.example.theloop.utils.AppConstants
21+
import com.example.theloop.OnboardingViewModel
2122

2223
@Composable
2324
fun OnboardingScreen(
24-
onFinish: () -> Unit
25+
onFinish: () -> Unit,
26+
viewModel: OnboardingViewModel = hiltViewModel()
2527
) {
2628
val context = LocalContext.current
2729
var currentStep by remember { mutableIntStateOf(0) }
28-
var name by remember { mutableStateOf("") }
30+
val name by viewModel.name.collectAsState()
2931
val totalSteps = 5
3032

3133
// Permissions state
@@ -97,21 +99,15 @@ fun OnboardingScreen(
9799
if (currentStep == 0) {
98100
// Save Name
99101
if (name.isNotEmpty()) {
100-
context.getSharedPreferences(AppConstants.PREFS_NAME, Context.MODE_PRIVATE)
101-
.edit()
102-
.putString(AppConstants.KEY_USER_NAME, name)
103-
.apply()
102+
viewModel.saveName()
104103
}
105104
}
106105

107106
if (currentStep < totalSteps - 1) {
108107
currentStep++
109108
} else {
110109
// Finish
111-
context.getSharedPreferences(AppConstants.PREFS_NAME, Context.MODE_PRIVATE)
112-
.edit()
113-
.putBoolean(AppConstants.KEY_ONBOARDING_COMPLETED, true)
114-
.apply()
110+
viewModel.completeOnboarding()
115111
onFinish()
116112
}
117113
}) {
@@ -130,7 +126,7 @@ fun OnboardingScreen(
130126
verticalArrangement = Arrangement.Center
131127
) {
132128
when (currentStep) {
133-
0 -> WelcomeStep(name) { name = it }
129+
0 -> WelcomeStep(name, viewModel::onNameChange)
134130
1 -> PermissionStep(
135131
title = "Enable Location",
136132
description = "We need your location to show local weather.",

build.log

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

lint.log

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

0 commit comments

Comments
 (0)