Skip to content

Commit

Permalink
feat: allow linked timestamps to local files
Browse files Browse the repository at this point in the history
local files are now supported when using linked timestamps

re #56 #60
  • Loading branch information
chhoumann committed Feb 8, 2023
1 parent 6d16edf commit 011a4f5
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 102 deletions.
63 changes: 38 additions & 25 deletions src/API/API.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,81 @@
import { Episode } from "src/types/Episode";
import { formatSeconds } from "src/utility/formatSeconds";
import { IAPI } from "./IAPI";
import { currentEpisode, currentTime, duration, isPaused, plugin } from "src/store";
import {
currentEpisode,
currentTime,
downloadedEpisodes,
duration,
isPaused,
plugin,
} from "src/store";
import { get } from "svelte/store";
import encodePodnotesURI from "src/utility/encodePodnotesURI";
import { isLocalFile } from "src/utility/isLocalFile";

export class API implements IAPI {
public get podcast(): Episode {
public get podcast(): Episode {
return get(currentEpisode);
}
}

public get length(): number {
public get length(): number {
return get(duration);
}
}

public get currentTime(): number {
public get currentTime(): number {
return get(currentTime);
}
}

public set currentTime(value: number) {
currentTime.update((_) => value);
}
public get isPlaying(): boolean {

public get isPlaying(): boolean {
return !get(isPaused);
}
}

/**
* Gets the current time in the given moment format.
* @param format Moment format.
* @param linkify Linking to the podcast so PodNotes can open it at this time later.
* @returns
*/
getPodcastTimeFormatted(format: string, linkify = false): string {
* Gets the current time in the given moment format.
* @param format Moment format.
* @param linkify Linking to the podcast so PodNotes can open it at this time later.
* @returns
*/
getPodcastTimeFormatted(format: string, linkify = false): string {
if (!this.podcast) {
throw new Error("No podcast loaded");
}

const time = formatSeconds(this.currentTime, format);

if (!linkify) return time;

if (!this.podcast.feedUrl) {
// Considered handling this as an error case, but I think
const epIsLocal = isLocalFile(this.podcast);
const feedUrl = !epIsLocal
? this.podcast.feedUrl
: downloadedEpisodes.getEpisode(this.podcast)?.filePath;

if (!feedUrl || feedUrl === "") {
// Considered handling this as an error case, but I think
// it's better UX to just show the time rather than getting an error.
return time;
}

const url = encodePodnotesURI(
this.podcast.title,
this.podcast.feedUrl,
feedUrl,
this.currentTime
);

return `[${time}](${url.href})`;
}
}

start(): void {
start(): void {
isPaused.update((_) => false);
}
}

stop(): void {
stop(): void {
isPaused.update((_) => true);
}
}

skipBackward(): void {
const skipBackLen = get(plugin).settings.skipBackwardLength;
Expand Down
28 changes: 18 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { TFile } from "obsidian";
import { createMediaUrlObjectFromFilePath } from "./utility/createMediaUrlObjectFromFilePath";
import { LocalFilesController } from "./store_controllers/LocalFilesController";
import PartialAppExtension from "./global";
import { LocalEpisode } from "./types/LocalEpisode";

export default class PodNotes extends Plugin implements IPodNotes {
public api: IAPI;
Expand Down Expand Up @@ -266,11 +267,20 @@ export default class PodNotes extends Plugin implements IPodNotes {
return;
}

const feedparser = new FeedParser();
const episode = await feedparser.findItemByTitle(
decodedName,
url
);
const localFile = app.vault.getAbstractFileByPath(url);

let episode: Episode | undefined;

if (localFile) {
episode = localFiles.getLocalEpisode(decodedName);
} else {
const feedparser = new FeedParser();

episode = await feedparser.findItemByTitle(
decodedName,
url
);
}

if (!episode) {
new Notice("Episode not found");
Expand Down Expand Up @@ -301,7 +311,7 @@ export default class PodNotes extends Plugin implements IPodNotes {
.setIcon("play")
.setTitle("Play with PodNotes")
.onClick(async () => {
const localEpisode: Episode = {
const localEpisode: LocalEpisode = {
title: file.basename,
description: "",
content: "",
Expand All @@ -327,10 +337,8 @@ export default class PodNotes extends Plugin implements IPodNotes {
file.path,
file.stat.size
);
localFiles.update((localFiles) => {
localFiles.episodes.push(localEpisode);
return localFiles;
});

localFiles.addEpisode(localEpisode);
}

// Fixes where the episode won't play if it has been played.
Expand Down
Loading

1 comment on commit 011a4f5

@vercel
Copy link

@vercel vercel bot commented on 011a4f5 Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.