AuthenticationActivity.kt 2.63 KB
Newer Older
1 2
package chat.rocket.android.authentication.ui

3 4
import android.content.Context
import android.content.Intent
5 6 7 8
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import chat.rocket.android.R
9 10
import chat.rocket.android.authentication.domain.model.LoginDeepLinkInfo
import chat.rocket.android.authentication.domain.model.getLoginDeepLinkInfo
11
import chat.rocket.android.authentication.presentation.AuthenticationPresenter
12
import chat.rocket.android.authentication.server.ui.ServerFragment
13
import chat.rocket.android.util.extensions.addFragment
14 15 16 17
import dagger.android.AndroidInjection
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.support.HasSupportFragmentInjector
18 19 20
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
21 22 23
import javax.inject.Inject

class AuthenticationActivity : AppCompatActivity(), HasSupportFragmentInjector {
24
    @Inject lateinit var fragmentDispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>
25
    @Inject lateinit var presenter: AuthenticationPresenter
26
    val job = Job()
27 28

    override fun onCreate(savedInstanceState: Bundle?) {
29
        AndroidInjection.inject(this)
30 31
        setContentView(R.layout.activity_authentication)
        setTheme(R.style.AuthenticationTheme)
32 33
        super.onCreate(savedInstanceState)

34
        val deepLinkInfo = intent.getLoginDeepLinkInfo()
35 36
        launch(UI + job) {
            val newServer = intent.getBooleanExtra(INTENT_ADD_NEW_SERVER, false)
37
            // if we got authenticateWithDeepLink information, pass true to newServer also
38
            presenter.loadCredentials(newServer || deepLinkInfo != null) { authenticated ->
39
                if (!authenticated) {
40
                    showServerInput(savedInstanceState, deepLinkInfo)
41
                }
42
            }
43 44 45
        }
    }

46 47 48 49 50
    override fun onDestroy() {
        job.cancel()
        super.onDestroy()
    }

51 52 53
    override fun supportFragmentInjector(): AndroidInjector<Fragment> {
        return fragmentDispatchingAndroidInjector
    }
54

55
    fun showServerInput(savedInstanceState: Bundle?, deepLinkInfo: LoginDeepLinkInfo?) {
56
        addFragment("ServerFragment", R.id.fragment_container) {
57
            ServerFragment.newInstance(deepLinkInfo)
58 59
        }
    }
60 61 62 63 64 65 66 67 68
}

const val INTENT_ADD_NEW_SERVER = "INTENT_ADD_NEW_SERVER"

fun Context.newServerIntent(): Intent {
    return Intent(this, AuthenticationActivity::class.java).apply {
        putExtra(INTENT_ADD_NEW_SERVER, true)
        flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
69
}