Skip to content

Commit

Permalink
Revert "Use explicit backing fields"
Browse files Browse the repository at this point in the history
This reverts commit a911ea4.

My IDE is giving a lot of incorrect errors some of the time, which is quite distracting, so I guess I was a little early in adopting this feature.

# Conflicts:
#	app/src/main/java/nl/ovfietsbeschikbaarheid/viewmodel/DetailsViewModel.kt
  • Loading branch information
cristan committed Oct 27, 2024
1 parent 664e268 commit 13da1e5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
6 changes: 0 additions & 6 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ android {
}
}

kotlin {
sourceSets.all {
languageSettings.enableLanguageFeature("ExplicitBackingFields")
}
}

dependencies {

implementation(libs.androidx.core.ktx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ class DetailsViewModel(
private val stationRepository: StationRepository
) : ViewModel() {

val screenState: State<ScreenState<DetailsContent>>
field = mutableStateOf<ScreenState<DetailsContent>>(ScreenState.Loading)
private val _screenState = mutableStateOf<ScreenState<DetailsContent>>(ScreenState.Loading)
val screenState: State<ScreenState<DetailsContent>> = _screenState

val title: State<String>
field = mutableStateOf("")
private val _title = mutableStateOf("")
val title: State<String> = _title

private lateinit var overviewModel: LocationOverviewModel

fun setLocationCode(locationCode: String) {
overviewModel = overviewRepository.getAllLocations().find { it.locationCode == locationCode }!!
title.value = overviewModel.title
_title.value = overviewModel.title
viewModelScope.launch {
doRefresh()
}
}

fun onReturnToScreenTriggered() {
if (screenState.value is ScreenState.Loaded) {
screenState.setRefreshing()
_screenState.setRefreshing()
viewModelScope.launch {
doRefresh()
}
Expand All @@ -54,13 +54,13 @@ class DetailsViewModel(

fun onPullToRefresh() {
viewModelScope.launch {
screenState.setRefreshing()
_screenState.setRefreshing()
doRefresh(MIN_REFRESH_TIME)
}
}

fun onRetryClick() {
screenState.value = ScreenState.Loading
_screenState.value = ScreenState.Loading
viewModelScope.launch {
doRefresh()
}
Expand All @@ -73,7 +73,7 @@ class DetailsViewModel(
if (details == null) {
val fetchTimeInstant = Instant.ofEpochSecond(1719066494)
val lastFetched = LocalDateTime.ofInstant(fetchTimeInstant, ZoneId.of("Europe/Amsterdam"))!!
screenState.value = ScreenState.Loaded(DetailsContent.NotFound(overviewModel.title, lastFetched))
_screenState.value = ScreenState.Loaded(DetailsContent.NotFound(overviewModel.title, lastFetched))
return
}
val allStations = stationRepository.getAllStations()
Expand All @@ -83,10 +83,10 @@ class DetailsViewModel(
if (timeElapsed < minDelay) {
delay(minDelay - timeElapsed)
}
screenState.value = ScreenState.Loaded(DetailsContent.Content(data))
_screenState.value = ScreenState.Loaded(DetailsContent.Content(data))
} catch (e: Exception) {
Timber.e(e)
screenState.value = ScreenState.FullPageError
_screenState.value = ScreenState.FullPageError
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ class HomeViewModel(
private val locationLoader: LocationLoader
) : ViewModel() {

val searchTerm: State<String>
field = mutableStateOf("")
private val _searchTerm = mutableStateOf("")
val searchTerm: State<String> = _searchTerm

val content: State<HomeContent>
// The initial value doesn't really matter: it gets overwritten right away anyway
field = mutableStateOf<HomeContent>(HomeContent.InitialEmpty)
// The initial value doesn't really matter: it gets overwritten right away anyway
private val _content = mutableStateOf<HomeContent>(HomeContent.InitialEmpty)
val content: State<HomeContent> = _content

private var geoCoderJob: Job? = null

fun screenLaunched() {
Timber.d("screenLaunched called")
loadData(searchTerm.value)
loadData(_searchTerm.value)
}

fun onReturnedToScreen() {
val currentlyShown = content.value
val currentlyShown = _content.value
when {
currentlyShown is HomeContent.GpsTurnedOff && locationPermissionHelper.isGpsTurnedOn() -> {
// The GPS is on now
Expand All @@ -59,21 +59,21 @@ class HomeViewModel(
&& currentlyShown.state == AskPermissionState.DeniedPermanently
&& LocationPermissionController.mobile().hasPermission() -> {
// The user went to the app settings and granted the location permission manually
content.value = HomeContent.LoadingGpsLocation
_content.value = HomeContent.LoadingGpsLocation
fetchLocation()
}

currentlyShown is HomeContent.GpsContent -> {
// Do basically a pull to refresh when re-entering this screen
content.value = currentlyShown.copy(isRefreshing = true)
_content.value = currentlyShown.copy(isRefreshing = true)
fetchLocation()
}
}
}

fun refreshGps() {
Timber.d("refreshGps called")
content.value = (content.value as HomeContent.GpsContent).copy(isRefreshing = true)
_content.value = (_content.value as HomeContent.GpsContent).copy(isRefreshing = true)
fetchLocation()
}

Expand All @@ -99,20 +99,20 @@ class HomeViewModel(
// Doesn't happen on Android
PermissionState.NotDetermined -> Unit
PermissionState.Denied -> {
content.value = HomeContent.AskGpsPermission(AskPermissionState.Denied)
_content.value = HomeContent.AskGpsPermission(AskPermissionState.Denied)
}
PermissionState.DeniedForever -> {
content.value = HomeContent.AskGpsPermission(AskPermissionState.DeniedPermanently)
_content.value = HomeContent.AskGpsPermission(AskPermissionState.DeniedPermanently)
}
PermissionState.Granted -> {
content.value = HomeContent.LoadingGpsLocation
_content.value = HomeContent.LoadingGpsLocation
fetchLocation()
}
}
}

fun onSearchTermChanged(searchTerm: String) {
this.searchTerm.value = searchTerm
this._searchTerm.value = searchTerm
loadData(searchTerm)
}

Expand All @@ -121,21 +121,21 @@ class HomeViewModel(
loadLocation()
} else {
val filteredLocations = overviewRepository.getLocations(searchTerm)
val currentContent = content.value
val currentContent = _content.value
if (currentContent is HomeContent.SearchTermContent) {
// Update the search results right away, but keep the nearby locations and update them in another thread to avoid flicker
content.value = currentContent.copy(locations = filteredLocations)
_content.value = currentContent.copy(locations = filteredLocations)
} else {
content.value = HomeContent.SearchTermContent(filteredLocations, searchTerm, nearbyLocations = null)
_content.value = HomeContent.SearchTermContent(filteredLocations, searchTerm, nearbyLocations = null)
}

geoCoderJob?.cancel()
geoCoderJob = viewModelScope.launch {
val nearbyLocations = findNearbyLocations(searchTerm)
if (nearbyLocations == null && filteredLocations.isEmpty()) {
content.value = HomeContent.NoSearchResults(searchTerm)
_content.value = HomeContent.NoSearchResults(searchTerm)
} else {
content.value = HomeContent.SearchTermContent(filteredLocations, searchTerm, nearbyLocations)
_content.value = HomeContent.SearchTermContent(filteredLocations, searchTerm, nearbyLocations)
}
}
}
Expand All @@ -162,13 +162,13 @@ class HomeViewModel(
*/
private fun loadLocation() {
if (!locationPermissionHelper.isGpsTurnedOn()) {
content.value = HomeContent.GpsTurnedOff
_content.value = HomeContent.GpsTurnedOff
} else if (!LocationPermissionController.mobile().hasPermission()) {
val showRationale = locationPermissionHelper.shouldShowLocationRationale()
val state = if (!showRationale) AskPermissionState.Initial else AskPermissionState.Denied
content.value = HomeContent.AskGpsPermission(state)
_content.value = HomeContent.AskGpsPermission(state)
} else {
content.value = HomeContent.LoadingGpsLocation
_content.value = HomeContent.LoadingGpsLocation
fetchLocation()
}
}
Expand All @@ -184,11 +184,11 @@ class HomeViewModel(

val coordinates = locationLoader.loadCurrentCoordinates()
if (coordinates == null) {
content.value = HomeContent.GpsError("Geen locatie gevonden")
_content.value = HomeContent.GpsError("Geen locatie gevonden")
} else {
val locationsWithDistance =
LocationsMapper.withDistance(overviewRepository.getAllLocations(), coordinates)
content.value = HomeContent.GpsContent(locationsWithDistance)
_content.value = HomeContent.GpsContent(locationsWithDistance)
}
}
}
Expand Down

0 comments on commit 13da1e5

Please sign in to comment.