Skip to content

Commit

Permalink
Move the logic to DonateViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
cooltey committed Aug 28, 2024
1 parent 4f4ae9a commit 4fbe0f4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
19 changes: 18 additions & 1 deletion app/src/main/java/org/wikipedia/donate/DonateViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import android.app.Activity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.wikipedia.dataclient.donate.DonationConfigHelper
import org.wikipedia.settings.Prefs
import org.wikipedia.util.GeoUtil
import org.wikipedia.util.Resource
import java.util.Locale

class DonateViewModel : ViewModel() {
private val _uiState = MutableStateFlow<Resource<Boolean>>(Resource.Loading())
Expand All @@ -19,7 +24,19 @@ class DonateViewModel : ViewModel() {
}) {
_uiState.value = Resource.Loading()

_uiState.value = Resource.Success(GooglePayComponent.isGooglePayAvailable(activity))
GooglePayViewModel.updatePaymentMethodsPreferences()
val donationConfig = async { DonationConfigHelper.getConfig() }
var googlePayAvailable = GooglePayComponent.isGooglePayAvailable(activity)
donationConfig.await()?.let { config ->
val currentCountryCode = GeoUtil.geoIPCountry.orEmpty()
val currencyCode = GeoUtil.currencyFormat(Locale.getDefault()).currency?.currencyCode ?: GooglePayComponent.CURRENCY_FALLBACK
googlePayAvailable = !(Prefs.paymentMethodsMerchantId.isEmpty() ||
Prefs.paymentMethodsGatewayId.isEmpty() ||
!config.countryCodeGooglePayEnabled.contains(currentCountryCode) ||
!config.currencyAmountPresets.containsKey(currencyCode))
}

_uiState.value = Resource.Success(googlePayAvailable)
}
}
}
32 changes: 1 addition & 31 deletions app/src/main/java/org/wikipedia/main/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.wikipedia.BackPressedHandler
Expand All @@ -46,10 +43,6 @@ import org.wikipedia.commons.FilePageActivity
import org.wikipedia.concurrency.FlowEventBus
import org.wikipedia.databinding.FragmentMainBinding
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.dataclient.donate.DonationConfigHelper
import org.wikipedia.donate.DonateDialog
import org.wikipedia.donate.GooglePayComponent
import org.wikipedia.donate.GooglePayViewModel.Companion.updatePaymentMethodsPreferences
import org.wikipedia.events.ImportReadingListsEvent
import org.wikipedia.events.LoggedOutInBackgroundEvent
import org.wikipedia.feed.FeedFragment
Expand Down Expand Up @@ -87,14 +80,12 @@ import org.wikipedia.talk.TalkTopicsActivity
import org.wikipedia.usercontrib.UserContribListActivity
import org.wikipedia.util.DimenUtil
import org.wikipedia.util.FeedbackUtil
import org.wikipedia.util.GeoUtil
import org.wikipedia.util.ShareUtil
import org.wikipedia.util.TabUtil
import org.wikipedia.views.NotificationButtonView
import org.wikipedia.views.TabCountsView
import org.wikipedia.watchlist.WatchlistActivity
import java.io.File
import java.util.Locale
import java.util.concurrent.TimeUnit

class MainFragment : Fragment(), BackPressedHandler, MenuProvider, FeedFragment.Callback, HistoryFragment.Callback, MenuNavTabDialog.Callback {
Expand All @@ -113,7 +104,6 @@ class MainFragment : Fragment(), BackPressedHandler, MenuProvider, FeedFragment.
private val downloadReceiverCallback = MediaDownloadReceiverCallback()
private val pageChangeCallback = PageChangeCallback()
private var exclusiveTooltipRunnable: Runnable? = null
private var showDonationBottomSheet = false

// The permissions request API doesn't take a callback, so in the event we have to
// ask for permission to download a featured image from the feed, we'll have to hold
Expand Down Expand Up @@ -177,7 +167,6 @@ class MainFragment : Fragment(), BackPressedHandler, MenuProvider, FeedFragment.

notificationButtonView = NotificationButtonView(requireActivity())

checkDonateConfig()
maybeShowEditsTooltip()

if (savedInstanceState == null) {
Expand Down Expand Up @@ -476,26 +465,7 @@ class MainFragment : Fragment(), BackPressedHandler, MenuProvider, FeedFragment.
}

override fun donateClick() {
if (showDonationBottomSheet) {
(requireActivity() as? BaseActivity)?.launchDonateDialog()
} else {
DonateDialog.launchDonateLink(requireContext())
}
}

private fun checkDonateConfig() {
CoroutineScope(Dispatchers.IO).launch {
async { updatePaymentMethodsPreferences() }.await()
val donationConfig = async { DonationConfigHelper.getConfig() }
donationConfig.await()?.let { config ->
val currentCountryCode = GeoUtil.geoIPCountry.orEmpty()
val currencyCode = GeoUtil.currencyFormat(Locale.getDefault()).currency?.currencyCode ?: GooglePayComponent.CURRENCY_FALLBACK
showDonationBottomSheet = !(Prefs.paymentMethodsMerchantId.isEmpty() ||
Prefs.paymentMethodsGatewayId.isEmpty() ||
!config.countryCodeGooglePayEnabled.contains(currentCountryCode) ||
!config.currencyAmountPresets.containsKey(currencyCode))
}
}
(requireActivity() as? BaseActivity)?.launchDonateDialog()
}

fun setBottomNavVisible(visible: Boolean) {
Expand Down

0 comments on commit 4fbe0f4

Please sign in to comment.