Commit 78062d2a authored by Konstantin Tskhovrebov's avatar Konstantin Tskhovrebov 🤖
Browse files

Open server scope after app launch.

parent 4359b714
......@@ -7,10 +7,8 @@ import com.jakewharton.threetenabp.AndroidThreeTen
import io.fabric.sdk.android.Fabric
import ru.noties.markwon.SpannableConfiguration
import ru.noties.markwon.spans.SpannableTheme
import ru.terrakok.gitlabclient.model.data.auth.AuthHolder
import ru.terrakok.gitlabclient.toothpick.DI
import ru.terrakok.gitlabclient.toothpick.module.AppModule
import ru.terrakok.gitlabclient.toothpick.module.ServerModule
import timber.log.Timber
import toothpick.Toothpick
import toothpick.configuration.Configuration
......@@ -62,13 +60,8 @@ class App : Application() {
}
private fun initAppScope() {
val appScope = Toothpick.openScope(DI.APP_SCOPE)
appScope.installModules(AppModule(this))
//By default we need init ServerScope for launch app
val authHolder = appScope.getInstance(AuthHolder::class.java)
val serverScope = Toothpick.openScopes(DI.APP_SCOPE, DI.SERVER_SCOPE)
serverScope.installModules(ServerModule(authHolder.serverPath))
Toothpick.openScope(DI.APP_SCOPE)
.installModules(AppModule(this))
}
private fun initMarkwon() {
......
......@@ -45,8 +45,6 @@ class AuthInteractor(
fun checkOAuthRedirect(url: String) = url.indexOf(oauthParams.redirectUrl) == 0
fun isSignedIn() = authRepository.isSignedIn
fun login(oauthRedirect: String) =
Completable.defer {
if (oauthRedirect.contains(hash)) {
......
package ru.terrakok.gitlabclient.model.interactor.launch
import ru.terrakok.gitlabclient.model.repository.app.AppInfoRepository
import ru.terrakok.gitlabclient.model.repository.launch.LaunchRepository
import ru.terrakok.gitlabclient.toothpick.DI
import ru.terrakok.gitlabclient.toothpick.module.ServerModule
import toothpick.Toothpick
import javax.inject.Inject
/**
* Created by Konstantin Tskhovrebov (aka @terrakok) on 26.09.18.
*/
class LaunchInteractor @Inject constructor(
private val appInfoRepository: AppInfoRepository
private val appInfoRepository: AppInfoRepository,
private val launchRepository: LaunchRepository
) {
val isFirstLaunch: Boolean
get() {
......@@ -19,4 +24,12 @@ class LaunchInteractor @Inject constructor(
return false
}
}
fun signInToSession(): Boolean {
Toothpick.closeScope(DI.SERVER_SCOPE)
Toothpick
.openScopes(DI.APP_SCOPE, DI.SERVER_SCOPE)
.installModules(ServerModule(launchRepository.getSelectedServerPath()))
return launchRepository.isSignedIn
}
}
\ No newline at end of file
......@@ -16,8 +16,6 @@ class AuthRepository @Inject constructor(
@DefaultServerPath private val defaultServerPath: String
) {
val isSignedIn get() = !authData.token.isNullOrEmpty()
fun requestOAuthToken(
appId: String,
appKey: String,
......
package ru.terrakok.gitlabclient.model.repository.launch
import ru.terrakok.gitlabclient.model.data.storage.Prefs
import javax.inject.Inject
class LaunchRepository @Inject constructor(
private val prefs: Prefs
) {
val isSignedIn get() = prefs.token != null
fun getSelectedServerPath() = prefs.serverPath
}
\ No newline at end of file
......@@ -4,7 +4,6 @@ import com.arellomobile.mvp.MvpPresenter
import com.arellomobile.mvp.MvpView
import ru.terrakok.cicerone.Router
import ru.terrakok.gitlabclient.Screens
import ru.terrakok.gitlabclient.model.interactor.auth.AuthInteractor
import ru.terrakok.gitlabclient.model.interactor.launch.LaunchInteractor
import javax.inject.Inject
......@@ -12,14 +11,13 @@ import javax.inject.Inject
* Created by Konstantin Tskhovrebov (aka @terrakok) on 03.09.18.
*/
class AppPresenter @Inject constructor(
private val router: Router,
private val authInteractor: AuthInteractor,
private val launchInteractor: LaunchInteractor
private val launchInteractor: LaunchInteractor,
private val router: Router
) : MvpPresenter<MvpView>() {
fun coldStart() {
val rootScreen =
if (authInteractor.isSignedIn()) Screens.DrawerFlow
if (launchInteractor.signInToSession()) Screens.DrawerFlow
else Screens.AuthFlow
if (launchInteractor.isFirstLaunch) {
......
......@@ -2,14 +2,12 @@ package ru.terrakok.gitlabclient.toothpick.module
import android.content.Context
import android.content.res.AssetManager
import com.google.gson.Gson
import ru.terrakok.cicerone.Cicerone
import ru.terrakok.cicerone.NavigatorHolder
import ru.terrakok.cicerone.Router
import ru.terrakok.gitlabclient.BuildConfig
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.entity.app.develop.AppInfo
import ru.terrakok.gitlabclient.model.data.auth.AuthHolder
import ru.terrakok.gitlabclient.model.data.storage.Prefs
import ru.terrakok.gitlabclient.model.data.storage.RawAppData
import ru.terrakok.gitlabclient.model.interactor.app.AppInfoInteractor
import ru.terrakok.gitlabclient.model.repository.app.AppInfoRepository
......@@ -19,7 +17,11 @@ import ru.terrakok.gitlabclient.model.system.ResourceManager
import ru.terrakok.gitlabclient.model.system.SchedulersProvider
import ru.terrakok.gitlabclient.model.system.message.SystemMessageNotifier
import ru.terrakok.gitlabclient.toothpick.PrimitiveWrapper
import ru.terrakok.gitlabclient.toothpick.qualifier.*
import ru.terrakok.gitlabclient.toothpick.provider.GsonProvider
import ru.terrakok.gitlabclient.toothpick.qualifier.AppDevelopersPath
import ru.terrakok.gitlabclient.toothpick.qualifier.CacheLifetime
import ru.terrakok.gitlabclient.toothpick.qualifier.DefaultPageSize
import ru.terrakok.gitlabclient.toothpick.qualifier.DefaultServerPath
import toothpick.config.Module
/**
......@@ -39,15 +41,13 @@ class AppModule(context: Context) : Module() {
bind(AssetManager::class.java).toInstance(context.assets)
bind(RawAppData::class.java)
bind(SystemMessageNotifier::class.java).toInstance(SystemMessageNotifier())
bind(Gson::class.java).toProvider(GsonProvider::class.java).providesSingletonInScope()
//Navigation
val cicerone = Cicerone.create()
bind(Router::class.java).toInstance(cicerone.router)
bind(NavigatorHolder::class.java).toInstance(cicerone.navigatorHolder)
//Auth
bind(AuthHolder::class.java).to(Prefs::class.java).singletonInScope()
//AppInfo
bind(AppInfo::class.java).toInstance(
AppInfo(
......
package ru.terrakok.gitlabclient.toothpick.module
import com.google.gson.Gson
import okhttp3.OkHttpClient
import ru.terrakok.gitlabclient.BuildConfig
import ru.terrakok.gitlabclient.model.data.auth.AuthHolder
import ru.terrakok.gitlabclient.model.data.cache.ProjectCache
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
import ru.terrakok.gitlabclient.model.data.server.MarkDownUrlResolver
import ru.terrakok.gitlabclient.model.data.storage.Prefs
import ru.terrakok.gitlabclient.model.interactor.auth.AuthInteractor
import ru.terrakok.gitlabclient.model.interactor.auth.OAuthParams
import ru.terrakok.gitlabclient.model.interactor.event.EventInteractor
......@@ -26,7 +27,6 @@ import ru.terrakok.gitlabclient.model.repository.user.UserRepository
import ru.terrakok.gitlabclient.presentation.global.ErrorHandler
import ru.terrakok.gitlabclient.presentation.global.MarkDownConverter
import ru.terrakok.gitlabclient.toothpick.provider.ApiProvider
import ru.terrakok.gitlabclient.toothpick.provider.GsonProvider
import ru.terrakok.gitlabclient.toothpick.provider.MarkDownConverterProvider
import ru.terrakok.gitlabclient.toothpick.provider.OkHttpClientProvider
import ru.terrakok.gitlabclient.toothpick.provider.OkHttpClientWithErrorHandlerProvider
......@@ -39,9 +39,12 @@ import toothpick.config.Module
*/
class ServerModule(serverUrl: String) : Module() {
init {
//Auth
bind(AuthHolder::class.java).to(Prefs::class.java).singletonInScope()
//Network
bind(String::class.java).withName(ServerPath::class.java).toInstance(serverUrl)
bind(Gson::class.java).toProvider(GsonProvider::class.java).providesSingletonInScope()
bind(OkHttpClient::class.java).toProvider(OkHttpClientProvider::class.java).providesSingletonInScope()
bind(OkHttpClient::class.java).withName(WithErrorHandler::class.java)
.toProvider(OkHttpClientWithErrorHandlerProvider::class.java)
......
......@@ -13,6 +13,7 @@ import ru.terrakok.cicerone.Navigator
import ru.terrakok.cicerone.NavigatorHolder
import ru.terrakok.cicerone.android.support.SupportAppNavigator
import ru.terrakok.cicerone.commands.Command
import ru.terrakok.gitlabclient.App
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.model.system.message.SystemMessageNotifier
import ru.terrakok.gitlabclient.model.system.message.SystemMessageType
......@@ -26,6 +27,9 @@ import javax.inject.Inject
/**
* Created by Konstantin Tskhovrebov (aka @terrakok) on 03.09.18.
*/
private const val STATE_LAUNCH_FLAG = "state_launch_flag"
class AppActivity : MvpAppCompatActivity(), MvpView {
@InjectPresenter
......@@ -33,7 +37,7 @@ class AppActivity : MvpAppCompatActivity(), MvpView {
@ProvidePresenter
fun providePresenter() =
Toothpick.openScope(DI.SERVER_SCOPE)
Toothpick.openScope(DI.APP_SCOPE)
.getInstance(AppPresenter::class.java)
@Inject
......@@ -62,11 +66,11 @@ class AppActivity : MvpAppCompatActivity(), MvpView {
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme)
Toothpick.inject(this, Toothpick.openScope(DI.SERVER_SCOPE))
Toothpick.inject(this, Toothpick.openScope(DI.APP_SCOPE))
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_container)
if (savedInstanceState == null) {
if (isFirstLaunch(savedInstanceState)) {
presenter.coldStart()
}
}
......@@ -83,6 +87,16 @@ class AppActivity : MvpAppCompatActivity(), MvpView {
super.onPause()
}
private fun isFirstLaunch(savedInstanceState: Bundle?): Boolean {
val savedAppCode = savedInstanceState?.getString(STATE_LAUNCH_FLAG)
return savedAppCode != App.appCode
}
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState)
outState?.putString(STATE_LAUNCH_FLAG, App.appCode)
}
override fun onBackPressed() {
currentFragment?.onBackPressed() ?: super.onBackPressed()
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment