Skip to content

Commit

Permalink
fix: remote media (funkwhale)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeopJr committed Oct 14, 2023
1 parent 457693e commit b76a17b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/API/Status/PreviewCard.vala
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public class Tuba.API.PreviewCard : Entity, Widgetizable {
Host.open_uri (card_url);
} else {
if (bookwyrm_obj == null) {
app.main_window.show_media_viewer_remote_video (res_url, null, card_url);
app.main_window.show_media_viewer (res_url, Tuba.Attachment.MediaType.VIDEO, null, 0, null, false, null, card_url, true);
} else {
app.main_window.show_book (bookwyrm_obj, card_url);
}
Expand Down
21 changes: 5 additions & 16 deletions src/Dialogs/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,16 @@ public class Tuba.Dialogs.MainWindow: Adw.ApplicationWindow, Saveable {
Gtk.Widget? source_widget = null,
bool as_is = false,
string? alt_text = null,
string? user_friendly_url = null
string? user_friendly_url = null,
bool stream = false
) {
if (as_is && preview == null) return;

media_viewer.add_media (url, media_type, preview, pos, as_is, user_friendly_url);
media_viewer.add_media (url, media_type, preview, pos, as_is, alt_text, user_friendly_url, stream);

if (!is_media_viewer_visible) {
if (source_widget != null) {
media_viewer.reveal (source_widget);
media_viewer_source_widget = source_widget;
} else {
media_viewer.visible = true;
}
}
}

public void show_media_viewer_remote_video (string url, Gdk.Paintable? preview, string? user_friendly_url = null) {
media_viewer.set_remote_video (url, preview, user_friendly_url);

if (!is_media_viewer_visible) {
media_viewer.visible = true;
media_viewer.reveal (source_widget);
media_viewer_source_widget = source_widget;
}
}

Expand Down
50 changes: 32 additions & 18 deletions src/Views/MediaViewer.vala
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
}

public void done () {
if (is_done) return;

spinner.spinning = false;
stack.visible_child_name = "child";
if (is_video) {
Expand Down Expand Up @@ -672,6 +674,7 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {

private void reset_media_viewer () {
this.fullscreen = false;
todo_items.clear ();

items.foreach ((item) => {
carousel.remove (item);
Expand All @@ -688,14 +691,15 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
}

private bool revealed = false;
public void reveal (Gtk.Widget widget) {
public void reveal (Gtk.Widget? widget) {
if (revealed) return;

this.visible = true;
scale_revealer.source_widget = widget;
scale_revealer.reveal_child = true;

revealed = true;
do_todo_items ();
}

public void add_media (
Expand All @@ -705,7 +709,8 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
int? pos = null,
bool as_is = false,
string? alt_text = null,
string? user_friendly_url = null
string? user_friendly_url = null,
bool stream = false
) {
Item item;
string final_friendly_url = user_friendly_url == null ? url : user_friendly_url;
Expand All @@ -715,12 +720,15 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
var video = new Gtk.Video ();
item = new Item (video, final_friendly_url, final_preview, true);

if (!as_is) {
if (stream) {
File file = File.new_for_uri (url);
video.set_file (file);
} else if (!as_is) {
download_video.begin (url, (obj, res) => {
try {
var path = download_video.end (res);
video.set_filename (path);
item.done ();
add_todo_item (item);
}
catch (Error e) {
var dlg = app.inform (_("Error"), e.message);
Expand All @@ -744,37 +752,43 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
image_cache.request_paintable (url, (is_loaded, data) => {
if (is_loaded) {
picture.paintable = data;
item.done ();
add_todo_item (item);
}
});
} else {
picture.paintable = preview;
}
}

if (as_is) item.done ();

if (pos == null) {
carousel.append (item);
items.add (item);
} else {
carousel.insert (item, pos);
items.insert (pos, item);
}
}

public void set_remote_video (string url, Gdk.Paintable? preview, string? user_friendly_url = null) {
var video = new Gtk.Video ();
var item = new Item (video, user_friendly_url, preview, true);

File file = File.new_for_uri (url);
video.set_file (file);
item.done ();
if (as_is || stream) add_todo_item (item);
}

carousel.append (item);
items.add (item);
private Gee.ArrayList<string> todo_items = new Gee.ArrayList<string> ();
private void add_todo_item (Item todo_item) {
if (revealed) {
todo_item.done ();
} else {
todo_items.add (todo_item.url);
}
}
private void do_todo_items () {
if (todo_items.size == 0 || items.size == 0) return;

carousel.page_changed (0);
items.foreach (item => {
if (todo_items.contains (item.url)) {
item.done ();
todo_items.remove (item.url);
}
return true;
});
}

public void scroll_to (int pos, bool should_timeout = true) {
Expand Down

0 comments on commit b76a17b

Please sign in to comment.