Skip to content

Commit

Permalink
Refactored code.| #2162
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Jan 23, 2023
1 parent 53fe0df commit d79a5cb
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,15 @@ interface ApiService {
@GET("https://{directHost}/.well-known/openpgpkey/policy")
suspend fun checkPolicyForWkdDirect(@Path("directHost") directHost: String): Response<ResponseBody>

/**
* This method calls "https://flowcrypt.com/shared-tenant-fes/account/get"
*/
@POST(BuildConfig.API_URL + "account/get")
suspend fun getClientConfigurationFromFlowCryptComBackend(
@Header("Authorization") authorization: String,
@Body body: Any = Any(),
): Response<ClientConfigurationResponse>

/**
* This method calls "https://fes.$domain/api/v1/client-configuration?domain=$domain"
*
* @param fesUrl URL of FES
* @param customFesUrl A custom FES URL
*/
@GET
suspend fun getClientConfigurationFromFes(@Url fesUrl: String): Response<ClientConfigurationResponse>
suspend fun getClientConfigurationFromFes(
@Url customFesUrl: String? = BuildConfig.API_URL + "account/get"
): Response<ClientConfigurationResponse>

/**
* Set or replace public key with idToken as an auth mechanism
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,13 @@ import java.util.concurrent.TimeUnit
class FlowcryptApiRepository : ApiRepository {
override suspend fun getClientConfiguration(
context: Context,
fesUrl: String?,
customFesUrl: String?,
idToken: String
): Result<ClientConfigurationResponse> =
withContext(Dispatchers.IO) {
val apiService = ApiHelper.getInstance(context).retrofit.create(ApiService::class.java)
getResult(context = context) {
if (fesUrl != null) {
apiService.getClientConfigurationFromFes(fesUrl = fesUrl)
} else {
apiService.getClientConfigurationFromFlowCryptComBackend(authorization = "Bearer $idToken")
}
apiService.getClientConfigurationFromFes(customFesUrl = customFesUrl)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ import kotlinx.coroutines.launch
* Time: 9:54 AM
* E-mail: [email protected]
*/
class CheckFesServerViewModel(application: Application) : BaseAndroidViewModel(application) {
class CheckCustomUrlFesServerViewModel(application: Application) :
BaseAndroidViewModel(application) {
private val repository = FlowcryptApiRepository()
val checkFesServerLiveData: MutableLiveData<Result<FesServerResponse>> =
val checkFesServerAvailabilityLiveData: MutableLiveData<Result<FesServerResponse>> =
MutableLiveData(Result.none())

fun checkFesServerAvailability(account: String) {
fun checkServerAvailability(account: String) {
viewModelScope.launch {
val context: Context = getApplication()
checkFesServerLiveData.value =
checkFesServerAvailabilityLiveData.value =
Result.loading(progressMsg = context.getString(R.string.loading))

try {
Expand All @@ -43,7 +44,7 @@ class CheckFesServerViewModel(application: Application) : BaseAndroidViewModel(a
if (result.status == Result.Status.EXCEPTION) {
val causedException = result.exception
if (causedException != null) {
checkFesServerLiveData.value = Result.exception(
checkFesServerAvailabilityLiveData.value = Result.exception(
GeneralUtil.preProcessException(
context = context,
causedException = causedException
Expand All @@ -53,9 +54,9 @@ class CheckFesServerViewModel(application: Application) : BaseAndroidViewModel(a
}
}

checkFesServerLiveData.value = result
checkFesServerAvailabilityLiveData.value = result
} catch (e: Exception) {
checkFesServerLiveData.value = Result.exception(e)
checkFesServerAvailabilityLiveData.value = Result.exception(e)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ClientConfigurationViewModel(application: Application) : BaseAndroidViewMo
val clientConfigurationLiveData: MutableLiveData<Result<ClientConfigurationResponse>> =
MutableLiveData(Result.none())

fun fetchClientConfiguration(idToken: String, fesUrl: String? = null) {
fun fetchClientConfiguration(idToken: String, customFesUrl: String? = null) {
viewModelScope.launch {
val context: Context = getApplication()
clientConfigurationLiveData.value =
Expand All @@ -39,7 +39,7 @@ class ClientConfigurationViewModel(application: Application) : BaseAndroidViewMo
clientConfigurationLiveData.value = repository.getClientConfiguration(
context = context,
idToken = idToken,
fesUrl = fesUrl,
customFesUrl = customFesUrl,
)
} catch (e: Exception) {
clientConfigurationLiveData.value = Result.exception(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RefreshClientConfigurationWorker(context: Context, params: WorkerParameter
return Result.success()
}

val fesUrl = GeneralUtil.generateFesUrl(domain)
val customFesUrl = GeneralUtil.generatePotentialCustomFesUrl(domain)
try {
val idToken = GeneralUtil.getGoogleIdToken(
context = applicationContext,
Expand All @@ -47,8 +47,8 @@ class RefreshClientConfigurationWorker(context: Context, params: WorkerParameter

val result = repository.getClientConfiguration(
context = applicationContext,
fesUrl = fesUrl,
idToken = idToken
customFesUrl = customFesUrl,
idToken = idToken,
)

if (result.status == Status.SUCCESS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import com.flowcrypt.email.extensions.showFeedbackFragment
import com.flowcrypt.email.extensions.showInfoDialog
import com.flowcrypt.email.extensions.showTwoWayDialog
import com.flowcrypt.email.extensions.toast
import com.flowcrypt.email.jetpack.viewmodel.CheckFesServerViewModel
import com.flowcrypt.email.jetpack.viewmodel.CheckCustomUrlFesServerViewModel
import com.flowcrypt.email.jetpack.viewmodel.ClientConfigurationViewModel
import com.flowcrypt.email.jetpack.viewmodel.EkmViewModel
import com.flowcrypt.email.model.KeyImportDetails
Expand Down Expand Up @@ -86,9 +86,9 @@ class MainSignInFragment : BaseSingInFragment<FragmentMainSignInBinding>() {
private lateinit var client: GoogleSignInClient
private var googleSignInAccount: GoogleSignInAccount? = null
private var clientConfiguration: ClientConfiguration? = null
private var fesUrl: String? = null
private var customFesUrl: String? = null

private val checkFesServerViewModel: CheckFesServerViewModel by viewModels()
private val checkCustomUrlFesServerViewModel: CheckCustomUrlFesServerViewModel by viewModels()
private val clientConfigurationViewModel: ClientConfigurationViewModel by viewModels()
private val ekmViewModel: EkmViewModel by viewModels()
private var useStartTlsForSmtp = false
Expand Down Expand Up @@ -143,7 +143,7 @@ class MainSignInFragment : BaseSingInFragment<FragmentMainSignInBinding>() {
AccountEntity(
googleSignInAccount = it,
clientConfiguration = clientConfiguration,
useFES = fesUrl?.isNotEmpty() == true,
useFES = customFesUrl?.isNotEmpty() == true,
useStartTlsForSmtp = useStartTlsForSmtp,
)
}
Expand Down Expand Up @@ -224,8 +224,8 @@ class MainSignInFragment : BaseSingInFragment<FragmentMainSignInBinding>() {
onSignSuccess(googleSignInAccount)
} else {
clientConfiguration = null
fesUrl = null
checkFesServerViewModel.checkFesServerAvailability(account)
customFesUrl = null
checkCustomUrlFesServerViewModel.checkServerAvailability(account)
}
} else {
val error = task.exception
Expand Down Expand Up @@ -403,14 +403,17 @@ class MainSignInFragment : BaseSingInFragment<FragmentMainSignInBinding>() {
when (requestCode) {
REQUEST_CODE_RETRY_CHECK_FES_AVAILABILITY -> if (result == TwoWayDialogFragment.RESULT_OK) {
clientConfiguration = null
fesUrl = null
customFesUrl = null
val account = googleSignInAccount?.account?.name ?: return@setFragmentResultListener
checkFesServerViewModel.checkFesServerAvailability(account)
checkCustomUrlFesServerViewModel.checkServerAvailability(account)
}

REQUEST_CODE_RETRY_GET_CLIENT_CONFIGURATION -> if (result == TwoWayDialogFragment.RESULT_OK) {
val idToken = googleSignInAccount?.idToken ?: return@setFragmentResultListener
clientConfigurationViewModel.fetchClientConfiguration(idToken = idToken, fesUrl = fesUrl)
clientConfigurationViewModel.fetchClientConfiguration(
idToken = idToken,
customFesUrl = customFesUrl
)
}

REQUEST_CODE_RETRY_FETCH_PRV_KEYS_VIA_EKM -> if (result == TwoWayDialogFragment.RESULT_OK) {
Expand Down Expand Up @@ -509,7 +512,7 @@ class MainSignInFragment : BaseSingInFragment<FragmentMainSignInBinding>() {
}

private fun initCheckFesServerViewModel() {
checkFesServerViewModel.checkFesServerLiveData.observe(viewLifecycleOwner) {
checkCustomUrlFesServerViewModel.checkFesServerAvailabilityLiveData.observe(viewLifecycleOwner) {
when (it.status) {
Result.Status.LOADING -> {
countingIdlingResource?.incrementSafely(this@MainSignInFragment)
Expand All @@ -521,22 +524,22 @@ class MainSignInFragment : BaseSingInFragment<FragmentMainSignInBinding>() {
googleSignInAccount?.account?.name?.let { account ->
val domain = EmailUtil.getDomain(account)
val idToken = googleSignInAccount?.idToken ?: return@let
fesUrl = GeneralUtil.generateFesUrl(domain)
customFesUrl = GeneralUtil.generatePotentialCustomFesUrl(domain)
clientConfigurationViewModel.fetchClientConfiguration(
idToken = idToken,
fesUrl = fesUrl
customFesUrl = customFesUrl
)
}
} else {
continueBasedOnFlavorSettings()
}

checkFesServerViewModel.checkFesServerLiveData.value = Result.none()
checkCustomUrlFesServerViewModel.checkFesServerAvailabilityLiveData.value = Result.none()
countingIdlingResource?.decrementSafely(this@MainSignInFragment)
}

Result.Status.ERROR -> {
checkFesServerViewModel.checkFesServerLiveData.value = Result.none()
checkCustomUrlFesServerViewModel.checkFesServerAvailabilityLiveData.value = Result.none()
showDialogWithRetryButton(it, REQUEST_CODE_RETRY_CHECK_FES_AVAILABILITY)
countingIdlingResource?.decrementSafely(this@MainSignInFragment)
}
Expand Down Expand Up @@ -579,7 +582,7 @@ class MainSignInFragment : BaseSingInFragment<FragmentMainSignInBinding>() {
}
}

checkFesServerViewModel.checkFesServerLiveData.value = Result.none()
checkCustomUrlFesServerViewModel.checkFesServerAvailabilityLiveData.value = Result.none()
countingIdlingResource?.decrementSafely(this@MainSignInFragment)
}
else -> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ class GeneralUtil {
/**
* Generate a FES url.
*/
fun generateFesUrl(domain: String): String {
fun generatePotentialCustomFesUrl(domain: String): String {
return "https://fes.$domain/api/v1/client-configuration?domain=$domain"
}

Expand Down

0 comments on commit d79a5cb

Please sign in to comment.