Skip to content

Commit 281e081

Browse files
committed
make prompt star
1 parent 907b1fa commit 281e081

5 files changed

Lines changed: 211 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.seenot.app.config
2+
3+
import android.content.Context
4+
import android.content.SharedPreferences
5+
6+
object GitHubStarPromptPrefs {
7+
private const val PREFS_NAME = "seenot_github_star_prompt"
8+
private const val KEY_PENDING_HOME_PROMPT = "pending_home_prompt"
9+
private const val KEY_SHOWN = "shown"
10+
private const val KEY_DISMISSED_FOREVER = "dismissed_forever"
11+
12+
private const val TRIGGER_CONTROLLED_APP_COUNT = 3
13+
14+
private fun prefs(context: Context): SharedPreferences {
15+
return context.applicationContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
16+
}
17+
18+
fun onControlledAppCountChanged(context: Context, previousCount: Int, currentCount: Int) {
19+
if (previousCount < TRIGGER_CONTROLLED_APP_COUNT && currentCount >= TRIGGER_CONTROLLED_APP_COUNT) {
20+
scheduleIfAllowed(context)
21+
}
22+
}
23+
24+
fun onControlledAppCountSeen(context: Context, currentCount: Int) {
25+
if (currentCount >= TRIGGER_CONTROLLED_APP_COUNT) {
26+
scheduleIfAllowed(context)
27+
}
28+
}
29+
30+
fun shouldShowOnHome(context: Context): Boolean {
31+
val prefs = prefs(context)
32+
return prefs.getBoolean(KEY_PENDING_HOME_PROMPT, false) &&
33+
!prefs.getBoolean(KEY_SHOWN, false) &&
34+
!prefs.getBoolean(KEY_DISMISSED_FOREVER, false)
35+
}
36+
37+
fun markShown(context: Context) {
38+
prefs(context)
39+
.edit()
40+
.putBoolean(KEY_SHOWN, true)
41+
.putBoolean(KEY_PENDING_HOME_PROMPT, false)
42+
.apply()
43+
}
44+
45+
fun dismissForever(context: Context) {
46+
prefs(context)
47+
.edit()
48+
.putBoolean(KEY_DISMISSED_FOREVER, true)
49+
.putBoolean(KEY_PENDING_HOME_PROMPT, false)
50+
.apply()
51+
}
52+
53+
private fun scheduleIfAllowed(context: Context) {
54+
val prefs = prefs(context)
55+
if (!prefs.getBoolean(KEY_SHOWN, false) && !prefs.getBoolean(KEY_DISMISSED_FOREVER, false)) {
56+
prefs.edit().putBoolean(KEY_PENDING_HOME_PROMPT, true).apply()
57+
}
58+
}
59+
}

