Skip to content

Commit

Permalink
resolve cyclic dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
westnordost committed Jun 7, 2024
1 parent 50ccc5d commit 279dea8
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package de.westnordost.streetcomplete

import android.app.Application
import android.content.ComponentCallbacks2
import android.net.ConnectivityManager
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.getSystemService
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
Expand Down Expand Up @@ -36,6 +38,7 @@ import de.westnordost.streetcomplete.data.quest.questModule
import de.westnordost.streetcomplete.data.upload.uploadModule
import de.westnordost.streetcomplete.data.urlconfig.urlConfigModule
import de.westnordost.streetcomplete.data.user.UserLoginController
import de.westnordost.streetcomplete.data.user.UserUpdater
import de.westnordost.streetcomplete.data.user.achievements.achievementsModule
import de.westnordost.streetcomplete.data.user.statistics.statisticsModule
import de.westnordost.streetcomplete.data.user.userModule
Expand Down Expand Up @@ -81,6 +84,7 @@ class StreetCompleteApplication : Application() {
private val editHistoryController: EditHistoryController by inject()
private val userLoginController: UserLoginController by inject()
private val cacheTrimmer: CacheTrimmer by inject()
private val userUpdater: UserUpdater by inject()

private val applicationScope = CoroutineScope(SupervisorJob() + CoroutineName("Application"))

Expand Down Expand Up @@ -151,6 +155,8 @@ class StreetCompleteApplication : Application() {
editHistoryController.deleteSyncedOlderThan(nowAsEpochMilliseconds() - ApplicationConstants.MAX_UNDO_HISTORY_AGE)
}

if (isConnected) userUpdater.update()

enqueuePeriodicCleanupWork()

updateDefaultTheme()
Expand Down Expand Up @@ -222,4 +228,7 @@ class StreetCompleteApplication : Application() {
).setInitialDelay(1, TimeUnit.HOURS).build()
)
}

private val isConnected: Boolean
get() = getSystemService<ConnectivityManager>()?.activeNetworkInfo?.isConnected == true
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@ import de.westnordost.streetcomplete.Prefs
import de.westnordost.streetcomplete.util.Listeners

