Skip to content

Commit

Permalink
Merge pull request #1017 from Mzazi25/quick-fixes
Browse files Browse the repository at this point in the history
Injecting viewmodel to abstract class fix
  • Loading branch information
jumaallan authored May 16, 2023
2 parents 10e65b3 + 15ea2dd commit 8b0f751
Show file tree
Hide file tree
Showing 50 changed files with 251 additions and 147 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
54 changes: 53 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,8 +17,12 @@ 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.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 @@ -27,7 +31,10 @@ import com.hover.stax.MainNavigationDirections
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 @@ -38,10 +45,21 @@ import com.hover.stax.transactions.TransactionHistoryViewModel
import com.hover.stax.transfers.TransferViewModel
import com.hover.stax.utils.UIHelper
import com.hover.stax.views.StaxDialog
import dagger.hilt.android.AndroidEntryPoint
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 transferViewModel: TransferViewModel by viewModels()
Expand All @@ -52,12 +70,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 @@ -100,7 +122,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 @@ -137,6 +185,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,28 +32,24 @@ 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
import javax.inject.Inject

const val FORCED_VERSION = "force_update_app_version"

abstract class AbstractGoogleAuthActivity :
AppCompatActivity(),
StaxGoogleLoginInterface {

@Inject
lateinit var loginViewModel: LoginViewModel
protected abstract fun provideLoginViewModel(): LoginViewModel

private lateinit var staxGoogleLoginInterface: StaxGoogleLoginInterface

private lateinit var updateManager: AppUpdateManager
private var installListener: InstallStateUpdatedListener? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initGoogleAuth()
setLoginObserver()

updateManager = AppUpdateManagerFactory.create(this)

Expand All @@ -82,34 +75,6 @@ abstract class AbstractGoogleAuthActivity :
this.staxGoogleLoginInterface = staxGoogleLoginInterface
}

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@AbstractGoogleAuthActivity) {
it?.let { staxGoogleLoginInterface.googleLoginFailed() }
}

googleUser.observe(this@AbstractGoogleAuthActivity) {
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 checkForUpdates() {
val updateInfoTask = updateManager.appUpdateInfo

Expand Down Expand Up @@ -164,10 +129,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 @@ -191,10 +156,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 @@ -32,11 +32,15 @@ import com.hover.stax.permissions.PermissionUtils
import com.hover.stax.utils.NavUtil
import com.hover.stax.utils.UIHelper
import com.hover.stax.core.Utils
import com.hover.stax.login.LoginViewModel

class OnBoardingActivity : AbstractGoogleAuthActivity() {

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

override fun onCreate(savedInstanceState: Bundle?) {
UIHelper.setFullscreenView(this)
Expand Down Expand Up @@ -88,6 +92,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
Loading

0 comments on commit 8b0f751

Please sign in to comment.