@@ -20,18 +20,10 @@ import com.aurora.gplayapi.helpers.AppDetailsHelper
20
20
import com.aurora.gplayapi.helpers.PurchaseHelper
21
21
import com.aurora.gplayapi.helpers.SearchHelper
22
22
import com.google.gson.Gson
23
- import kotlinx.coroutines.CoroutineScope
24
- import kotlinx.coroutines.Dispatchers
25
- import kotlinx.coroutines.FlowPreview
26
23
import kotlinx.coroutines.flow.catch
27
- import kotlinx.coroutines.flow.debounce
28
- import kotlinx.coroutines.flow.filter
29
24
import kotlinx.coroutines.flow.flow
30
- import kotlinx.coroutines.flow.launchIn
31
- import kotlinx.coroutines.flow.onEach
32
25
33
26
34
- @OptIn(FlowPreview ::class )
35
27
class PlayRepository (
36
28
private val context : Context ,
37
29
private val gson : Gson ,
@@ -41,16 +33,8 @@ class PlayRepository(
41
33
const val AUTH_URL = " https://auroraoss.com/api/auth"
42
34
}
43
35
44
- init {
45
- // TODO: Needs testing.
46
- PlayHttpClient .responseCode
47
- .filter { it == 401 }
48
- .debounce(60 * 5 * 1_000 )
49
- .onEach { runCatching { refreshAuth() }.getOrNull() }
50
- .launchIn(CoroutineScope (Dispatchers .IO ))
51
- }
52
-
53
36
private fun refreshAuth (): AuthData {
37
+ Log .i(" PlayRepository" , " Refreshing token." )
54
38
val properties = NativeDeviceInfoProvider (context).getNativeDeviceProperties()
55
39
val playResponse = PlayHttpClient .postAuth(AUTH_URL , gson.toJson(properties).toByteArray())
56
40
if (playResponse.isSuccessful) {
@@ -66,6 +50,21 @@ class PlayRepository(
66
50
if (savedData.email.isEmpty()) {
67
51
return refreshAuth()
68
52
}
53
+ if (System .currentTimeMillis() - prefs.lastPlayCheck.get() > 60 * 60 * 1_000 ) {
54
+ // Update check time
55
+ prefs.lastPlayCheck.put(System .currentTimeMillis())
56
+ Log .i(" PlayRepository" , " Checking token validity." )
57
+
58
+ // 1h has passed check if token still works
59
+ val app = AppDetailsHelper (savedData)
60
+ .using(PlayHttpClient )
61
+ .getAppByPackageName(" com.google.android.gm" )
62
+
63
+ if (app.packageName.isEmpty()) {
64
+ return refreshAuth()
65
+ }
66
+ Log .i(" PlayRepository" , " Token still valid." )
67
+ }
69
68
return savedData
70
69
}
71
70
@@ -77,7 +76,7 @@ class PlayRepository(
77
76
.using(PlayHttpClient )
78
77
.searchResults(text)
79
78
.appList
80
- .take(5 )
79
+ .take(10 )
81
80
.map { it.toAppUpdate(::getInstallFiles) }
82
81
emit(Result .success(updates))
83
82
} else {
@@ -118,12 +117,11 @@ class PlayRepository(
118
117
.using(PlayHttpClient )
119
118
.purchase(app.packageName, app.versionCode, app.offerType)
120
119
.filter { it.type == File .FileType .BASE || it.type == File .FileType .SPLIT }
121
- .map { it.url }
122
120
123
121
}
124
122
125
123
fun App.toAppUpdate (
126
- getInstallFiles : (App ) -> List <String >,
124
+ getInstallFiles : (App ) -> List <File >,
127
125
oldVersion : String = "",
128
126
oldVersionCode : Long = 0L
129
127
) = AppUpdate (
0 commit comments