Skip to content
Open
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
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ dependencies {
implementation(libs.ktor.client.cio)
implementation(libs.ktor.server.core)
implementation(libs.ktor.server.cio)
implementation(libs.ktor.server.websockets)
implementation(libs.ktor.client.websockets)

implementation(libs.ktor.server.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.ktor.client.content.negotiation)
Expand Down
101 changes: 96 additions & 5 deletions app/src/main/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,83 @@ <h3 class="text-xl font-semibold text-gray-700 mb-2">Loading links...</h3>
<script>
// Global variables to store all links and available tags
let allLinks = [];
let availableTags = []; // Now contains {id, name, count} objects
let selectedTags = []; // Now contains {id, name} objects
let availableTags = [];
let selectedTags = [];
let linksSocket = null;
let reconnectTimer = null;
let hasConnectedToSocket = false;
const WS_RETRY_MS = 5000;

// WebSocket connection management
function connectLinksWebSocket() {
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
const wsUrl = `${protocol}://${window.location.host}/ws/updates`;

try {
linksSocket?.close();
linksSocket = new WebSocket(wsUrl);
} catch (error) {
console.error('Unable to create websocket', error);
scheduleSocketReconnect();
return;
}

linksSocket.addEventListener('open', () => {
hasConnectedToSocket = true;
if (reconnectTimer) {
clearTimeout(reconnectTimer);
reconnectTimer = null;
}
showToast('Live updates enabled', 'info');
});

linksSocket.addEventListener('message', (event) => handleSocketMessage(event.data));

linksSocket.addEventListener('close', () => {
if (hasConnectedToSocket) {
showToast('Live updates disconnected. Retrying...', 'error');
}
scheduleSocketReconnect();
});

linksSocket.addEventListener('error', () => {
linksSocket?.close();
});
}

function handleSocketMessage(rawData) {
try {
const parsed = JSON.parse(rawData);
switch(parsed.dataType){
case 'links_list':
allLinks = parsed.links;
populateTagFilter();
filterAndDisplayLinks();
break;
default:
console.warn('Unknown dataType received from websocket:', parsed.dataType);
}
} catch (error) {
console.error('Failed parsing websocket payload', error);
}
}

function scheduleSocketReconnect() {
if (reconnectTimer) {
return;
}
reconnectTimer = setTimeout(() => {
reconnectTimer = null;
connectLinksWebSocket();
}, WS_RETRY_MS);
}

function disconnectWebSocket() {
if (websocket) {
websocket.close();
websocket = null;
}
}

// Toast notification system
function showToast(message, type = 'success') {
Expand Down Expand Up @@ -676,7 +751,13 @@ <h3 class="text-xl font-semibold text-gray-700 mb-2">Error loading links</h3>
tagsInput.value = '';
selectedTags = [];
updateSelectedTagsDisplay();
loadLinks();

// If WebSocket is not connected, fallback to HTTP
if (!websocket || websocket.readyState !== WebSocket.OPEN) {
loadLinks();
}
// Otherwise, WebSocket will automatically send updated data

loadAvailableTags();

button.innerHTML = `
Expand Down Expand Up @@ -764,12 +845,22 @@ <h3 class="text-xl font-semibold text-gray-700 mb-2">Error loading links</h3>

// Form and refresh
document.getElementById('addLinkForm').addEventListener('submit', addLink);
document.getElementById('refreshBtn').addEventListener('click', loadLinks);
document.getElementById('refreshBtn').addEventListener('click', function() {
if (websocket && websocket.readyState === WebSocket.OPEN) {
showToast('Using real-time connection', 'info');
} else {
connectLinksWebSocket();
}
});

// Initialize
updateSortOrderIcon();
loadLinks();
loadAvailableTags();
connectLinksWebSocket();
});

window.addEventListener('beforeunload', () => {
linksSocket?.close();
});
</script>
</body>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/yogeshpaliyal/deepr/DeeprApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.yogeshpaliyal.deepr.backup.ExportRepository
import com.yogeshpaliyal.deepr.backup.ExportRepositoryImpl
import com.yogeshpaliyal.deepr.backup.ImportRepository
import com.yogeshpaliyal.deepr.backup.ImportRepositoryImpl
import com.yogeshpaliyal.deepr.data.DataProvider
import com.yogeshpaliyal.deepr.data.HtmlParser
import com.yogeshpaliyal.deepr.data.NetworkRepository
import com.yogeshpaliyal.deepr.preference.AppPreferenceDataStore
Expand Down Expand Up @@ -133,6 +134,10 @@ class DeeprApplication : Application() {
ReviewManagerFactory.create()
}

single<DataProvider> {
DataProvider(get())
}

viewModel {
TransferLinkLocalServerViewModel(get())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class CsvBookmarkImporter(
val tagsString = row.getOrNull(5) ?: ""
val thumbnail = row.getOrNull(6) ?: ""
val isFavourite = row.getOrNull(7)?.toLongOrNull() ?: 0
val existing = deeprQueries.getDeeprByLink(link).executeAsOneOrNull()
if (link.isNotBlank() && existing == null) {
val existing = deeprQueries.isLinkExists(link).executeAsOneOrNull() ?: 0L
if (link.isNotBlank() && existing == 0L) {
updatedCount++
deeprQueries.transaction {
deeprQueries.importDeepr(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ abstract class HtmlBookmarkImporter(
val bookmarks = extractBookmarks(document)

bookmarks.forEach { bookmark ->
val existing = deeprQueries.getDeeprByLink(bookmark.url).executeAsOneOrNull()
if (bookmark.url.isNotBlank() && existing == null) {
val existing = deeprQueries.isLinkExists(bookmark.url).executeAsOneOrNull()
if (bookmark.url.isNotBlank() && existing == 0L) {
try {
deeprQueries.transaction {
deeprQueries.insertDeepr(
Expand All @@ -38,6 +38,7 @@ abstract class HtmlBookmarkImporter(
name = bookmark.title,
notes = bookmark.folder ?: "",
thumbnail = "",
isFavourite = 0
)

// Add tags if present
Expand Down
Loading