/** Controller that handles user login, logout, auth and updated data */
class UserDataController(
private val prefs: ObservableSettings,
private val userLoginSource: UserLoginSource
) : UserDataSource {

private val userLoginStatusListener = object : UserLoginSource.Listener {
override fun onLoggedIn() {}
override fun onLoggedOut() {
clear()
}
}
class UserDataController(private val prefs: ObservableSettings) : UserDataSource {

private val listeners = Listeners<UserDataSource.Listener>()

Expand All @@ -30,18 +20,14 @@ class UserDataController(
listeners.forEach { it.onUpdated() }
}

init {
userLoginSource.addListener(userLoginStatusListener)
}

fun setDetails(userDetails: UserDetails) {
prefs.putLong(Prefs.OSM_USER_ID, userDetails.id)
prefs.putString(Prefs.OSM_USER_NAME, userDetails.displayName)
prefs.putInt(Prefs.OSM_UNREAD_MESSAGES, userDetails.unreadMessagesCount)
listeners.forEach { it.onUpdated() }
}

private fun clear() {
fun clear() {
prefs.remove(Prefs.OSM_USER_ID)
prefs.remove(Prefs.OSM_USER_NAME)
prefs.remove(Prefs.OSM_UNREAD_MESSAGES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import de.westnordost.streetcomplete.util.Listeners
class UserLoginController(
private val osmConnection: OsmConnection,
private val prefs: ObservableSettings,
private val userUpdater: UserUpdater,
) : UserLoginSource {

private val listeners = Listeners<UserLoginSource.Listener>()
Expand All @@ -22,7 +21,6 @@ class UserLoginController(
prefs.putString(Prefs.OAUTH2_ACCESS_TOKEN, accessToken)
osmConnection.oAuthAccessToken = accessToken
listeners.forEach { it.onLoggedIn() }
userUpdater.update()
}

fun logOut() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ val OAUTH2_REQUIRED_SCOPES = listOf(
val userModule = module {

single<UserDataSource> { get<UserDataController>() }
single { UserDataController(get(), get()) }
single { UserDataController(get()) }

single<UserLoginSource> { get<UserLoginController>() }
single { UserLoginController(get(), get(), get()) }
single { UserLoginController(get(), get()) }

single { UserUpdater(get(), get(), get(), get(), get()) }
single { UserUpdater(get(), get(), get(), get(), get(), get()) }

single { OAuthService(get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,36 @@ class UserUpdater(
private val userApi: UserApi,
private val avatarsDownloader: AvatarsDownloader,
private val statisticsDownloader: StatisticsDownloader,
private val userController: UserDataController,
private val statisticsController: StatisticsController
private val userDataController: UserDataController,
private val statisticsController: StatisticsController,
private val userLoginSource: UserLoginSource
) {
private val coroutineScope = CoroutineScope(SupervisorJob())

private val userLoginListener = object : UserLoginSource.Listener {
override fun onLoggedIn() {
update()
}
override fun onLoggedOut() {
clear()
}
}

interface Listener {
fun onUserAvatarUpdated()
}
private val userAvatarListeners = Listeners<Listener>()

init {
userLoginSource.addListener(userLoginListener)
}

fun update() = coroutineScope.launch(Dispatchers.IO) {
if (!userLoginSource.isLoggedIn) return@launch
try {
val userDetails = userApi.getMine()

userController.setDetails(userDetails)
userDataController.setDetails(userDetails)
val profileImageUrl = userDetails.profileImageUrl
if (profileImageUrl != null) {
updateAvatar(userDetails.id, profileImageUrl)
Expand All @@ -40,6 +55,11 @@ class UserUpdater(
}
}

fun clear() {
userDataController.clear()
statisticsController.clear()
}

private fun updateAvatar(userId: Long, imageUrl: String) = coroutineScope.launch(Dispatchers.IO) {
avatarsDownloader.download(userId, imageUrl)
userAvatarListeners.forEach { it.onUserAvatarUpdated() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,11 @@ class StatisticsController(
private val currentWeekCountryStatisticsDao: CountryStatisticsDao,
private val activeDatesDao: ActiveDatesDao,
private val countryBoundaries: Lazy<CountryBoundaries>,
private val prefs: ObservableSettings,
userLoginSource: UserLoginSource
private val prefs: ObservableSettings
) : StatisticsSource {

private val listeners = Listeners<StatisticsSource.Listener>()

private val userLoginStatusListener = object : UserLoginSource.Listener {
override fun onLoggedIn() {}
override fun onLoggedOut() {
clear()
}
}

override var rank: Int
get() = prefs.getInt(Prefs.USER_GLOBAL_RANK, -1)
private set(value) {
Expand Down Expand Up @@ -72,10 +64,6 @@ class StatisticsController(
prefs.putLong(Prefs.USER_LAST_TIMESTAMP_ACTIVE, value)
}

init {
userLoginSource.addListener(userLoginStatusListener)
}

override fun getEditCount(): Int =
editTypeStatisticsDao.getTotalAmount()

Expand Down Expand Up @@ -158,7 +146,7 @@ class StatisticsController(
listeners.forEach { it.onUpdatedAll() }
}

private fun clear() {
fun clear() {
editTypeStatisticsDao.clear()
countryStatisticsDao.clear()
currentWeekEditTypeStatisticsDao.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ val statisticsModule = module {
activeDatesDao = get(),
countryBoundaries = get(named("CountryBoundariesLazy")),
prefs = get(),
userLoginSource = get()
) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.res.Configuration
import android.net.ConnectivityManager
import android.os.Bundle
import android.text.Html
import android.text.method.LinkMovementMethod
Expand All @@ -17,7 +16,6 @@ import android.widget.TextView
import android.widget.Toast
import androidx.annotation.AnyThread
import androidx.appcompat.app.AlertDialog
import androidx.core.content.getSystemService
import androidx.core.text.parseAsHtml
import androidx.fragment.app.commit
import androidx.lifecycle.lifecycleScope
Expand All @@ -42,7 +40,6 @@ import de.westnordost.streetcomplete.data.upload.UploadProgressSource
import de.westnordost.streetcomplete.data.upload.VersionBannedException
import de.westnordost.streetcomplete.data.urlconfig.UrlConfigController
import de.westnordost.streetcomplete.data.user.UserLoginController
import de.westnordost.streetcomplete.data.user.UserUpdater
import de.westnordost.streetcomplete.data.visiblequests.QuestPresetsSource
import de.westnordost.streetcomplete.screens.main.MainFragment
import de.westnordost.streetcomplete.screens.main.messages.MessagesContainerFragment
Expand Down Expand Up @@ -70,7 +67,6 @@ class MainActivity :
private val downloadProgressSource: DownloadProgressSource by inject()
private val uploadProgressSource: UploadProgressSource by inject()
private val locationAvailabilityReceiver: LocationAvailabilityReceiver by inject()
private val userUpdater: UserUpdater by inject()
private val elementEditsSource: ElementEditsSource by inject()
private val noteEditsSource: NoteEditsSource by inject()
private val unsyncedChangesCountSource: UnsyncedChangesCountSource by inject()
Expand Down Expand Up @@ -128,9 +124,6 @@ class MainActivity :
add(R.id.fragment_container, TutorialFragment())
}
}
if (userLoginController.isLoggedIn && isConnected) {
userUpdater.update()
}
}

elementEditsSource.addListener(elementEditsListener)
Expand Down Expand Up @@ -239,9 +232,6 @@ class MainActivity :
dontShowRequestAuthorizationAgain = true
}

private val isConnected: Boolean
get() = getSystemService<ConnectivityManager>()?.activeNetworkInfo?.isConnected == true

/* ------------------------------------- Preferences ---------------------------------------- */

private fun updateScreenOn() {
Expand Down

0 comments on commit 279dea8

Please sign in to comment.