Skip to content

Commit

Permalink
Merge branch 'develop' into 160523-provide-authentication-viewmodel-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jumaallan authored May 16, 2023
2 parents c942a3a + 8b0f751 commit 1edafc6
Show file tree
Hide file tree
Showing 50 changed files with 249 additions and 115 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/com/hover/stax/bounty/BountyRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import com.hover.stax.countries.CountryAdapter
import com.hover.stax.data.actions.ActionRepo
import com.hover.stax.database.models.Channel
import com.hover.stax.database.models.StaxTransaction
import com.hover.stax.di.Dispatcher
import com.hover.stax.di.StaxDispatchers
import com.hover.stax.model.Bounty
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
Expand All @@ -44,7 +46,7 @@ interface BountyRepository {

class BountyRepositoryImpl @Inject constructor(
val actionRepo: ActionRepo,
private val coroutineDispatcher: CoroutineDispatcher
@Dispatcher(StaxDispatchers.IO) private val coroutineDispatcher: CoroutineDispatcher
) : BountyRepository {

override val bountyActions: List<HoverAction>
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/com/hover/stax/di/DispatchersModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hover.stax.di

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

@Module
@InstallIn(SingletonComponent::class)
object DispatchersModule {

@Provides
@Dispatcher(StaxDispatchers.IO)
fun providesIODispatcher(): CoroutineDispatcher = Dispatchers.IO
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/hover/stax/di/StaxDispatchers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.hover.stax.di

import javax.inject.Qualifier
import kotlin.annotation.AnnotationRetention.RUNTIME

@Qualifier
@Retention(RUNTIME)
annotation class Dispatcher(val staxDispatcher: StaxDispatchers)

enum class StaxDispatchers {
Default,
IO,
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class GetChannelBountiesUseCase(
class GetChannelBountiesUseCase @Inject constructor(
private val channelRepository: ChannelRepository,
private val bountyRepository: BountyRepository,
private val transactionRepo: TransactionRepo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import com.hover.sdk.sims.SimInfo
import com.hover.stax.data.actions.ActionRepo
import com.hover.stax.database.models.Account
import com.hover.stax.data.accounts.AccountRepository
import com.hover.stax.di.Dispatcher
import com.hover.stax.di.StaxDispatchers
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import javax.inject.Inject

data class SimWithAccount(
val sim: SimInfo,
Expand All @@ -30,11 +33,11 @@ data class SimWithAccount(
val airtimeActions: List<HoverAction?> = emptyList()
)

class ListSimsUseCase(
class ListSimsUseCase @Inject constructor(
private val simRepository: com.hover.stax.data.sim.SimInfoRepository,
private val accountRepository: AccountRepository,
private val actionRepository: ActionRepo,
private val defaultDispatcher: CoroutineDispatcher
@Dispatcher(StaxDispatchers.IO) private val defaultDispatcher: CoroutineDispatcher
) {

suspend operator fun invoke(): List<SimWithAccount> = withContext(defaultDispatcher) {
Expand Down
51 changes: 50 additions & 1 deletion app/src/main/java/com/hover/stax/home/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ package com.hover.stax.home

import android.content.Intent
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavDirections
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.hover.sdk.actions.HoverAction
import com.hover.sdk.api.Hover
import com.hover.sdk.permissions.PermissionHelper
Expand All @@ -29,7 +33,9 @@ import com.hover.stax.R
import com.hover.stax.databinding.ActivityMainBinding
import com.hover.stax.login.AbstractGoogleAuthActivity
import com.hover.stax.login.LoginViewModel
import com.hover.stax.login.StaxGoogleLoginInterface
import com.hover.stax.notifications.PushNotificationTopicsInterface
import com.hover.stax.presentation.bounties.BountyApplicationFragmentDirections
import com.hover.stax.presentation.financial_tips.FinancialTipsFragment
import com.hover.stax.requests.NewRequestViewModel
import com.hover.stax.requests.REQUEST_LINK
Expand All @@ -46,6 +52,15 @@ import timber.log.Timber
@AndroidEntryPoint
class MainActivity : AbstractGoogleAuthActivity(), BiometricChecker.AuthListener, PushNotificationTopicsInterface, RequestSenderInterface {

private lateinit var viewModelFactory: ViewModelProvider.Factory

private lateinit var loginViewModel: LoginViewModel
private lateinit var staxGoogleLoginInterface: StaxGoogleLoginInterface

override fun provideLoginViewModel(): LoginViewModel {
val loginViewModel: LoginViewModel by viewModels()
return loginViewModel
}
lateinit var navHelper: NavHelper

private val loginViewModel : LoginViewModel by viewModels()
Expand All @@ -58,12 +73,16 @@ class MainActivity : AbstractGoogleAuthActivity(), BiometricChecker.AuthListener

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
loginViewModel = provideLoginViewModel()
staxGoogleLoginInterface = this
viewModelFactory = ViewModelProvider.AndroidViewModelFactory.getInstance(application)

navHelper = NavHelper(this)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
navHelper.setUpNav()

initGoogleAuth()
setLoginObserver()
initFromIntent()
checkForRequest(intent)
checkForFragmentDirection(intent)
Expand Down Expand Up @@ -108,7 +127,33 @@ class MainActivity : AbstractGoogleAuthActivity(), BiometricChecker.AuthListener
navHelper.checkPermissionsAndNavigate(toWhere)
}
}
private fun initGoogleAuth() {
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.google_server_client_id)).requestEmail().build()
loginViewModel.signInClient = GoogleSignIn.getClient(this, gso)
}

private fun setLoginObserver() = with(loginViewModel) {
error.observe(this@MainActivity) {
it?.let { staxGoogleLoginInterface.googleLoginFailed() }
}

googleUser.observe(this@MainActivity) {
it?.let { staxGoogleLoginInterface.googleLoginSuccessful() }
}
}

fun signIn() = loginForResult.launch(loginViewModel.signInClient.signInIntent)

private val loginForResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
loginViewModel.signIntoGoogle(result.data)
} else {
Timber.e("Google sign in failed")
staxGoogleLoginInterface.googleLoginFailed()
}
}
private fun initFromIntent() {
when {
intent.hasExtra(REQUEST_LINK) -> createFromRequest(intent.getStringExtra(REQUEST_LINK)!!)
Expand Down Expand Up @@ -145,6 +190,10 @@ class MainActivity : AbstractGoogleAuthActivity(), BiometricChecker.AuthListener
}
}

override fun googleLoginSuccessful() {
if (loginViewModel.staxUser.value?.isMapper == true) BountyApplicationFragmentDirections.actionBountyApplicationFragmentToBountyListFragment()
}

override fun onAuthError(error: String) {
Timber.e("Error : $error")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ package com.hover.stax.login

import android.content.Intent
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.material.snackbar.Snackbar
import com.google.android.play.core.appupdate.AppUpdateInfo
import com.google.android.play.core.appupdate.AppUpdateManager
Expand All @@ -35,7 +32,6 @@ import com.google.android.play.core.install.model.UpdateAvailability.UPDATE_AVAI
import com.hover.stax.BuildConfig
import com.hover.stax.R
import com.hover.stax.core.Utils
import com.hover.stax.presentation.bounties.BountyApplicationFragmentDirections
import com.hover.stax.utils.UIHelper
import timber.log.Timber

Expand All @@ -46,6 +42,9 @@ abstract class AbstractGoogleAuthActivity :
StaxGoogleLoginInterface {

private lateinit var loginViewModel: LoginViewModel

protected abstract fun provideLoginViewModel(): LoginViewModel

private lateinit var staxGoogleLoginInterface: StaxGoogleLoginInterface

private lateinit var updateManager: AppUpdateManager
Expand Down Expand Up @@ -112,7 +111,7 @@ abstract class AbstractGoogleAuthActivity :
staxGoogleLoginInterface.googleLoginFailed()
}
}

private fun checkForUpdates() {
val updateInfoTask = updateManager.appUpdateInfo

Expand Down Expand Up @@ -167,10 +166,10 @@ abstract class AbstractGoogleAuthActivity :
).apply {
setAction(getString(R.string.restart)) {
updateManager.completeUpdate(); installListener?.let {
updateManager.unregisterListener(
it
)
}
updateManager.unregisterListener(
it
)
}
}
setActionTextColor(
ContextCompat.getColor(
Expand All @@ -194,10 +193,6 @@ abstract class AbstractGoogleAuthActivity :
}
}

override fun googleLoginSuccessful() {
if (loginViewModel.staxUser.value?.isMapper == true) BountyApplicationFragmentDirections.actionBountyApplicationFragmentToBountyListFragment()
}

override fun googleLoginFailed() {
UIHelper.flashAndReportMessage(this, R.string.login_google_err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ import com.hover.stax.database.models.BUSINESS_NO
import com.hover.stax.database.models.Merchant
import com.hover.stax.database.repo.ContactRepo
import com.hover.stax.transfers.AbstractFormViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MerchantViewModel @Inject constructor(
application: Application,
contactRepo: ContactRepo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class OnBoardingActivity : AbstractGoogleAuthActivity() {

private lateinit var binding: OnboardingLayoutBinding
private lateinit var navController: NavController
override fun provideLoginViewModel(): LoginViewModel {
TODO("Not yet implemented")
}

private val loginViewModel: LoginViewModel by viewModels()

Expand Down Expand Up @@ -96,6 +99,10 @@ class OnBoardingActivity : AbstractGoogleAuthActivity() {
checkPermissionsAndNavigate()
}

override fun googleLoginSuccessful() {
TODO("Not yet implemented")
}

private fun navigateToMainActivity() {
val intent = Intent(this, MainActivity::class.java).apply {
putExtra(FRAGMENT_DIRECT, NAV_LINK_ACCOUNT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.hover.stax.presentation.bounties.components.ChannelBountiesCardPrevie
import com.hover.stax.presentation.bounties.components.ChannelBountyCard
import com.hover.stax.presentation.bounties.components.CountryDropdown
import com.hover.stax.presentation.bounties.components.CountryDropdownPreview
import com.hover.stax.ui.theme.StaxTheme
import com.hover.stax.views.theme.StaxTheme

const val CODE_ALL_COUNTRIES = "00"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.hover.stax.countries.CountryAdapter
import com.hover.stax.domain.use_case.bounties.GetChannelBountiesUseCase
import com.hover.stax.core.Utils.getPackage
import com.hover.stax.model.Bounty
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -39,7 +40,7 @@ import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class BountyViewModel @Inject constructor(
private val simRepository: com.hover.stax.data.sim.SimInfoRepository,
private val bountiesUseCase: GetChannelBountiesUseCase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import com.hover.stax.R
import com.hover.stax.model.Bounty
import com.hover.stax.presentation.bounties.BountySelectEvent
import com.hover.stax.presentation.bounties.BountyViewModel
import com.hover.stax.ui.theme.Brutalista
import com.hover.stax.views.theme.Brutalista

@Composable
fun BountyLi(bounty: Bounty, bountyViewModel: BountyViewModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hover.stax.ui.theme.Border
import com.hover.stax.ui.theme.BrightBlue
import com.hover.stax.ui.theme.ColorPrimary
import com.hover.stax.ui.theme.OffWhite
import com.hover.stax.ui.theme.TextGrey
import com.hover.stax.ui.theme.mainBackground
import com.hover.stax.views.theme.Border
import com.hover.stax.views.theme.BrightBlue
import com.hover.stax.views.theme.ColorPrimary
import com.hover.stax.views.theme.OffWhite
import com.hover.stax.views.theme.TextGrey
import com.hover.stax.views.theme.mainBackground

const val SECONDARY = 0
const val PRIMARY = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import androidx.compose.material.Card
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.hover.stax.ui.theme.Border
import com.hover.stax.ui.theme.mainBackground
import com.hover.stax.views.theme.Border
import com.hover.stax.views.theme.mainBackground

@Composable
fun StaxCard(content: @Composable ColumnScope.() -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.hover.stax.model.Resource
import com.hover.stax.domain.use_case.financial_tips.TipsUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject

@HiltViewModel
class FinancialTipsViewModel @Inject constructor(
private val tipsUseCase: TipsUseCase
) : ViewModel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import com.hover.stax.database.models.Account
import com.hover.stax.presentation.home.components.BalanceHeader
import com.hover.stax.presentation.home.components.BalanceItem
import com.hover.stax.presentation.home.components.EmptyBalance
import com.hover.stax.ui.theme.StaxTheme
import com.hover.stax.views.theme.StaxTheme

interface BalanceTapListener {
fun onTapBalanceRefresh(account: Account?)
Expand Down
Loading

0 comments on commit 1edafc6

Please sign in to comment.