feat: Use global instances for tokenManager & authRepository
This commit is contained in:
parent
07620e7875
commit
29f63d85c1
|
@ -16,7 +16,7 @@ import kotlinx.coroutines.Dispatchers
|
|||
import kotlinx.coroutines.launch
|
||||
|
||||
class LoginActivity : AppCompatActivity() {
|
||||
private lateinit var authRepository: AuthRepository
|
||||
private val authRepository by lazy { (application as MyApplication).authRepository }
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
@ -28,9 +28,6 @@ class LoginActivity : AppCompatActivity() {
|
|||
insets
|
||||
}
|
||||
|
||||
val tokenManager = TokenManager(this)
|
||||
authRepository = AuthRepository(tokenManager)
|
||||
|
||||
val loginButton = findViewById<Button>(R.id.loginButton)
|
||||
val registerButton = findViewById<Button>(R.id.registerButton)
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ class MainActivity : AppCompatActivity() {
|
|||
|
||||
private var apiReachable = false
|
||||
private var loggedIn = false
|
||||
private lateinit var authRepository: AuthRepository
|
||||
private val authRepository by lazy { (application as MyApplication).authRepository }
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
// Attach the splash screen to the activity before initialization begins
|
||||
|
@ -32,9 +32,6 @@ class MainActivity : AppCompatActivity() {
|
|||
enableEdgeToEdge()
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
var tokenManager = TokenManager(this)
|
||||
authRepository = AuthRepository(tokenManager)
|
||||
|
||||
// Perform initialization (api reachability check & login)
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
initialize()
|
||||
|
|
|
@ -2,12 +2,24 @@ package com.p_vacho.neat_calendar
|
|||
|
||||
import android.app.Application
|
||||
import com.p_vacho.neat_calendar.api.RetrofitClient
|
||||
import com.p_vacho.neat_calendar.auth.AuthRepository
|
||||
import com.p_vacho.neat_calendar.auth.TokenManager
|
||||
|
||||
class MyApplication : Application() {
|
||||
lateinit var tokenManager: TokenManager
|
||||
private set
|
||||
|
||||
lateinit var authRepository: AuthRepository
|
||||
private set
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
// Initialize RetrofitClient
|
||||
// Initialize RetrofitClient (mutating the global object)
|
||||
RetrofitClient.initialize(this)
|
||||
|
||||
// Initialize TokenManager and AuthRepository globally
|
||||
tokenManager = TokenManager(this)
|
||||
authRepository = AuthRepository(tokenManager)
|
||||
}
|
||||
}
|
|
@ -7,18 +7,24 @@ import com.p_vacho.neat_calendar.LoginActivity
|
|||
import com.p_vacho.neat_calendar.auth.AuthRepository
|
||||
import com.p_vacho.neat_calendar.auth.TokenManager
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.p_vacho.neat_calendar.MyApplication
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Protocol
|
||||
import okhttp3.Response
|
||||
import okhttp3.ResponseBody
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
|
||||
class AuthInterceptor(
|
||||
private val context: Context,
|
||||
) : Interceptor {
|
||||
|
||||
private val tokenManager: TokenManager = TokenManager(context)
|
||||
private val authRepository: AuthRepository = AuthRepository(tokenManager)
|
||||
|
||||
private val tokenManager: TokenManager by lazy {
|
||||
(context.applicationContext as MyApplication).tokenManager
|
||||
}
|
||||
private val authRepository: AuthRepository by lazy {
|
||||
(context.applicationContext as MyApplication).authRepository
|
||||
}
|
||||
|
||||
companion object {
|
||||
// List of URLs to bypass in the interceptor
|
||||
|
|
Loading…
Reference in a new issue