Commit a5d873c7 authored by Konstantin Tskhovrebov's avatar Konstantin Tskhovrebov 🤖

refactored BaseActivity

parent 81e7b1dd
package ru.terrakok.gitlabclient
import android.content.Context
import android.content.Intent
import ru.terrakok.gitlabclient.ui.auth.AuthActivity
import ru.terrakok.gitlabclient.ui.issue.IssueActivity
import ru.terrakok.gitlabclient.ui.launch.MainActivity
import ru.terrakok.gitlabclient.ui.mergerequest.MergeRequestActivity
import ru.terrakok.gitlabclient.ui.project.ProjectActivity
import ru.terrakok.gitlabclient.ui.user.UserActivity
/**
* @author Konstantin Tskhovrebov (aka terrakok) on 26.03.17.
*/
......@@ -24,4 +33,20 @@ object Screens {
const val ISSUE_FLOW = "issue flow"
const val ISSUE_INFO_SCREEN = "issue info screen"
fun getFlowIntent(context: Context, flowKey: String, data: Any?): Intent? = when (flowKey) {
Screens.AUTH_FLOW -> AuthActivity.getStartIntent(context)
Screens.MAIN_FLOW -> MainActivity.getStartIntent(context)
Screens.PROJECT_FLOW -> ProjectActivity.getStartIntent(data as Long, context)
Screens.USER_FLOW -> UserActivity.getStartIntent(data as Long, context)
Screens.MR_FLOW -> {
val (projectId, mrId) = data as Pair<Long, Long>
MergeRequestActivity.getStartIntent(projectId, mrId, context)
}
Screens.ISSUE_FLOW -> {
val (projectId, issueId) = data as Pair<Long, Long>
IssueActivity.getStartIntent(projectId, issueId, context)
}
else -> null
}
}
\ No newline at end of file
......@@ -4,21 +4,17 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import ru.terrakok.cicerone.NavigatorHolder
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.Screens
import ru.terrakok.gitlabclient.model.system.flow.FlowNavigator
import ru.terrakok.gitlabclient.toothpick.DI
import ru.terrakok.gitlabclient.ui.global.BaseActivity
import toothpick.Toothpick
import javax.inject.Inject
/**
* Created by Konstantin Tskhovrebov (aka @terrakok) on 29.10.17.
*/
class AuthActivity : BaseActivity() {
@Inject lateinit var navigationHolder: NavigatorHolder
override val layoutRes = R.layout.activity_container
override fun onCreate(savedInstanceState: Bundle?) {
......@@ -30,19 +26,10 @@ class AuthActivity : BaseActivity() {
}
}
override fun onResumeFragments() {
super.onResumeFragments()
navigationHolder.setNavigator(navigator)
}
override fun onPause() {
navigationHolder.removeNavigator()
super.onPause()
}
private val navigator = object : FlowNavigator(this, R.id.container) {
override val navigator = object : FlowNavigator(this, R.id.container) {
override fun createFlowIntent(flowKey: String, data: Any?) = getFlowIntent(flowKey, data)
override fun createFlowIntent(flowKey: String, data: Any?) =
Screens.getFlowIntent(this@AuthActivity, flowKey, data)
override fun createFragment(screenKey: String?, data: Any?): Fragment? = when (screenKey) {
Screens.AUTH_SCREEN -> AuthFragment()
......
package ru.terrakok.gitlabclient.ui.global
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.arellomobile.mvp.MvpAppCompatActivity
import ru.terrakok.gitlabclient.Screens
import ru.terrakok.gitlabclient.ui.auth.AuthActivity
import ru.terrakok.gitlabclient.ui.issue.IssueActivity
import ru.terrakok.gitlabclient.ui.launch.MainActivity
import ru.terrakok.gitlabclient.ui.mergerequest.MergeRequestActivity
import ru.terrakok.gitlabclient.ui.project.ProjectActivity
import ru.terrakok.gitlabclient.ui.user.UserActivity
import ru.terrakok.cicerone.Navigator
import ru.terrakok.cicerone.NavigatorHolder
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper
import javax.inject.Inject
/**
* @author Konstantin Tskhovrebov (aka terrakok) on 26.03.17.
*/
abstract class BaseActivity : MvpAppCompatActivity() {
@Inject lateinit var navigatorHolder: NavigatorHolder
abstract val layoutRes: Int
protected abstract val layoutRes: Int
protected abstract val navigator: Navigator
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase))
......@@ -28,20 +25,14 @@ abstract class BaseActivity : MvpAppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(layoutRes)
}
protected fun getFlowIntent(flowKey: String, data: Any?): Intent? = when (flowKey) {
Screens.AUTH_FLOW -> AuthActivity.getStartIntent(this)
Screens.MAIN_FLOW -> MainActivity.getStartIntent(this)
Screens.PROJECT_FLOW -> ProjectActivity.getStartIntent(data as Long, this)
Screens.USER_FLOW -> UserActivity.getStartIntent(data as Long, this)
Screens.MR_FLOW -> {
val (projectId, mrId) = data as Pair<Long, Long>
MergeRequestActivity.getStartIntent(projectId, mrId, this)
}
Screens.ISSUE_FLOW -> {
val (projectId, issueId) = data as Pair<Long, Long>
IssueActivity.getStartIntent(projectId, issueId, this)
}
else -> null
override fun onResumeFragments() {
super.onResumeFragments()
navigatorHolder.setNavigator(navigator)
}
override fun onPause() {
navigatorHolder.removeNavigator()
super.onPause()
}
}
\ No newline at end of file
......@@ -26,8 +26,8 @@ abstract class BaseFragment : MvpAppCompatFragment() {
open protected fun restoreState(state: Bundle) {}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?)
= inflater.inflate(layoutRes, container, false)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
inflater.inflate(layoutRes, container, false)
protected fun showProgressDialog(progress: Boolean) {
if (!isAdded) return
......
......@@ -3,7 +3,6 @@ package ru.terrakok.gitlabclient.ui.issue
import android.content.Context
import android.content.Intent
import android.os.Bundle
import ru.terrakok.cicerone.NavigatorHolder
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.Screens
import ru.terrakok.gitlabclient.model.system.flow.FlowNavigator
......@@ -14,14 +13,11 @@ import ru.terrakok.gitlabclient.toothpick.qualifier.ProjectId
import ru.terrakok.gitlabclient.ui.global.BaseActivity
import toothpick.Toothpick
import toothpick.config.Module
import javax.inject.Inject
/**
* Created by Konstantin Tskhovrebov (aka @terrakok) on 25.11.17.
*/
class IssueActivity : BaseActivity() {
@Inject lateinit var navigationHolder: NavigatorHolder
override val layoutRes = R.layout.activity_container
private val issueId get() = intent.getLongExtra(ARG_ISSUE_ID, 0)
private val projectId get() = intent.getLongExtra(ARG_PROJECT_ID, 0)
......@@ -48,23 +44,13 @@ class IssueActivity : BaseActivity() {
}
}
override fun onResumeFragments() {
super.onResumeFragments()
navigationHolder.setNavigator(navigator)
}
override fun onPause() {
navigationHolder.removeNavigator()
super.onPause()
}
override fun onDestroy() {
super.onDestroy()
if (isFinishing) Toothpick.closeScope(DI.ISSUE_SCOPE)
}
private val navigator = object : FlowNavigator(this, R.id.container) {
override val navigator = object : FlowNavigator(this, R.id.container) {
override fun createFragment(screenKey: String?, data: Any?) = when(screenKey) {
Screens.ISSUE_INFO_SCREEN -> IssueInfoFragment()
......
......@@ -10,7 +10,6 @@ import com.arellomobile.mvp.presenter.InjectPresenter
import com.arellomobile.mvp.presenter.ProvidePresenter
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_main.*
import ru.terrakok.cicerone.NavigatorHolder
import ru.terrakok.cicerone.commands.Command
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.Screens
......@@ -32,7 +31,6 @@ import toothpick.Toothpick
import javax.inject.Inject
class MainActivity : BaseActivity(), LaunchView {
@Inject lateinit var navigationHolder: NavigatorHolder
@Inject lateinit var menuController: GlobalMenuController
private var menuStateDisposable: Disposable? = null
......@@ -45,7 +43,8 @@ class MainActivity : BaseActivity(), LaunchView {
private val drawerFragment
get() = supportFragmentManager.findFragmentById(R.id.navDrawerContainer) as NavigationDrawerFragment?
@InjectPresenter lateinit var presenter: LaunchPresenter
@InjectPresenter
lateinit var presenter: LaunchPresenter
@ProvidePresenter
fun providePresenter(): LaunchPresenter {
......@@ -68,12 +67,10 @@ class MainActivity : BaseActivity(), LaunchView {
override fun onResumeFragments() {
super.onResumeFragments()
menuStateDisposable = menuController.state.subscribe { openNavDrawer(it) }
navigationHolder.setNavigator(navigator)
}
override fun onPause() {
menuStateDisposable?.dispose()
navigationHolder.removeNavigator()
super.onPause()
}
......@@ -91,14 +88,15 @@ class MainActivity : BaseActivity(), LaunchView {
updateNavDrawer()
}
private val navigator = object : FlowNavigator(this, R.id.mainContainer) {
override val navigator = object : FlowNavigator(this, R.id.mainContainer) {
override fun applyCommands(commands: Array<out Command>?) {
super.applyCommands(commands)
updateNavDrawer()
}
override fun createFlowIntent(flowKey: String, data: Any?) = getFlowIntent(flowKey, data)
override fun createFlowIntent(flowKey: String, data: Any?) =
Screens.getFlowIntent(this@MainActivity, flowKey, data)
override fun createFragment(screenKey: String?, data: Any?): Fragment? = when (screenKey) {
Screens.MAIN_SCREEN -> MainFragment()
......
......@@ -3,7 +3,6 @@ package ru.terrakok.gitlabclient.ui.mergerequest
import android.content.Context
import android.content.Intent
import android.os.Bundle
import ru.terrakok.cicerone.NavigatorHolder
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.Screens
import ru.terrakok.gitlabclient.model.system.flow.FlowNavigator
......@@ -14,13 +13,11 @@ import ru.terrakok.gitlabclient.toothpick.qualifier.ProjectId
import ru.terrakok.gitlabclient.ui.global.BaseActivity
import toothpick.Toothpick
import toothpick.config.Module
import javax.inject.Inject
/**
* Created by Konstantin Tskhovrebov (aka @terrakok) on 25.11.17.
*/
class MergeRequestActivity : BaseActivity() {
@Inject lateinit var navigationHolder: NavigatorHolder
override val layoutRes = R.layout.activity_container
private val mrId get() = intent.getLongExtra(ARG_MR_ID, 0)
......@@ -48,23 +45,13 @@ class MergeRequestActivity : BaseActivity() {
}
}
override fun onResumeFragments() {
super.onResumeFragments()
navigationHolder.setNavigator(navigator)
}
override fun onPause() {
navigationHolder.removeNavigator()
super.onPause()
}
override fun onDestroy() {
super.onDestroy()
if (isFinishing) Toothpick.closeScope(DI.MERGE_REQUEST_SCOPE)
}
private val navigator = object : FlowNavigator(this, R.id.container) {
override val navigator = object : FlowNavigator(this, R.id.container) {
override fun createFragment(screenKey: String?, data: Any?) = when(screenKey) {
Screens.MR_SCREEN -> MergeRequestFragment()
......
......@@ -4,7 +4,6 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import ru.terrakok.cicerone.NavigatorHolder
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.Screens
import ru.terrakok.gitlabclient.model.system.flow.FlowNavigator
......@@ -15,13 +14,11 @@ import ru.terrakok.gitlabclient.ui.global.BaseActivity
import ru.terrakok.gitlabclient.ui.project.info.ProjectInfoFragment
import toothpick.Toothpick
import toothpick.config.Module
import javax.inject.Inject
/**
* Created by Konstantin Tskhovrebov (aka @terrakok) on 25.11.17.
*/
class ProjectActivity : BaseActivity() {
@Inject lateinit var navigationHolder: NavigatorHolder
override val layoutRes = R.layout.activity_container
private val projectId get() = intent.getLongExtra(ARG_PROJECT_ID, 0)
......@@ -45,23 +42,13 @@ class ProjectActivity : BaseActivity() {
}
}
override fun onResumeFragments() {
super.onResumeFragments()
navigationHolder.setNavigator(navigator)
}
override fun onPause() {
navigationHolder.removeNavigator()
super.onPause()
}
override fun onDestroy() {
super.onDestroy()
if (isFinishing) Toothpick.closeScope(DI.PROJECT_SCOPE)
}
private val navigator = object : FlowNavigator(this, R.id.container) {
override val navigator = object : FlowNavigator(this, R.id.container) {
override fun createFragment(screenKey: String?, data: Any?): Fragment? = when (screenKey) {
Screens.PROJECT_INFO_SCREEN -> ProjectInfoFragment()
......
......@@ -4,7 +4,6 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import ru.terrakok.cicerone.NavigatorHolder
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.Screens
import ru.terrakok.gitlabclient.model.system.flow.FlowNavigator
......@@ -15,13 +14,11 @@ import ru.terrakok.gitlabclient.ui.global.BaseActivity
import ru.terrakok.gitlabclient.ui.user.info.UserInfoFragment
import toothpick.Toothpick
import toothpick.config.Module
import javax.inject.Inject
/**
* Created by Konstantin Tskhovrebov (aka @terrakok) on 25.11.17.
*/
class UserActivity : BaseActivity() {
@Inject lateinit var navigationHolder: NavigatorHolder
override val layoutRes = R.layout.activity_container
private val userId get() = intent.getLongExtra(ARG_USER_ID, 0)
......@@ -45,23 +42,13 @@ class UserActivity : BaseActivity() {
}
}
override fun onResumeFragments() {
super.onResumeFragments()
navigationHolder.setNavigator(navigator)
}
override fun onPause() {
navigationHolder.removeNavigator()
super.onPause()
}
override fun onDestroy() {
super.onDestroy()
if (isFinishing) Toothpick.closeScope(DI.USER_SCOPE)
}
private val navigator = object : FlowNavigator(this, R.id.container) {
override val navigator = object : FlowNavigator(this, R.id.container) {
override fun createFragment(screenKey: String?, data: Any?): Fragment? = when (screenKey) {
Screens.USER_INFO_SCREEN -> UserInfoFragment()
......
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