app/src/main/java/com/seenot/app/ui/screens/MainScreen.kt

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import com.seenot.app.config.SttSettings
7777
import com.seenot.app.config.recommendedSttModelPresets
7878
import com.seenot.app.config.selectableProviders
7979
import com.seenot.app.config.selectableSttProviders
80+
import com.seenot.app.config.GitHubStarPromptPrefs
8081
import com.seenot.app.config.IntentReminderPrefs
8182
import com.seenot.app.config.NoMonitorReminderPrefs
8283
import com.seenot.app.config.RuleRecordingPrefs
@@ -151,6 +152,7 @@ fun MainScreen(
151152
var versionCheckResponse by remember { mutableStateOf(SeenotVersionCheckPrefs.getLastVersionCheckResponse(context)) }
152153
var visibleVersionUpdateDialog by remember { mutableStateOf<SeenotVersionCheckResponse?>(null) }
153154
var isVersionCheckInFlight by remember { mutableStateOf(false) }
155+
var showGitHubStarPrompt by remember { mutableStateOf(false) }
154156

155157
// State
156158
var selectedTab by remember { mutableIntStateOf(0) }
@@ -336,11 +338,26 @@ fun MainScreen(
336338

337339
LaunchedEffect(Unit) {
338340
controlledAppCount = sessionManager.controlledApps.value.size
341+
GitHubStarPromptPrefs.onControlledAppCountSeen(context, controlledAppCount)
342+
var previousCount = controlledAppCount
339343
sessionManager.controlledApps.collectLatest {
344+
GitHubStarPromptPrefs.onControlledAppCountChanged(
345+
context = context,
346+
previousCount = previousCount,
347+
currentCount = it.size
348+
)
349+
GitHubStarPromptPrefs.onControlledAppCountSeen(context, it.size)
350+
previousCount = it.size
340351
controlledAppCount = it.size
341352
}
342353
}
343354

355+
LaunchedEffect(selectedTab, controlledAppCount) {
356+
if (selectedTab == 0 && GitHubStarPromptPrefs.shouldShowOnHome(context)) {
357+
showGitHubStarPrompt = true
358+
}
359+
}
360+
344361
LaunchedEffect(Unit) {
345362
globalMonitoringPause = sessionManager.globalMonitoringPause.value
346363
sessionManager.globalMonitoringPause.collectLatest {
@@ -685,6 +702,24 @@ fun MainScreen(
685702
)
686703
}
687704

705+
if (showGitHubStarPrompt) {
706+
GitHubStarPromptDialog(
707+
onOpenGitHub = {
708+
GitHubStarPromptPrefs.markShown(context)
709+
showGitHubStarPrompt = false
710+
openExternalUrl(context, "https://github.com/RoderickQiu/seenot-app")
711+
},
712+
onLater = {
713+
GitHubStarPromptPrefs.markShown(context)
714+
showGitHubStarPrompt = false
715+
},
716+
onNever = {
717+
GitHubStarPromptPrefs.dismissForever(context)
718+
showGitHubStarPrompt = false
719+
}
720+
)
721+
}
722+
688723
pendingPermissionGuide?.let { guideType ->
689724
PermissionGuideDialog(
690725
type = guideType,
@@ -810,6 +845,44 @@ fun MainScreen(
810845
}
811846
}
812847

848+
@Composable
849+
private fun GitHubStarPromptDialog(
850+
onOpenGitHub: () -> Unit,
851+
onLater: () -> Unit,
852+
onNever: () -> Unit
853+
) {
854+
AlertDialog(
855+
onDismissRequest = onLater,
856+
icon = {
857+
Icon(
858+
imageVector = Icons.Outlined.StarBorder,
859+
contentDescription = null
860+
)
861+
},
862+
title = {
863+
Text(stringResource(R.string.github_star_prompt_title))
864+
},
865+
text = {
866+
Text(stringResource(R.string.github_star_prompt_message))
867+
},
868+
confirmButton = {
869+
TextButton(onClick = onOpenGitHub) {
870+
Text(stringResource(R.string.github_star_prompt_open_action))
871+
}
872+
},
873+
dismissButton = {
874+
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
875+
TextButton(onClick = onNever) {
876+
Text(stringResource(R.string.github_star_prompt_never_action))
877+
}
878+
TextButton(onClick = onLater) {
879+
Text(stringResource(R.string.github_star_prompt_later_action))
880+
}
881+
}
882+
}
883+
)
884+
}
885+
813886
private fun isSeenotAccessibilityEnabled(context: Context): Boolean {
814887
val expectedServiceId = "${context.packageName}/com.seenot.app.service.SeenotAccessibilityService"
815888
val accessibilityManager = context.getSystemService(Context.ACCESSIBILITY_SERVICE) as? AccessibilityManager

app/src/main/res/values-en/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,11 @@
814814
<string name="version_update_dialog_published_at">Published: %s</string>
815815
<string name="version_update_dialog_download_action">Update</string>
816816
<string name="version_update_dialog_later_action">Later</string>
817+
<string name="github_star_prompt_title">Support SeeNot</string>
818+
<string name="github_star_prompt_message">SeeNot is open source. If you have a GitHub account, starring the project helps more people find it and shows that this direction is worth continuing.</string>
819+
<string name="github_star_prompt_open_action">Star on GitHub</string>
820+
<string name="github_star_prompt_later_action">Maybe later</string>
821+
<string name="github_star_prompt_never_action">Don\'t show again</string>
817822
<string name="about_official_site">Official Site</string>
818823
<string name="about_github">GitHub</string>
819824
<string name="about_github_desc">Source code, releases, and issue tracking</string>

app/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,11 @@
820820
<string name="version_update_dialog_published_at">发布时间:%s</string>
821821
<string name="version_update_dialog_download_action">去更新</string>
822822
<string name="version_update_dialog_later_action">稍后</string>
823+
<string name="github_star_prompt_title">支持 SeeNot</string>
824+
<string name="github_star_prompt_message">SeeNot 是开源项目。如果你有 GitHub 账号,点一个 Star 能让更多人发现它,也能让我知道这个方向值得继续做。</string>
825+
<string name="github_star_prompt_open_action">去 GitHub 点 Star</string>
826+
<string name="github_star_prompt_later_action">以后再说</string>
827+
<string name="github_star_prompt_never_action">不再提示</string>
823828
<string name="about_official_site">官网</string>
824829
<string name="about_github">GitHub</string>
825830
<string name="about_github_desc">查看源码、版本和问题反馈</string>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.seenot.app.config
2+
3+
import android.content.Context
4+
import androidx.test.core.app.ApplicationProvider
5+
import org.junit.Assert.assertFalse
6+
import org.junit.Assert.assertTrue
7+
import org.junit.Before
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
import org.robolectric.RobolectricTestRunner
11+
12+
@RunWith(RobolectricTestRunner::class)
13+
class GitHubStarPromptPrefsTest {
14+
private val context = ApplicationProvider.getApplicationContext<Context>()
15+
16+
@Before
17+
fun clearPrefs() {
18+
context.getSharedPreferences("seenot_github_star_prompt", Context.MODE_PRIVATE)
19+
.edit()
20+
.clear()
21+
.commit()
22+
}
23+
24+
@Test
25+
fun existingThreeOrMoreControlledAppsSchedulesPrompt() {
26+
GitHubStarPromptPrefs.onControlledAppCountSeen(context, currentCount = 5)
27+
28+
assertTrue(GitHubStarPromptPrefs.shouldShowOnHome(context))
29+
}
30+
31+
@Test
32+
fun enteringThreeOrMoreControlledAppsSchedulesPromptOnce() {
33+
assertFalse(GitHubStarPromptPrefs.shouldShowOnHome(context))
34+
35+
GitHubStarPromptPrefs.onControlledAppCountChanged(context, previousCount = 1, currentCount = 4)
36+
37+
assertTrue(GitHubStarPromptPrefs.shouldShowOnHome(context))
38+
39+
GitHubStarPromptPrefs.markShown(context)
40+
41+
assertFalse(GitHubStarPromptPrefs.shouldShowOnHome(context))
42+
}
43+
44+
@Test
45+
fun stayingAtThreeOrMoreControlledAppsDoesNotSchedulePrompt() {
46+
GitHubStarPromptPrefs.onControlledAppCountChanged(context, previousCount = 4, currentCount = 5)
47+
48+
assertFalse(GitHubStarPromptPrefs.shouldShowOnHome(context))
49+
}
50+
51+
@Test
52+
fun shownPromptDoesNotRescheduleAfterDeletingAndAddingApp() {
53+
GitHubStarPromptPrefs.onControlledAppCountChanged(context, previousCount = 2, currentCount = 3)
54+
GitHubStarPromptPrefs.markShown(context)
55+
56+
GitHubStarPromptPrefs.onControlledAppCountChanged(context, previousCount = 2, currentCount = 3)
57+
58+
assertFalse(GitHubStarPromptPrefs.shouldShowOnHome(context))
59+
}
60+
61+
@Test
62+
fun dismissForeverPreventsSchedulingAndShowing() {
63+
GitHubStarPromptPrefs.dismissForever(context)
64+
65+
GitHubStarPromptPrefs.onControlledAppCountChanged(context, previousCount = 1, currentCount = 4)
66+
67+
assertFalse(GitHubStarPromptPrefs.shouldShowOnHome(context))
68+
}
69+
}

0 commit comments

Comments
 (0)