Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.
Rust androiddocument-scannerdocximage-editorjetpack-composekotlinliquid-glassofflineopen-sourcepdfpdf-compressorpdf-editorpdf-mergerpdf-readerpdf-splitterpdf-toolspdf-viewerpptxspreadsheet-editorxlsx
Ask for a GitHub star at a good moment, not after a fixed count
chethan61662ef5f1be8a1
diff --git a/DESIGN.md b/DESIGN.mdindex d51404e..12d772b 100644--- a/DESIGN.md+++ b/DESIGN.md@@ -167,6 +167,9 @@ by hand must do the same. `settle`, `fade` and layout springs never bounce and ititles SemiBold, values in the secondary colour.- Menus are compact: rows about 40 dp, menu width about 228 dp.- Every popover, menu and sheet dismisses on Back and on an outside tap.+- Asks the app makes of people (the GitHub star prompt) wait for a calm moment after something+ went right: never at launch, mid-task, in a document opened from another app, or after a failure,+ and they back off when dismissed. `GitHubStarPromptManager` holds the rules; don't add fixed counts.- Strings live in `values/strings.xml`. Never hard-code user-facing text.---diff --git a/app/src/main/java/com/chethan616/clearpdf/data/repository/Preferences.kt b/app/src/main/java/com/chethan616/clearpdf/data/repository/Preferences.ktindex e760f21..099edba 100644--- a/app/src/main/java/com/chethan616/clearpdf/data/repository/Preferences.kt+++ b/app/src/main/java/com/chethan616/clearpdf/data/repository/Preferences.kt@@ -252,47 +252,104 @@ object AppSettingsManager {}/**- * Tracks usage for the GitHub star prompt.- * Best practice prompt rules:- * - Requires at least 10 successful PDF interactions- * - Permanently disables after user stars or clicks "Don't show again"- * - Snoozes for 30 days if user clicks "Not now"+ * Decides when to ask for a GitHub star from how the person really uses the app, not a fixed count.+ * Everything stays on the device.+ *+ * - **Earned.** A task that produced a file (merge, compress, scan, export...) scores+ * [SUCCESS_POINTS]; opening a document scores [OPEN_POINTS]. Nothing is asked below [SCORE_NEEDED].+ * - **A returning user.** Used on [ACTIVE_DAYS_NEEDED] different days and installed at least+ * [MIN_INSTALL_DAYS] days ago, so the first sessions are never interrupted.+ * - **A good moment.** Only after something went right in this launch (a file made, a document+ * read), and only once the person is back on Home or Tools (the app checks that). Never at launch,+ * never mid-task.+ * - **Not after a failure.** A task that failed in the last [FAILURE_QUIET_MS] holds it back.+ * - **Backs off.** "Not now" snoozes for [SNOOZE_DAYS] in turn and the score starts over; once+ * those run out, the next "Not now" is taken as a no. A star or "Don't show again" ends it. At most+ * one ask per launch.*/object GitHubStarPromptManager {private const val PREFS_NAME = "clearpdf_settings"- private const val KEY_INTERACTION_COUNT = "github_star_interaction_count"+ /** The old fixed counter (3 interactions); carried into the score once. */+ private const val KEY_LEGACY_COUNT = "github_star_interaction_count"+ private const val KEY_SCORE = "github_star_score"+ private const val KEY_ACTIVE_DAYS = "github_star_active_days"+ private const val KEY_LAST_ACTIVE_DAY = "github_star_last_active_day"+ private const val KEY_LAST_FAILURE_MS = "github_star_last_failure_ms"+ private const val KEY_DISMISS_COUNT = "github_star_dismiss_count"private const val KEY_PROMPT_SNOOZE_UNTIL_MS = "github_star_prompt_snooze_until_ms"private const val KEY_NEVER_SHOW = "github_star_never_show"- private const val INTERACTION_THRESHOLD = 3- private const val THIRTY_DAYS_MS = 30L * 24L * 60L * 60L * 1000L+ private const val SUCCESS_POINTS = 3+ private const val OPEN_POINTS = 1+ private const val SCORE_NEEDED = 9+ private const val ACTIVE_DAYS_NEEDED = 3+ private const val MIN_INSTALL_DAYS = 3+ private const val DAY_MS = 24L * 60L * 60L * 1000L+ private const val FAILURE_QUIET_MS = 10L * 60L * 1000L+ private val SNOOZE_DAYS = intArrayOf(21, 60)const val REPO_URL: String = "https://github.com/Chethan616/ClearPDF"+ /** Something went right in this launch; the app may ask at its next calm moment. */+ @Volatile private var positiveMoment = false+ @Volatile private var askedThisLaunch = false+private fun prefs(context: Context): SharedPreferences =context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)- fun recordPdfInteraction(context: Context): Boolean {- val preferences = prefs(context)- if (preferences.getBoolean(KEY_NEVER_SHOW, false)) return false+ /** Once per launch: counts the distinct days the app is used. */+ fun recordActiveDay(context: Context) {+ val p = prefs(context)+ val today = System.currentTimeMillis() / DAY_MS+ if (p.getLong(KEY_LAST_ACTIVE_DAY, -1L) != today) {+ p.edit()+ .putLong(KEY_LAST_ACTIVE_DAY, today)+ .putInt(KEY_ACTIVE_DAYS, p.getInt(KEY_ACTIVE_DAYS, 0) + 1)+ .apply()+ }+ }- val now = System.currentTimeMillis()- val snoozeUntil = preferences.getLong(KEY_PROMPT_SNOOZE_UNTIL_MS, 0L)- if (now < snoozeUntil) return false+ /** A task produced a file. True when this would be a good time to ask. */+ fun recordSuccess(context: Context): Boolean = record(context, SUCCESS_POINTS)- val nextCount = preferences.getInt(KEY_INTERACTION_COUNT, 0) + 1- preferences.edit().putInt(KEY_INTERACTION_COUNT, nextCount).apply()- return nextCount >= INTERACTION_THRESHOLD+ /** A document opened and rendered. True when this would be a good time to ask. */+ fun recordDocumentOpened(context: Context): Boolean = record(context, OPEN_POINTS)++ /** A task failed: nobody is asked for a star right after the app let them down. */+ fun recordFailure(context: Context) {+ prefs(context).edit().putLong(KEY_LAST_FAILURE_MS, System.currentTimeMillis()).apply()}- fun shouldShowPrompt(context: Context): Boolean {- val preferences = prefs(context)- if (preferences.getBoolean(KEY_NEVER_SHOW, false)) return false+ private fun record(context: Context, points: Int): Boolean {+ val p = prefs(context)+ if (p.getBoolean(KEY_NEVER_SHOW, false)) return false+ p.edit().putInt(KEY_SCORE, score(p) + points).apply()+ positiveMoment = true+ return shouldShowPrompt(context)+ }+ private fun score(p: SharedPreferences): Int =+ p.getInt(KEY_SCORE, -1).takeIf { it >= 0 } ?: (p.getInt(KEY_LEGACY_COUNT, 0) * SUCCESS_POINTS)++ private fun installedAt(context: Context): Long = runCatching {+ context.packageManager.getPackageInfo(context.packageName, 0).firstInstallTime+ }.getOrDefault(0L)++ fun shouldShowPrompt(context: Context): Boolean {+ if (askedThisLaunch || !positiveMoment) return false+ val p = prefs(context)+ if (p.getBoolean(KEY_NEVER_SHOW, false)) return falseval now = System.currentTimeMillis()- val snoozeUntil = preferences.getLong(KEY_PROMPT_SNOOZE_UNTIL_MS, 0L)- if (now < snoozeUntil) return false- return preferences.getInt(KEY_INTERACTION_COUNT, 0) >= INTERACTION_THRESHOLD+ if (now < p.getLong(KEY_PROMPT_SNOOZE_UNTIL_MS, 0L)) return false+ if (now - p.getLong(KEY_LAST_FAILURE_MS, 0L) < FAILURE_QUIET_MS) return false+ if (now - installedAt(context) < MIN_INSTALL_DAYS * DAY_MS) return false+ if (p.getInt(KEY_ACTIVE_DAYS, 0) < ACTIVE_DAYS_NEEDED) return false+ return score(p) >= SCORE_NEEDED+ }++ /** The prompt is on screen: no second ask in this launch, whatever the person picks. */+ fun onPromptShown() {+ askedThisLaunch = true}fun onPromptAccepted(context: Context) {@@ -300,17 +357,23 @@ object GitHubStarPromptManager {}fun onPromptDismissed(context: Context) {- val snoozeUntil = System.currentTimeMillis() + THIRTY_DAYS_MS- prefs(context).edit()- .putInt(KEY_INTERACTION_COUNT, 0)- .putLong(KEY_PROMPT_SNOOZE_UNTIL_MS, snoozeUntil)+ val p = prefs(context)+ val dismissals = p.getInt(KEY_DISMISS_COUNT, 0) + 1+ if (dismissals > SNOOZE_DAYS.size) {+ setNeverShowAgain(context)+ return+ }+ p.edit()+ .putInt(KEY_DISMISS_COUNT, dismissals)+ .putInt(KEY_SCORE, 0)+ .putLong(KEY_PROMPT_SNOOZE_UNTIL_MS, System.currentTimeMillis() + SNOOZE_DAYS[dismissals - 1] * DAY_MS).apply()}fun setNeverShowAgain(context: Context) {prefs(context).edit().putBoolean(KEY_NEVER_SHOW, true)- .putInt(KEY_INTERACTION_COUNT, 0)+ .putInt(KEY_SCORE, 0).apply()}}diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/DocsApp.kt b/app/src/main/java/com/chethan616/clearpdf/ui/DocsApp.ktindex dc5e279..cefb489 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/DocsApp.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/DocsApp.kt@@ -75,7 +75,6 @@ import com.chethan616.clearpdf.ui.components.LocalGlassOverlayHostimport com.chethan616.clearpdf.ui.components.LocalBackToLibraryActionimport com.kyant.backdrop.backdrops.layerBackdropimport com.kyant.backdrop.backdrops.rememberLayerBackdrop-import kotlinx.coroutines.flow.collectLatest/** Walk the ContextWrapper chain to the hosting Activity (for locale-change recreate). */private tailrec fun android.content.Context.findActivity(): android.app.Activity? = when (this) {@@ -148,14 +147,11 @@ fun DocsApp(shortcutRoute: String? = null, incomingPdfUri: android.net.Uri? = nuOnboardingManager.shouldShowWhatsNew(context)}+ // A finished task only reports that a good moment happened; the ask itself waits for a calm+ // screen (below). It no longer pops up at launch.+ var starMoment by remember { mutableIntStateOf(0) }LaunchedEffect(context) {- if (showWhatsNew) return@LaunchedEffect- showStarPrompt = GitHubStarPromptManager.shouldShowPrompt(context)- StarPromptEventBus.promptRequests.collectLatest {- if (GitHubStarPromptManager.shouldShowPrompt(context)) {- showStarPrompt = true- }- }+ StarPromptEventBus.promptRequests.collect { starMoment++ }}val openRepo = remember(context) {@@ -267,6 +263,20 @@ fun DocsApp(shortcutRoute: String? = null, incomingPdfUri: android.net.Uri? = nu}}+ // The star ask, at a calm moment only: back on Home or Tools after something went right in+ // this launch (GitHubStarPromptManager holds the rest: earned use, a returning user, no+ // recent failure, backing off). Never mid-task, never in a document opened from another+ // app, never on a launch that showed the tour or What's New. Leaving the tab cancels it.+ LaunchedEffect(currentRoute, starMoment) {+ if (showWhatsNew || needsOnboarding || documentWindow) return@LaunchedEffect+ if (currentRoute != "home" && currentRoute != "tools") return@LaunchedEffect+ kotlinx.coroutines.delay(900)+ if (GitHubStarPromptManager.shouldShowPrompt(context)) {+ GitHubStarPromptManager.onPromptShown()+ showStarPrompt = true+ }+ }+// Keep tab highlight synced even when navigation occurs from cards/deep links.LaunchedEffect(currentRoute) {selectedTab = when (currentRoute) {@@ -290,6 +300,7 @@ fun DocsApp(shortcutRoute: String? = null, incomingPdfUri: android.net.Uri? = nuremember(context) {com.chethan616.clearpdf.ui.components.GlassSettings.load(context)com.chethan616.clearpdf.ui.components.MotionSettings.load(context)+ GitHubStarPromptManager.recordActiveDay(context)true}val platformHaptics = androidx.compose.ui.platform.LocalHapticFeedback.currentdiff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/ScanDocumentScreen.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/ScanDocumentScreen.ktindex 8937e04..9b8147b 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/ScanDocumentScreen.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/ScanDocumentScreen.kt@@ -499,7 +499,7 @@ fun ScanDocumentScreen(viewModel.cancelScanning()if (pdfUri != null) {viewModel.markAsSaved(pdfUri)- if (GitHubStarPromptManager.recordPdfInteraction(context)) {+ if (GitHubStarPromptManager.recordSuccess(context)) {StarPromptEventBus.requestPrompt()}Toast.makeText(context, context.getString(R.string.pdf_saved_successfully), Toast.LENGTH_SHORT).show()diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/CompressPdfViewModel.kt b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/CompressPdfViewModel.ktindex 72d862d..9f4ed2e 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/CompressPdfViewModel.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/CompressPdfViewModel.kt@@ -177,10 +177,11 @@ class CompressPdfViewModel(private val compressPdfUseCase: CompressPdfUseCase) :resultMessage = context.getString(R.string.compression_success, origKb, compKb, reduction, saveLabel))- if (GitHubStarPromptManager.recordPdfInteraction(context)) {+ if (GitHubStarPromptManager.recordSuccess(context)) {StarPromptEventBus.requestPrompt()}} catch (e: Exception) {+ GitHubStarPromptManager.recordFailure(context)_uiState.value = _uiState.value.copy(isCompressing = false,errorMessage = context.getString(R.string.compression_failed)diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/CreatePdfViewModel.kt b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/CreatePdfViewModel.ktindex b565960..9da2fbd 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/CreatePdfViewModel.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/CreatePdfViewModel.kt@@ -172,10 +172,11 @@ class CreatePdfViewModel(private val createPdfUseCase: CreatePdfUseCase) : ViewM)}- if (GitHubStarPromptManager.recordPdfInteraction(context)) {+ if (GitHubStarPromptManager.recordSuccess(context)) {StarPromptEventBus.requestPrompt()}} catch (e: Exception) {+ GitHubStarPromptManager.recordFailure(context)_uiState.update {it.copy(isCreating = false, errorMessage = context.getString(R.string.create_failed))}diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/ImagesToPdfViewModel.kt b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/ImagesToPdfViewModel.ktindex 4a2c460..37960fe 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/ImagesToPdfViewModel.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/ImagesToPdfViewModel.kt@@ -87,8 +87,9 @@ class ImagesToPdfViewModel(private val converter: PdfConverter) : ViewModel() {saveLocationLabel = saveLabel,resultMessage = context.getString(R.string.images_success, images.size, saveLabel))- if (GitHubStarPromptManager.recordPdfInteraction(context)) StarPromptEventBus.requestPrompt()+ if (GitHubStarPromptManager.recordSuccess(context)) StarPromptEventBus.requestPrompt()} catch (e: Exception) {+ GitHubStarPromptManager.recordFailure(context)_uiState.value = _uiState.value.copy(isSaving = false, errorMessage = context.getString(R.string.conversion_failed))}}diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/MergePdfViewModel.kt b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/MergePdfViewModel.ktindex a3ba911..9e63c2c 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/MergePdfViewModel.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/MergePdfViewModel.kt@@ -122,10 +122,11 @@ class MergePdfViewModel(private val mergePdfUseCase: MergePdfUseCase) : ViewModeresultMessage = context.getString(R.string.merge_success, uris.size, outName, saveLabel))- if (GitHubStarPromptManager.recordPdfInteraction(context)) {+ if (GitHubStarPromptManager.recordSuccess(context)) {StarPromptEventBus.requestPrompt()}} catch (e: Exception) {+ GitHubStarPromptManager.recordFailure(context)_uiState.value = _uiState.value.copy(isMerging = false,errorMessage = context.getString(R.string.merge_failed)diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PageOrganizerViewModel.kt b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PageOrganizerViewModel.ktindex fdc30bb..057165a 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PageOrganizerViewModel.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PageOrganizerViewModel.kt@@ -190,9 +190,10 @@ class PageOrganizerViewModel(private val editor: PdfEditor) : ViewModel() {savedLayout = layoutOf(pages))onSaved?.invoke()- val shouldPrompt = withContext(Dispatchers.IO) { GitHubStarPromptManager.recordPdfInteraction(context) }+ val shouldPrompt = withContext(Dispatchers.IO) { GitHubStarPromptManager.recordSuccess(context) }if (shouldPrompt) StarPromptEventBus.requestPrompt()} catch (e: Exception) {+ GitHubStarPromptManager.recordFailure(context)_uiState.value = _uiState.value.copy(isSaving = false, errorMessage = context.getString(R.string.organize_save_failed))}}diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.kt b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.ktindex 433be64..d67d03a 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.kt@@ -422,7 +422,7 @@ class PdfViewerViewModel(private val openPdfUseCase: OpenPdfUseCase) : ViewModelpageCount = doc.pageCount,sizeBytes = doc.sizeBytes))- if (GitHubStarPromptManager.recordPdfInteraction(context)) {+ if (GitHubStarPromptManager.recordDocumentOpened(context)) {StarPromptEventBus.requestPrompt()}renderPage(context, 0, DEFAULT_RENDER_WIDTH)@@ -823,10 +823,11 @@ class PdfViewerViewModel(private val openPdfUseCase: OpenPdfUseCase) : ViewModelexportMessage = context.getString(R.string.viewer_save_success, outputName),lastExportedUri = outputUri)- if (GitHubStarPromptManager.recordPdfInteraction(context)) {+ if (GitHubStarPromptManager.recordSuccess(context)) {StarPromptEventBus.requestPrompt()}} catch (_: Exception) {+ GitHubStarPromptManager.recordFailure(context)_uiState.value = _uiState.value.copy(isExporting = false,exportError = context.getString(R.string.viewer_save_failed)diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/SplitPdfViewModel.kt b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/SplitPdfViewModel.ktindex e07f695..5abd93c 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/SplitPdfViewModel.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/SplitPdfViewModel.kt@@ -176,10 +176,11 @@ class SplitPdfViewModel(private val splitter: PdfSplitter) : ViewModel() {resultMessage = context.getString(R.string.split_success, results.size, saveLabel))- if (GitHubStarPromptManager.recordPdfInteraction(context)) {+ if (GitHubStarPromptManager.recordSuccess(context)) {StarPromptEventBus.requestPrompt()}} catch (e: Exception) {+ GitHubStarPromptManager.recordFailure(context)_uiState.value = _uiState.value.copy(isSplitting = false,errorMessage = context.getString(R.string.split_failed)@@ -210,10 +211,11 @@ class SplitPdfViewModel(private val splitter: PdfSplitter) : ViewModel() {resultMessage = context.getString(R.string.extract_success, pages.size, saveLabel))- if (GitHubStarPromptManager.recordPdfInteraction(context)) {+ if (GitHubStarPromptManager.recordSuccess(context)) {StarPromptEventBus.requestPrompt()}} catch (e: Exception) {+ GitHubStarPromptManager.recordFailure(context)_uiState.value = _uiState.value.copy(isSplitting = false,errorMessage = context.getString(R.string.extract_failed)