Skip to content

Commit

Permalink
Add forcing network when retrieving auth data about user.
Browse files Browse the repository at this point in the history
  • Loading branch information
Garneg committed Jul 21, 2024
1 parent 991d20d commit 5f182cf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/garnegsoft/hubs/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class MainActivity : ComponentActivity() {

val updateMeData = OneTimeWorkRequestBuilder<MeDataUpdateWorker>()
.setConstraints(Constraints(requiredNetworkType = NetworkType.CONNECTED))

.build()
WorkManager.getInstance(this).enqueue(updateMeData)

Expand Down
13 changes: 7 additions & 6 deletions app/src/main/java/com/garnegsoft/hubs/api/me/MeController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@ import com.garnegsoft.hubs.api.HabrApi
import com.garnegsoft.hubs.api.HabrDataParser
import com.garnegsoft.hubs.api.utils.placeholderAvatarUrl
import kotlinx.serialization.Serializable
import okhttp3.CacheControl


class MeController {
companion object {
private fun get(): Result<Me?> {
val response = HabrApi.get("me")
val response = HabrApi.get("me", cacheControl = CacheControl.FORCE_NETWORK)

if (response?.code != 200)
return Result.failure(Exception())
return Result.failure(Exception("Response code was ${response?.code}"))
return response.body?.string()?.let {
if (it == "null") return Result.success(null)
if (it == "null") return Result.success(null) // this means that request were successful, but user is not authorized, so data about them is null
val me = HabrDataParser.parseJson<Me>(it)
me.avatarUrl = me.avatarUrl?.let {
it.replace("//habrastorage", "https://hsto")
} ?: placeholderAvatarUrl(me.alias)
return Result.success(me)
} ?: Result.failure(Exception())
} ?: Result.failure(Exception("Body was null"))
}

fun getMe(): Result<com.garnegsoft.hubs.api.me.Me?> {
val raw = get()
if (raw.isFailure) return Result.failure(Exception())
if (raw.isFailure) return Result.failure(raw.exceptionOrNull()!!)

raw.getOrNull()?.let {
return Result.success(Me(
Expand All @@ -34,7 +35,7 @@ class MeController {
))
}

return Result.failure(Exception())
return Result.failure(Exception("Raw was null"))
}

}
Expand Down
29 changes: 27 additions & 2 deletions app/src/main/java/com/garnegsoft/hubs/api/me/MeDataUpdateWorker.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package com.garnegsoft.hubs.api.me

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import com.garnegsoft.hubs.R
import com.garnegsoft.hubs.api.dataStore.HubsDataStore
import com.garnegsoft.hubs.api.utils.placeholderAvatarUrl
import kotlinx.coroutines.Dispatchers
Expand All @@ -19,6 +27,18 @@ class MeDataUpdateWorker(
params: WorkerParameters
) : CoroutineWorker(appContext, params) {
override suspend fun doWork(): Result {
if (Build.VERSION.SDK_INT >= 26) {

val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel("updateMe", "Обновление данных авторизации", NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)

val notification = Notification.Builder(applicationContext, "updateMe")
.setSmallIcon(R.drawable.notification_default_icon)
.build()
setForeground(ForegroundInfo(0, notification))
}
Log.e("updateMe", "started!")
withContext(Dispatchers.IO) {
val lastAvatarUrl = HubsDataStore.Auth.getValueFlow(
applicationContext,
Expand All @@ -31,6 +51,11 @@ class MeDataUpdateWorker(


val me = MeController.getMe()
if (me.isSuccess)
Log.e("updateMe", "Retrieved data successfully!")
else {
Log.e("updateMe", "Request failed + ${me.exceptionOrNull()?.message}")
}
if (me.isSuccess && me.getOrNull() == null) {
HubsDataStore.Auth.edit(applicationContext, HubsDataStore.Auth.Alias, "")
HubsDataStore.Auth.edit(applicationContext, HubsDataStore.Auth.AvatarFileName, "")
Expand Down Expand Up @@ -64,12 +89,12 @@ class MeDataUpdateWorker(
)
}
}

Log.e("updateMe", "data saved!")
}


}

Log.e("updateMe", "ended!")

return Result.success()
}
Expand Down

0 comments on commit 5f182cf

Please sign in to comment.