Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.yogeshpaliyal.deepr.viewmodel.SortType
Expand All @@ -21,6 +22,7 @@ class AppPreferenceDataStore(
private val USE_LINK_BASED_ICONS = booleanPreferencesKey("use_link_based_icons")
private val SYNC_ENABLED = booleanPreferencesKey("sync_enabled")
private val SYNC_FILE_PATH = stringPreferencesKey("sync_file_path")
private val LAST_SYNC_TIME = longPreferencesKey("last_sync_time")
}

val getSortingOrder: Flow<@SortType String> =
Expand All @@ -43,6 +45,11 @@ class AppPreferenceDataStore(
preferences[SYNC_FILE_PATH] ?: "" // Default to empty path
}

val getLastSyncTime: Flow<Long> =
context.appDataStore.data.map { preferences ->
preferences[LAST_SYNC_TIME] ?: 0L // Default to 0 (never synced)
}

suspend fun setSortingOrder(order: @SortType String) {
context.appDataStore.edit { prefs ->
prefs[SORTING_ORDER] = order
Expand All @@ -66,4 +73,10 @@ class AppPreferenceDataStore(
prefs[SYNC_FILE_PATH] = path
}
}

suspend fun setLastSyncTime(timestamp: Long) {
context.appDataStore.edit { prefs ->
prefs[LAST_SYNC_TIME] = timestamp
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ interface SyncRepository {
suspend fun syncToMarkdown(): RequestResult<String>

suspend fun validateMarkdownFile(filePath: String): RequestResult<Boolean>

suspend fun recordSyncTime()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withContext
import java.io.File
import java.io.OutputStream
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class SyncRepositoryImpl(
private val context: Context,
Expand Down Expand Up @@ -44,6 +47,8 @@ class SyncRepositoryImpl(
}

writeMarkdownData(it, dataToSync)
// Record sync time on successful completion
recordSyncTime()
RequestResult.Success(
context.getString(
R.string.sync_success,
Expand Down Expand Up @@ -84,15 +89,23 @@ class SyncRepositoryImpl(
}
}

override suspend fun recordSyncTime() {
val currentTime = System.currentTimeMillis()
preferenceDataStore.setLastSyncTime(currentTime)
}

private fun writeMarkdownData(
file: OutputStream,
data: List<Deepr>,
) {
file.use { outputStream ->
outputStream.bufferedWriter().use { writer ->
// Write header comment
// Write header comment with sync time
val syncTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date())
writer.write("<!-- Deepr Sync File - Do not modify the table structure -->\n")
writer.write("<!-- Last Synced: $syncTime -->\n")
writer.write("# Deeplinks\n\n")
writer.write("**Last Sync:** $syncTime\n\n")
writer.write("**Warning:** Please maintain the markdown table format when editing this file.\n\n")

// Write markdown table header
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/com/yogeshpaliyal/deepr/ui/screens/Settings.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yogeshpaliyal.deepr.ui.screens

import android.Manifest
import android.content.Intent
import android.os.Build
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
Expand Down Expand Up @@ -54,6 +55,9 @@ import compose.icons.tablericons.Settings
import compose.icons.tablericons.Upload
import kotlinx.coroutines.flow.collectLatest
import org.koin.androidx.compose.koinViewModel
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

data object Settings

Expand Down Expand Up @@ -81,13 +85,21 @@ fun SettingsScreen(
// Collect sync preference states
val syncEnabled by viewModel.syncEnabled.collectAsStateWithLifecycle()
val syncFilePath by viewModel.syncFilePath.collectAsStateWithLifecycle()
val lastSyncTime by viewModel.lastSyncTime.collectAsStateWithLifecycle()

// Launcher for picking sync file location
val syncFileLauncher =
rememberLauncherForActivityResult(
contract = ActivityResultContracts.CreateDocument("text/markdown"),
) { uri ->
uri?.let {
val contentResolver = context.contentResolver

val takeFlags: Int =
Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
// Check for the freshest data.
contentResolver.takePersistableUriPermission(uri, takeFlags)
viewModel.setSyncFilePath(it.toString())
}
}
Expand Down Expand Up @@ -245,6 +257,27 @@ fun SettingsScreen(
},
)

// Show last sync time if sync is enabled
ListItem(
headlineContent = { Text(stringResource(R.string.last_sync_time)) },
supportingContent = {
Text(
if (lastSyncTime > 0) {
val formatter = SimpleDateFormat("MMM dd, yyyy 'at' HH:mm", Locale.getDefault())
stringResource(R.string.last_sync_time_format, formatter.format(Date(lastSyncTime)))
} else {
stringResource(R.string.last_sync_time_never)
},
)
},
leadingContent = {
Icon(
TablerIcons.InfoCircle,
contentDescription = stringResource(R.string.last_sync_time),
)
},
)

if (syncFilePath.isNotEmpty()) {
ListItem(
modifier =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ class AccountViewModel(
preferenceDataStore.getSyncFilePath
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), "")

val lastSyncTime =
preferenceDataStore.getLastSyncTime
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), 0L)

fun setSyncEnabled(enabled: Boolean) {
viewModelScope.launch(Dispatchers.IO) {
preferenceDataStore.setSyncEnabled(enabled)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
<string name="sync_status_no_file">No sync file selected</string>
<string name="sync_now">Sync Now</string>
<string name="sync_now_description">Manually sync deeplinks to selected file</string>
<string name="last_sync_time">Last Sync</string>
<string name="last_sync_time_never">Never synced</string>
<string name="last_sync_time_format">%s</string>

<!-- Sync Messages -->
<string name="sync_disabled">Sync is disabled</string>
Expand Down