Skip to content

Commit

Permalink
Merge pull request #3 from hseapp/feature/HSEOAPI-20
Browse files Browse the repository at this point in the history
Ui polishment, save image urls and fullName
  • Loading branch information
RomanAnchugov authored Oct 3, 2020
2 parents 454e642 + a35c573 commit 31458da
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 112 deletions.
3 changes: 2 additions & 1 deletion auth/src/main/java/com/hse/auth/models/MeDataEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.google.gson.annotations.SerializedName
import com.hse.auth.ui.models.UserEntity

data class MeDataEntity(
@SerializedName("avatar_url") var avatarUrl: String?
@SerializedName("avatar_url") var avatarUrl: String?,
@SerializedName("full_name") var fullName: String?
) : BaseDataEntity<UserEntity> {

override fun toEntity(): UserEntity {
Expand Down
4 changes: 2 additions & 2 deletions auth/src/main/java/com/hse/auth/ui/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.FrameLayout
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.hse.auth.R
import com.hse.auth.ui.accountmanager.AccountManagerFragment
Expand All @@ -22,7 +22,7 @@ import net.danlew.android.joda.JodaTimeAndroid
class LoginActivity : BaseActivity(), NavigationCallback {
private val mode = Mode.BASIC

private lateinit var bottomSheetBehavior: BottomSheetBehavior<LinearLayout>
lateinit var bottomSheetBehavior: BottomSheetBehavior<FrameLayout>

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.hse.auth.R
import com.hse.auth.di.AuthComponentProvider
import com.hse.auth.ui.LoginActivity
import com.hse.auth.ui.credentials.WebViewCredentialsFragment
import com.hse.auth.utils.AuthConstants.KEY_ACCESS_TOKEN
import com.hse.auth.utils.AuthConstants.KEY_REFRESH_TOKEN
Expand Down Expand Up @@ -99,15 +102,22 @@ class AccountManagerFragment : BaseFragment<AccountManagerViewModel>() {
putExtra(KEY_REFRESH_TOKEN, userData.refreshToken)
}
it.setResult(Activity.RESULT_OK, data)
it.finish()
it.overridePendingTransition(0, 0)
(it as? LoginActivity)?.bottomSheetBehavior?.state =
BottomSheetBehavior.STATE_HIDDEN
}
})

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

viewModel.error.observe(viewLifecycleOwner, Observer {
Toast.makeText(requireContext(), getString(R.string.error_happened), Toast.LENGTH_SHORT)
.show()
(activity() as? LoginActivity)?.bottomSheetBehavior?.state =
BottomSheetBehavior.STATE_HIDDEN
})

