Skip to content

Commit

Permalink
Merge pull request #3984 from kiwix/Fixes#3736
Browse files Browse the repository at this point in the history
Added a donation popup to our application.
  • Loading branch information
kelson42 authored Sep 9, 2024
2 parents 681e3a0 + 9dee134 commit f2534d3
Show file tree
Hide file tree
Showing 30 changed files with 543 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class DeepLinksTest : BaseActivityTest() {
prefIsTest = true
playStoreRestrictionPermissionDialog = false
putPrefLanguage("en")
lastDonationPopupShownInMilliSeconds = System.currentTimeMillis()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ class DownloadTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class HelpFragmentTest : BaseActivityTest() {
handleLocaleChange(
it,
"en",
SharedPreferenceUtil(context)
SharedPreferenceUtil(context).apply {
lastDonationPopupShownInMilliSeconds = System.currentTimeMillis()
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class InitialDownloadTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class IntroFragmentTest : BaseActivityTest() {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, true)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class LanguageFragmentTest {
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class CopyMoveFileHandlerTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class TopLevelDestinationTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_SHOW_SHOWCASE, false)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class MimeTypeTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ class LocalLibraryTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_SHOW_MANAGE_PERMISSION_DIALOG_ON_REFRESH, false)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class NoteFragmentTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ class ImportBookmarkTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class LibkiwixBookmarkTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class NavigationHistoryTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class EncodedUrlTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class SearchFragmentTest : BaseActivityTest() {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
putLong(
SharedPreferenceUtil.PREF_LAST_DONATION_POPUP_SHOWN_IN_MILLISECONDS,
System.currentTimeMillis()
)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class KiwixSettingsFragmentTest {
SharedPreferenceUtil(it).apply {
setIsPlayStoreBuildType(true)
playStoreRestrictionPermissionDialog = false
lastDonationPopupShownInMilliSeconds = System.currentTimeMillis()
}
)
it.navigate(R.id.introFragment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class ZimHostFragmentTest {
prefIsTest = true
playStoreRestrictionPermissionDialog = false
putPrefLanguage("en")
lastDonationPopupShownInMilliSeconds = System.currentTimeMillis()
}
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider {
drawerContainerLayout.closeDrawer(drawerNavView)
}

private fun openSupportKiwixExternalLink() {
fun openSupportKiwixExternalLink() {
externalLinkOpener.openExternalUrl(KIWIX_SUPPORT_URL.toUri().browserIntent())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import android.view.MenuInflater
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.view.animation.AnimationUtils
Expand Down Expand Up @@ -133,6 +134,8 @@ import org.kiwix.kiwixmobile.core.reader.ZimReaderSource
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen
import org.kiwix.kiwixmobile.core.utils.AnimationUtils.rotate
import org.kiwix.kiwixmobile.core.utils.DimenUtils.getToolbarHeight
import org.kiwix.kiwixmobile.core.utils.DonationDialogHandler
import org.kiwix.kiwixmobile.core.utils.DonationDialogHandler.ShowDonationDialogCallback
import org.kiwix.kiwixmobile.core.utils.ExternalLinkOpener
import org.kiwix.kiwixmobile.core.utils.LanguageUtils
import org.kiwix.kiwixmobile.core.utils.LanguageUtils.Companion.getCurrentLocale
Expand Down Expand Up @@ -173,7 +176,8 @@ abstract class CoreReaderFragment :
FragmentActivityExtensions,
WebViewProvider,
ReadAloudCallbacks,
NavigationHistoryClickListener {
NavigationHistoryClickListener,
ShowDonationDialogCallback {
protected val webViewList: MutableList<KiwixWebView> = ArrayList()
private val webUrlsProcessor = BehaviorProcessor.create<String>()
private var fragmentReaderBinding: FragmentReaderBinding? = null
Expand Down Expand Up @@ -227,6 +231,10 @@ abstract class CoreReaderFragment :
@Inject
var alertDialogShower: DialogShower? = null

@JvmField
@Inject
var donationDialogHandler: DonationDialogHandler? = null

@JvmField
@Inject
var painter: DarkModeViewPainter? = null
Expand Down Expand Up @@ -297,6 +305,7 @@ abstract class CoreReaderFragment :
private var tableDrawerAdapter: TableDrawerAdapter? = null
private var tableDrawerRight: RecyclerView? = null
private var tabCallback: ItemTouchHelper.Callback? = null
private var donationLayout: FrameLayout? = null
private var bookmarkingDisposable: Disposable? = null
private var isBookmarked = false
private lateinit var serviceConnection: ServiceConnection
Expand Down Expand Up @@ -389,6 +398,7 @@ abstract class CoreReaderFragment :
) {
super.onViewCreated(view, savedInstanceState)
setupMenu()
donationDialogHandler?.setDonationDialogCallBack(this)
val activity = requireActivity() as AppCompatActivity?
activity?.let {
WebView(it).destroy() // Workaround for buggy webViews see #710
Expand Down Expand Up @@ -511,6 +521,7 @@ abstract class CoreReaderFragment :
tabRecyclerView = findViewById(R.id.tab_switcher_recycler_view)
snackBarRoot = findViewById(R.id.snackbar_root)
bottomToolbarToc = findViewById(R.id.bottom_toolbar_toc)
donationLayout = findViewById(R.id.donation_layout)
}
}
}
Expand Down Expand Up @@ -1213,6 +1224,8 @@ abstract class CoreReaderFragment :
unRegisterReadAloudService()
storagePermissionForNotesLauncher?.unregister()
storagePermissionForNotesLauncher = null
donationDialogHandler?.setDonationDialogCallBack(null)
donationDialogHandler = null
}

private fun unBindViewsAndBinding() {
Expand Down Expand Up @@ -1243,6 +1256,8 @@ abstract class CoreReaderFragment :
closeAllTabsButton = null
tableDrawerRightContainer = null
fragmentReaderBinding = null
donationLayout?.removeAllViews()
donationLayout = null
}

private fun updateTableOfContents() {
Expand Down Expand Up @@ -1846,6 +1861,76 @@ abstract class CoreReaderFragment :
if (tts == null) {
setUpTTS()
}
donationDialogHandler?.attemptToShowDonationPopup()
}

@Suppress("InflateParams", "MagicNumber")
protected open fun showDonationLayout() {
val donationCardView = layoutInflater.inflate(R.layout.layout_donation_bottom_sheet, null)
val layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT
).apply {
val rightAndLeftMargin = requireActivity().resources.getDimensionPixelSize(
org.kiwix.kiwixmobile.core.R.dimen.activity_horizontal_margin
)
setMargins(
rightAndLeftMargin,
0,
rightAndLeftMargin,
getBottomMarginForDonationPopup()
)
}

donationCardView.layoutParams = layoutParams
donationLayout?.apply {
removeAllViews()
addView(donationCardView)
setDonationLayoutVisibility(VISIBLE)
}
donationCardView.findViewById<TextView>(R.id.descriptionText).apply {
text = getString(
R.string.donation_dialog_description,
(requireActivity() as CoreMainActivity).appName
)
}
val donateButton: TextView = donationCardView.findViewById(R.id.donateButton)
donateButton.setOnClickListener {
donationDialogHandler?.updateLastDonationPopupShownTime()
setDonationLayoutVisibility(GONE)
openKiwixSupportUrl()
}

val laterButton: TextView = donationCardView.findViewById(R.id.laterButton)
laterButton.setOnClickListener {
donationDialogHandler?.donateLater()
setDonationLayoutVisibility(GONE)
}
}

private fun getBottomMarginForDonationPopup(): Int {
var bottomMargin = requireActivity().resources.getDimensionPixelSize(
R.dimen.donation_popup_bottom_margin
)
val bottomAppBar = requireActivity()
.findViewById<BottomAppBar>(R.id.bottom_toolbar)
if (bottomAppBar.visibility == VISIBLE) {
// if bottomAppBar is visible then add the height of the bottomAppBar.
bottomMargin += requireActivity().resources.getDimensionPixelSize(
R.dimen.material_minimum_height_and_width
)
bottomMargin += requireActivity().resources.getDimensionPixelSize(R.dimen.card_margin)
}

return bottomMargin
}

protected open fun openKiwixSupportUrl() {
(requireActivity() as CoreMainActivity).openSupportKiwixExternalLink()
}

private fun setDonationLayoutVisibility(visibility: Int) {
donationLayout?.visibility = visibility
}

private fun openFullScreenIfEnabled() {
Expand Down Expand Up @@ -2388,6 +2473,10 @@ abstract class CoreReaderFragment :
unbindService()
}

override fun showDonationDialog() {
showDonationLayout()
}

private fun bindService() {
requireActivity().bindService(
Intent(requireActivity(), ReadAloudService::class.java), serviceConnection,
Expand Down
Loading

0 comments on commit f2534d3

Please sign in to comment.