Skip to content

Commit 5edbc00

Browse files
Add setting to disable clipboard link detection (#352)
* Initial plan * Add setting to disable clipboard link detection - Added clipboard_link_detection_enabled preference to AppPreferenceDataStore - Added getter/setter methods in AccountViewModel - Added UI toggle in Settings screen with Clipboard icon - Updated MainActivity to respect the preference setting - Added string resources for the new setting - Default value is true (enabled) to maintain existing behavior Co-authored-by: yogeshpaliyal <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yogeshpaliyal <[email protected]> Co-authored-by: Yogesh Choudhary Paliyal <[email protected]>
1 parent 748ab58 commit 5edbc00

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

app/src/main/java/com/yogeshpaliyal/deepr/MainActivity.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,29 @@ fun Dashboard(
192192
val hapticFeedback = LocalHapticFeedback.current
193193
val layoutDirection = LocalLayoutDirection.current
194194
val context = LocalContext.current
195+
val viewModel: AccountViewModel = koinViewModel()
196+
197+
// Collect clipboard link detection preference
198+
val clipboardLinkDetectionEnabled by viewModel.clipboardLinkDetectionEnabled.collectAsStateWithLifecycle()
195199

196200
// Clipboard link detection
197201
var clipboardLink by remember { mutableStateOf<ClipboardLink?>(null) }
198202

199-
LaunchedEffect(Unit) {
200-
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
201-
val clipData = clipboard.primaryClip
202-
if (clipData != null && clipData.itemCount > 0) {
203-
val text = clipData.getItemAt(0).text?.toString()
204-
if (!text.isNullOrBlank()) {
205-
val normalizedLink = normalizeLink(text)
206-
if (isValidDeeplink(normalizedLink)) {
207-
clipboardLink = ClipboardLink(normalizedLink)
203+
LaunchedEffect(clipboardLinkDetectionEnabled) {
204+
if (clipboardLinkDetectionEnabled) {
205+
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
206+
val clipData = clipboard.primaryClip
207+
if (clipData != null && clipData.itemCount > 0) {
208+
val text = clipData.getItemAt(0).text?.toString()
209+
if (!text.isNullOrBlank()) {
210+
val normalizedLink = normalizeLink(text)
211+
if (isValidDeeplink(normalizedLink)) {
212+
clipboardLink = ClipboardLink(normalizedLink)
213+
}
208214
}
209215
}
216+
} else {
217+
clipboardLink = null
210218
}
211219
}
212220

app/src/main/java/com/yogeshpaliyal/deepr/preference/AppPreferenceDataStore.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class AppPreferenceDataStore(
3939
private val SELECTED_PROFILE_ID = longPreferencesKey("selected_profile_id")
4040
private val GOOGLE_DRIVE_AUTO_BACKUP_ENABLED =
4141
booleanPreferencesKey("google_drive_auto_backup_enabled")
42+
private val CLIPBOARD_LINK_DETECTION_ENABLED =
43+
booleanPreferencesKey("clipboard_link_detection_enabled")
4244
}
4345

4446
val getSortingOrder: Flow<@SortType String> =
@@ -126,6 +128,11 @@ class AppPreferenceDataStore(
126128
preferences[GOOGLE_DRIVE_AUTO_BACKUP_ENABLED] ?: false // Default to disabled
127129
}
128130

131+
val getClipboardLinkDetectionEnabled: Flow<Boolean> =
132+
context.appDataStore.data.map { preferences ->
133+
preferences[CLIPBOARD_LINK_DETECTION_ENABLED] ?: true // Default to enabled
134+
}
135+
129136
suspend fun setSortingOrder(order: @SortType String) {
130137
context.appDataStore.edit { prefs ->
131138
prefs[SORTING_ORDER] = order
@@ -233,4 +240,10 @@ class AppPreferenceDataStore(
233240
prefs[GOOGLE_DRIVE_AUTO_BACKUP_ENABLED] = enabled
234241
}
235242
}
243+
244+
suspend fun setClipboardLinkDetectionEnabled(enabled: Boolean) {
245+
context.appDataStore.edit { prefs ->
246+
prefs[CLIPBOARD_LINK_DETECTION_ENABLED] = enabled
247+
}
248+
}
236249
}

app/src/main/java/com/yogeshpaliyal/deepr/ui/screens/Settings.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import com.yogeshpaliyal.deepr.viewmodel.AccountViewModel
5353
import compose.icons.TablerIcons
5454
import compose.icons.tablericons.AlertTriangle
5555
import compose.icons.tablericons.ArrowLeft
56+
import compose.icons.tablericons.Clipboard
5657
import compose.icons.tablericons.Download
5758
import compose.icons.tablericons.ExternalLink
5859
import compose.icons.tablericons.InfoCircle
@@ -103,6 +104,7 @@ fun SettingsScreen(
103104
val defaultPageFavourites by viewModel.defaultPageFavouritesEnabled.collectAsStateWithLifecycle()
104105
val isThumbnailEnable by viewModel.isThumbnailEnable.collectAsStateWithLifecycle()
105106
val showOpenCounter by viewModel.showOpenCounter.collectAsStateWithLifecycle()
107+
val clipboardLinkDetectionEnabled by viewModel.clipboardLinkDetectionEnabled.collectAsStateWithLifecycle()
106108

107109
Scaffold(
108110
contentWindowInsets = windowInsets,
@@ -287,6 +289,21 @@ fun SettingsScreen(
287289
)
288290
},
289291
)
292+
293+
SettingsItem(
294+
TablerIcons.Clipboard,
295+
title = stringResource(R.string.clipboard_link_detection),
296+
description = stringResource(R.string.clipboard_link_detection_description),
297+
onClick = {
298+
viewModel.setClipboardLinkDetectionEnabled(!clipboardLinkDetectionEnabled)
299+
},
300+
trailing = {
301+
Switch(
302+
checked = clipboardLinkDetectionEnabled,
303+
onCheckedChange = { viewModel.setClipboardLinkDetectionEnabled(it) },
304+
)
305+
},
306+
)
290307
}
291308

292309
SettingsSection("About") {

app/src/main/java/com/yogeshpaliyal/deepr/viewmodel/AccountViewModel.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,17 @@ class AccountViewModel(
675675
}
676676
}
677677

678+
// Clipboard link detection preference methods
679+
val clipboardLinkDetectionEnabled =
680+
preferenceDataStore.getClipboardLinkDetectionEnabled
681+
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), true)
682+
683+
fun setClipboardLinkDetectionEnabled(enabled: Boolean) {
684+
viewModelScope.launch(Dispatchers.IO) {
685+
preferenceDataStore.setClipboardLinkDetectionEnabled(enabled)
686+
}
687+
}
688+
678689
// Auto backup preference methods
679690
val autoBackupEnabled =
680691
preferenceDataStore.getAutoBackupEnabled

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,8 @@
275275
<string name="invalid_link_silent_save">Invalid link</string>
276276
<string name="failed_to_save_link">Failed to save link</string>
277277
<string name="link_already_exists">Link already exists</string>
278+
279+
<!-- Clipboard Link Detection Setting -->
280+
<string name="clipboard_link_detection">Clipboard link detection</string>
281+
<string name="clipboard_link_detection_description">Automatically detect links in clipboard and show a banner to add them</string>
278282
</resources>

0 commit comments

Comments
 (0)