loginWithNewAccBtn.onClick {
viewModel.onNewAccLoginClick()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import com.hse.auth.requests.RefreshTokenRequest
import com.hse.auth.ui.models.UserAccountData
import com.hse.auth.utils.AuthConstants
import com.hse.auth.utils.AuthConstants.KEY_ACCESS_EXPIRES_IN_MILLIS
import com.hse.auth.utils.AuthConstants.KEY_AVATAR_URL
import com.hse.auth.utils.AuthConstants.KEY_FULL_NAME
import com.hse.auth.utils.AuthConstants.KEY_REFRESH_EXPIRES_IN_MILLIS
import com.hse.auth.utils.AuthConstants.KEY_REFRESH_TOKEN
import com.hse.core.enums.LoadingState
import com.hse.core.viewmodels.BaseViewModel
import com.hse.network.Network
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
import org.joda.time.DateTime
import javax.inject.Inject

Expand All @@ -39,6 +38,7 @@ class AccountManagerViewModel @Inject constructor(val network: Network) :

private val exceptionsHandler = CoroutineExceptionHandler { _, throwable ->
Log.e(TAG, "${throwable.message}")
// _error.postValue(throwable)
}

private val _userAccountsLiveData: MutableLiveData<List<UserAccountData>> = MutableLiveData()
Expand All @@ -61,6 +61,10 @@ class AccountManagerViewModel @Inject constructor(val network: Network) :
val reloginWithSelectedAccount: LiveData<UserAccountData>
get() = _reloginWithSelectedAccount

private val _error: MutableLiveData<Throwable> = MutableLiveData()
val error: LiveData<Throwable>
get() = _error

lateinit var clientId: String
lateinit var refreshToken: String
lateinit var accountManager: AccountManager
Expand All @@ -70,36 +74,39 @@ class AccountManagerViewModel @Inject constructor(val network: Network) :
accountManager: AccountManager,
accountType: String,
clientId: String
) = viewModelScope.launch(Dispatchers.IO + exceptionsHandler) {
) = CoroutineScope(SupervisorJob() + viewModelScope.coroutineContext).launch() {
this@AccountManagerViewModel.clientId = clientId
this@AccountManagerViewModel.accountManager = accountManager
this@AccountManagerViewModel.accountType = accountType

val accounts = accountManager.accounts.filter { it.type == accountType }
withContext(Dispatchers.Main) {
loadingState.value = LoadingState.LOADING
}

loadingState.value = LoadingState.LOADING
val accountsDataList = mutableListOf<UserAccountData>()
Log.i(TAG, "For start")
accounts.forEach { acc ->
val token = accountManager.blockingGetAuthToken(acc, acc.type, true)
val refreshToken = accountManager.getUserData(acc, KEY_REFRESH_TOKEN)
val accessExpiresIn =
accountManager.getUserData(acc, KEY_ACCESS_EXPIRES_IN_MILLIS).toLong()
val refreshExpiresIn =
accountManager.getUserData(acc, KEY_REFRESH_EXPIRES_IN_MILLIS).toLong()

this@AccountManagerViewModel.refreshToken = refreshToken
Log.i(TAG, "Data from acc manager\nToken\n$token\nRefreshToken\n$refreshToken")

if (accessExpiresIn - DateTime().millis > MINIMUM_TIME_DELTA_MILLIS) {
try {
viewModelScope.launch(Dispatchers.IO + exceptionsHandler) {
val token = accountManager.blockingGetAuthToken(acc, acc.type, true)
val refreshToken = accountManager.getUserData(acc, KEY_REFRESH_TOKEN)
val accessExpiresIn =
accountManager.getUserData(acc, KEY_ACCESS_EXPIRES_IN_MILLIS).toLong()
val refreshExpiresIn =
accountManager.getUserData(acc, KEY_REFRESH_EXPIRES_IN_MILLIS).toLong()
val fullName = accountManager.getUserData(acc, KEY_FULL_NAME)
val avatarUrl = accountManager.getUserData(acc, KEY_AVATAR_URL)

this@AccountManagerViewModel.refreshToken = refreshToken
Log.i(TAG, "Data from acc manager\nToken\n$token\nRefreshToken\n$refreshToken")

//Токен не протух
if (accessExpiresIn - DateTime().millis > MINIMUM_TIME_DELTA_MILLIS) {
Log.i(TAG, "Try for get user for \n$token")
GetMeRequest(token).run(network)
?.let { meEntity ->
accountsDataList.add(
UserAccountData(
acc.name,
meEntity.avatarUrl,
meEntity.fullName,
token,
refreshToken,
accessExpiresIn,
Expand All @@ -108,27 +115,25 @@ class AccountManagerViewModel @Inject constructor(val network: Network) :
)
Log.i(TAG, "Successful got user data with token from acc manager")
}
} catch (e: Exception) {
Log.i(TAG, "Catched ${e.message}")
}
} else {
accountsDataList.add(
UserAccountData(
acc.name,
null,
token,
refreshToken,
accessExpiresIn,
refreshExpiresIn
} else {// Протух
accountsDataList.add(
UserAccountData(
acc.name,
avatarUrl,
fullName,
token,
refreshToken,
accessExpiresIn,
refreshExpiresIn
)
)
)
}
}

withContext(Dispatchers.Main) {
_userAccountsLiveData.value = accountsDataList
loadingState.value = LoadingState.DONE
}
Unit
}.join()
}
Log.i(TAG, "For end")
_userAccountsLiveData.postValue(accountsDataList)
loadingState.postValue(LoadingState.DONE)
}

fun onNewAccLoginClick() {
Expand Down Expand Up @@ -172,18 +177,23 @@ class AccountManagerViewModel @Inject constructor(val network: Network) :
userEmail = it
}

val accountData = UserAccountData(
email = userEmail,
accessToken = tokensResult.accessToken,
refreshToken = tokensResult.refreshToken ?: refreshToken,
avatartUrl = null,
accessExpiresIn = tokensResult.accessExpiresIn,
refreshExpiresIn = tokensResult.refreshExpiresIn
)
_userAccountLiveData.postValue(accountData)
_loginWithSelectedAccount.postValue(accountData)
GetMeRequest(tokensResult.accessToken).run(network)
?.let { meEntity ->

val accountData = UserAccountData(
email = userEmail,
accessToken = tokensResult.accessToken,
refreshToken = tokensResult.refreshToken!!,
avatartUrl = meEntity.avatarUrl,
fullName = meEntity.fullName,
accessExpiresIn = DateTime().millis + tokensResult.accessExpiresIn * 1000,
refreshExpiresIn = DateTime().millis + tokensResult.refreshExpiresIn * 1000
)
_userAccountLiveData.postValue(accountData)
_loginWithSelectedAccount.postValue(accountData)
}
}
} else {//рефреш протух, полный перелогин//TODO: hint for requset
} else {//рефреш протух, полный перелогин
_reloginWithSelectedAccount.postValue(userAccountData)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class UserAccountsAdapter(
override fun onBindViewHolder(holder: UserAccountViewHolder, position: Int) {
val item = getItem(position)
holder.view.userEmailTv.text = item.email
holder.view.userNameTv.text = item.fullName
holder.view.userAvatarIv.load(item.avatartUrl) {
crossfade(true)
error(R.drawable.ic_person)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.browser.customtabs.CustomTabsIntent
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.hse.auth.R
import com.hse.auth.di.AuthComponentProvider
import com.hse.auth.ui.LoginActivity
Expand All @@ -29,7 +29,9 @@ import com.hse.auth.utils.AuthConstants.AUTH_PROMPT
import com.hse.auth.utils.AuthConstants.AUTH_SCHEME
import com.hse.auth.utils.AuthConstants.KEY_ACCESS_EXPIRES_IN_MILLIS
import com.hse.auth.utils.AuthConstants.KEY_ACCESS_TOKEN
import com.hse.auth.utils.AuthConstants.KEY_AVATAR_URL
import com.hse.auth.utils.AuthConstants.KEY_CLIENT_ID
import com.hse.auth.utils.AuthConstants.KEY_FULL_NAME
import com.hse.auth.utils.AuthConstants.KEY_REDIRECT_URI
import com.hse.auth.utils.AuthConstants.KEY_REFRESH_EXPIRES_IN_MILLIS
import com.hse.auth.utils.AuthConstants.KEY_REFRESH_TOKEN
Expand Down Expand Up @@ -120,7 +122,7 @@ class WebViewCredentialsFragment :
putExtra(KEY_REFRESH_TOKEN, model.refreshToken)
}
it.setResult(Activity.RESULT_OK, data)
it.finish()
it.bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
}
})

