Skip to content

Commit

Permalink
Merge pull request #2 from hseapp/feature/HSEOAPI-20
Browse files Browse the repository at this point in the history
Feature/hseoapi 20
  • Loading branch information
RomanAnchugov authored Oct 1, 2020
2 parents d3ee157 + 97d7dd7 commit 454e642
Show file tree
Hide file tree
Showing 19 changed files with 321 additions and 169 deletions.
2 changes: 2 additions & 0 deletions auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ dependencies {

implementation 'com.github.hseapp:HseNetwork:1.0.1'
implementation 'com.github.hseapp:HSECore-Android:0.5.3'

implementation 'net.danlew:android.joda:2.10.6.1'
}
2 changes: 1 addition & 1 deletion auth/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

<application>
<activity android:name=".ui.LoginActivity" />
<activity android:name=".ui.LoginActivity" android:theme="@style/Theme.Transparent"/>

<service android:name=".accountmanager.AuthService">
<intent-filter>
Expand Down
1 change: 1 addition & 0 deletions auth/src/main/java/com/hse/auth/AuthHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.hse.auth

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import com.hse.auth.ui.LoginActivity
import com.hse.auth.ui.LoginBottomSheet
import com.hse.auth.utils.AuthConstants
Expand Down
4 changes: 3 additions & 1 deletion auth/src/main/java/com/hse/auth/models/TokensModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.google.gson.annotations.SerializedName

data class TokensModel(
@SerializedName("access_token") var accessToken: String,
@SerializedName("refresh_token") var refreshToken: String,
@SerializedName("expires_in") var accessExpiresIn: Long,
@SerializedName("refresh_token") var refreshToken: String?,
@SerializedName("refresh_token_expires_in") var refreshExpiresIn: Long,
@SerializedName("id_token") var idToken: String
)
71 changes: 53 additions & 18 deletions auth/src/main/java/com/hse/auth/ui/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import android.accounts.AccountManager
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.hse.auth.R
import com.hse.auth.ui.accountmanager.AccountManagerFragment
import com.hse.auth.ui.credentials.WebViewCredentialsFragment
Expand All @@ -13,25 +16,13 @@ import com.hse.core.navigation.Navigation
import com.hse.core.navigation.NavigationCallback
import com.hse.core.ui.BaseActivity
import com.hse.core.ui.BaseFragment
import kotlinx.android.synthetic.main.activity_login.*
import net.danlew.android.joda.JodaTimeAndroid

class LoginActivity : BaseActivity(), NavigationCallback {
val mode = Mode.BASIC
private val mode = Mode.BASIC

private fun getRootTag(): String {
return when (mode) {
Mode.BASIC -> WebViewCredentialsFragment.TAG
else -> ""
}
}

private fun getRootFragment(): BaseFragment<*> {
val am = AccountManager.get(this)
val acc = am.accounts.find { it.type == getString(R.string.ru_hseid_acc_type) }
val code = intent.getStringExtra(AuthConstants.KEY_CODE)

return if (acc != null && code == null) AccountManagerFragment()
else WebViewCredentialsFragment.newInstance(code)
}
private lateinit var bottomSheetBehavior: BottomSheetBehavior<LinearLayout>

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
Expand All @@ -40,7 +31,9 @@ class LoginActivity : BaseActivity(), NavigationCallback {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
JodaTimeAndroid.init(this)
setContentView(R.layout.activity_login)
initBottomSheetBehavior()

navigation =
Navigation(savedInstanceState, R.id.navHostFragment, supportFragmentManager, this)
Expand All @@ -49,10 +42,52 @@ class LoginActivity : BaseActivity(), NavigationCallback {

override fun getRootFragment(rootTag: String) = getRootFragment()

override fun onStackChanged(newRootTag: String) {
override fun onStackChanged(newRootTag: String) = Unit

override fun onTopFragmentChanged(fragment: BaseFragment<*>?, rootTag: String) = Unit

override fun onBackPressed() {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
}

private fun getRootTag(): String {
return when (mode) {
Mode.BASIC -> WebViewCredentialsFragment.TAG
else -> ""
}
}

override fun onTopFragmentChanged(fragment: BaseFragment<*>?, rootTag: String) {
private fun getRootFragment(): BaseFragment<*> {
val am = AccountManager.get(this)
val acc = am.accounts.find { it.type == getString(R.string.ru_hseid_acc_type) }
val code = intent.getStringExtra(AuthConstants.KEY_CODE)

return if (acc != null && code == null) AccountManagerFragment()
else WebViewCredentialsFragment.newInstance(code)
}

private fun initBottomSheetBehavior() {
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
// Expanded by default
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetBehavior.skipCollapsed = true
bottomSheetBehavior.addBottomSheetCallback(object :
BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
finish()
overridePendingTransition(0, 0)
}
}

override fun onSlide(bottomSheet: View, slideOffset: Float) {

}
}
)
bottomSheet.setOnClickListener {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,7 @@ class AccountManagerFragment : BaseFragment<AccountManagerViewModel>() {
)

val adapter = UserAccountsAdapter(
onUserClickListener = { userData ->
activity?.let {
val data = Intent().apply {
putExtra(KEY_ACCESS_TOKEN, userData.accessToken)
putExtra(KEY_REFRESH_TOKEN, userData.refreshToken)
}
it.setResult(Activity.RESULT_OK, data)
it.finish()
}
}
onUserClickListener = viewModel::onAccountClicked
)
userAccountsRv.adapter = adapter
userAccountsRv.layoutManager = LinearLayoutManager(requireContext())
Expand Down Expand Up @@ -101,13 +92,25 @@ class AccountManagerFragment : BaseFragment<AccountManagerViewModel>() {
am.setAuthToken(account, account.type, it.accessToken)
})

viewModel.loginWithSelectedAccount.observe(viewLifecycleOwner, Observer { userData ->
activity?.let {
val data = Intent().apply {
putExtra(KEY_ACCESS_TOKEN, userData.accessToken)
putExtra(KEY_REFRESH_TOKEN, userData.refreshToken)
}
it.setResult(Activity.RESULT_OK, data)
it.finish()
it.overridePendingTransition(0, 0)
}
})

viewModel.reloginWithSelectedAccount.observe(viewLifecycleOwner, Observer { userData ->
WebViewCredentialsFragment.Builder().addUserAccountData(userData).go(activity())
})

loginWithNewAccBtn.onClick {
viewModel.onNewAccLoginClick()
}

backBtn.onClick {
activity()?.onBackPressed()
}
}

private fun setLoadingState(state: LoadingState) = when (state) {
Expand Down
Loading

0 comments on commit 454e642

Please sign in to comment.