Expand All @@ -130,13 +132,13 @@ class WebViewCredentialsFragment :
putString(KEY_REFRESH_TOKEN, it.refreshToken)
putString(KEY_ACCESS_EXPIRES_IN_MILLIS, it.accessExpiresIn.toString())
putString(KEY_REFRESH_EXPIRES_IN_MILLIS, it.refreshExpiresIn.toString())
putString(KEY_AVATAR_URL, it.avatartUrl.toString())
putString(KEY_FULL_NAME, it.fullName.toString())
}
val am = AccountManager.get(context)
am.removeAccount(
account,
{ future ->
Log.i("LOL", "What happened $future?")

{ _ ->
am.addAccountExplicitly(account, "", userData)
am.setAuthToken(account, account.type, it.accessToken)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.auth0.android.jwt.JWT
import com.hse.auth.models.TokensModel
import com.hse.auth.requests.GetMeRequest
import com.hse.auth.requests.TokenRequest
import com.hse.auth.ui.models.UserAccountData
import com.hse.auth.utils.AuthConstants
import com.hse.core.enums.LoadingState
import com.hse.core.viewmodels.BaseViewModel
import com.hse.network.Network
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.joda.time.DateTime
import javax.inject.Inject

Expand Down Expand Up @@ -57,19 +56,22 @@ class WebViewCredentialsViewModel @Inject constructor(private val network: Netwo
userEmail = it
}

val accountData = UserAccountData(
email = userEmail,
accessToken = tokensResult.accessToken,
refreshToken = tokensResult.refreshToken!!,
avatartUrl = null,
accessExpiresIn = DateTime().millis + tokensResult.accessExpiresIn * 1000,
refreshExpiresIn = DateTime().millis + tokensResult.refreshExpiresIn * 1000
)
GetMeRequest(tokensResult.accessToken).run(network)
?.let { meEntity ->

withContext(Dispatchers.Main) {
_userAccountLiveData.value = accountData
_tokensResultLiveData.value = tokensResult
}
val accountData = UserAccountData(
email = userEmail,
accessToken = tokensResult.accessToken,
refreshToken = tokensResult.refreshToken!!,
avatartUrl = meEntity.avatarUrl,
fullName = meEntity.fullName,
accessExpiresIn = DateTime().millis + tokensResult.accessExpiresIn * 1000,
refreshExpiresIn = DateTime().millis + tokensResult.refreshExpiresIn * 1000
)

_userAccountLiveData.postValue(accountData)
_tokensResultLiveData.postValue(tokensResult)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.android.parcel.Parcelize
data class UserAccountData(
val email: String,
val avatartUrl: String?,
val fullName: String?,
val accessToken: String,
val refreshToken: String,
val accessExpiresIn: Long,
Expand Down
3 changes: 3 additions & 0 deletions auth/src/main/java/com/hse/auth/utils/AuthConstants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ object AuthConstants {
internal const val KEY_EMAIL = "email"
internal const val KEY_UPN = "upn"

internal const val KEY_AVATAR_URL = "avatar_url"
internal const val KEY_FULL_NAME = "full_name"

const val KEY_ACCESS_EXPIRES_IN_MILLIS = "access_expires_in"
const val KEY_REFRESH_EXPIRES_IN_MILLIS = "refresh_expires_in"
const val KEY_ACCESS_TOKEN = "access_token"
Expand Down
8 changes: 3 additions & 5 deletions auth/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
android:layout_height="match_parent"
android:background="@color/test">

<LinearLayout
<FrameLayout
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -27,8 +27,6 @@
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="32dp"
android:paddingTop="16dp"/>
</LinearLayout>
android:orientation="vertical" />
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Loading

0 comments on commit 31458da

Please sign in to comment.