Repository navigation
Expand file tree
/
Copy pathrssreader_menu.lua
More file actions
3386 lines (3111 loc) · 121 KB
/
Copy pathrssreader_menu.lua
File metadata and controls
3386 lines (3111 loc) · 121 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local util = require("util")
local Menu = require("ui/widget/menu")
local Button = require("ui/widget/button")
local ButtonDialog = require("ui/widget/buttondialog")
local Blitbuffer = require("ffi/blitbuffer")
local ffiUtil = require("ffi/util")
local Device = require("device")
local UIManager = require("ui/uimanager")
local InfoMessage = require("ui/widget/infomessage")
local _ = require("gettext")
local logger = require("logger")
local lfs = require("libs/libkoreader-lfs")
local NetworkMgr = require("ui/network/manager")
local Screen = Device.screen
local Commons = require("rssreader_commons")
local LocalStore = require("rssreader_local_store")
local StoryViewer = require("rssreader_story_viewer")
local FeedFetcher = require("rssreader_feed_fetcher")
local HtmlResources = require("rssreader_html_resources")
local InputDialog = require("ui/widget/inputdialog")
local OPMLHandler = require("rssreader_opml")
local QRMessage = require("ui/widget/qrmessage")
local utils = require("rssreader_menu_utils")
local backends = require("rssreader_menu_backends")
local Pool = require("rssreader_pool")
local LocalReadState
local MenuBuilder = {}
MenuBuilder.__index = MenuBuilder
-- Open a story as a document in the reader, marking it read first.
function MenuBuilder:openStoryDocument(stories, index, context)
local story = stories[index]
utils.normalizeStoryReadState(story)
if utils.isUnread(story) then
self:handleStoryAction(stories, index, "mark_read", story, context)
end
self:handleStoryAction(stories, index, "go_to_link", { story = story }, context)
end
function MenuBuilder:createTapCallback(stories, index, context)
local reader = self.reader
local tap_action = "preview"
if reader and type(reader.getTapAction) == "function" then
tap_action = reader:getTapAction()
end
local story = stories[index]
if tap_action == "open" then
return function()
self:openStoryDocument(stories, index, context)
end
elseif tap_action == "save" then
return function()
utils.normalizeStoryReadState(story)
if utils.isUnread(story) then
self:handleStoryAction(stories, index, "mark_read", story, context)
end
self:handleStoryAction(stories, index, "save_story", { story = story }, context)
end
elseif tap_action == "addtolist" then
return function()
local ok, err = Pool.addStory(story)
if ok then
UIManager:show(InfoMessage:new{ text = _("Added to List."), timeout = 1 })
utils.normalizeStoryReadState(story)
if utils.isUnread(story) then
self:handleStoryAction(stories, index, "mark_read", story, context)
end
if context and type(context.refresh) == "function" then
UIManager:scheduleIn(0.1, function()
context.refresh()
end)
end
elseif err == "duplicate" then
UIManager:show(InfoMessage:new{ text = _("Already in List."), timeout = 1 })
elseif err == "pool_full" then
UIManager:show(InfoMessage:new{ text = _("List is full."), timeout = 1 })
else
UIManager:show(InfoMessage:new{ text = _("Could not add to List."), timeout = 1 })
end
end
else
return function()
self:showStory(stories, index, function(action, payload)
self:handleStoryAction(stories, index, action, payload, context)
end, nil, nil, context)
end
end
end
function MenuBuilder:showStory(stories, index, on_action, on_close, options, context)
self.story_viewer = self.story_viewer or StoryViewer:new()
local reader = self.reader
if reader and type(reader.requestFeedStatePreservation) == "function" then
reader:requestFeedStatePreservation()
end
local current_menu = self.reader and self.reader.current_menu_info and self.reader.current_menu_info.menu
local current_info = self.reader and self.reader.current_menu_info
local history_snapshot
if self.reader and self.reader.history then
history_snapshot = {}
for i, entry in ipairs(self.reader.history) do
history_snapshot[i] = entry
end
end
local story = stories and stories[index]
if story then
utils.normalizeStoryReadState(story)
if utils.isUnread(story) then
self:handleStoryAction(stories, index, "mark_read", story, context)
end
end
local is_api_context = false
if context and (context.feed_type == "newsblur" or context.feed_type == "commafeed" or context.feed_type == "freshrss" or context.feed_type == "fever" or context.feed_type == "miniflux" or context.feed_type == "feedbin") then
is_api_context = true
end
local disable_mutators = false
if options and options.disable_story_mutators and not is_api_context then
disable_mutators = true
end
local allow_mark_unread = true
if context then
if context.feed_type == "local" then
allow_mark_unread = true
else
local client = context.client
if client and type(client.markStoryAsUnread) == "function" then
allow_mark_unread = true
else
allow_mark_unread = false
end
end
end
local allow_star = false
if context and context.client and type(context.client.markStoryAsStarred) == "function"
and type(context.client.markStoryAsUnstarred) == "function" then
allow_star = true
end
local show_images_in_preview = false
if self.accounts and self.accounts.config then
local flag = util.tableGetValue(self.accounts.config, "features", "show_images_in_preview")
show_images_in_preview = flag == true
end
self.story_viewer:showStory(story, function(action, payload)
self:handleStoryAction(stories, index, action, payload, context)
end, function()
if self.reader and current_menu then
if current_info and self.reader.current_menu_info ~= current_info then
self.reader.current_menu_info = current_info
end
if history_snapshot and #history_snapshot > 0 and (not self.reader.history or #self.reader.history == 0) and not current_menu._rss_is_root_menu then
self.reader.history = history_snapshot
end
self.reader:updateBackButton(current_menu)
else
end
if context and type(context.refresh) == "function" then
local should_refresh = context._needs_refresh or context.force_refresh_on_close
if should_refresh then
context._needs_refresh = nil
context.force_refresh_on_close = nil
context.refresh()
end
end
if on_close then
on_close()
end
end, {
disable_story_mutators = disable_mutators,
is_api_version = is_api_context,
allow_mark_unread = allow_mark_unread,
allow_star = allow_star,
show_images_in_preview = show_images_in_preview,
})
end
function MenuBuilder:_updateStoryEntry(context, stories, index)
if not context or not stories or not index then
return
end
local menu_instance = context.menu_instance
local story = stories[index]
if not menu_instance or not story or type(menu_instance) ~= "table" then
return
end
if type(menu_instance.item_table) ~= "table" then
return
end
local entry = menu_instance.item_table[index]
if not entry then
return
end
local view_mode = "compact"
if self.reader and type(self.reader.getListViewMode) == "function" then
view_mode = self.reader:getListViewMode()
end
entry.text = utils.buildStoryEntryText(story, true, view_mode)
entry.bold = utils.isUnread(story)
if type(menu_instance.updateItems) == "function" then
menu_instance:updateItems(nil, true)
end
end
function MenuBuilder:_updateFeedCache(context)
if not context then
return
end
local feed_node = context.feed_node
if not feed_node then
return
end
if context.menu_instance then
utils.persistFeedState(context.menu_instance, feed_node)
return
end
local reader = feed_node._rss_reader or self.reader
if not reader or type(reader.updateFeedState) ~= "function" then
return
end
local account_name = feed_node._account_name
or (context.account and context.account.name)
or context.account_name
or "unknown"
local stories_copy = feed_node._rss_stories and util.tableDeepCopy(feed_node._rss_stories) or {}
local story_keys_copy = {}
for key, value in pairs(feed_node._rss_story_keys or {}) do
if value then
story_keys_copy[key] = true
end
end
reader:updateFeedState(account_name, feed_node.id, {
stories = stories_copy,
story_keys = story_keys_copy,
menu_page = context.menu_instance and context.menu_instance.page or feed_node._rss_menu_page,
current_page = feed_node._rss_page,
has_more = feed_node._rss_has_more,
continuation = feed_node._rss_continuation,
})
end
function MenuBuilder:handleStoryAction(stories, index, action, payload, context)
local story = stories and stories[index]
if action == "go_to_link" then
local payload_table = type(payload) == "table" and payload or {}
local target_story = payload_table.story or story
utils.normalizeStoryLink(target_story)
local function closeCurrentStory()
if type(payload_table.close_story) == "function" then
payload_table.close_story()
end
end
local function closeActiveMenu()
local reader = self.reader
if reader and reader.current_menu_info and reader.current_menu_info.menu then
UIManager:close(reader.current_menu_info.menu)
reader.current_menu_info = nil
end
end
closeCurrentStory()
closeActiveMenu()
utils.downloadStoryToCache(target_story, self, function(path, err)
if err then
local link = target_story and (target_story.permalink or target_story.href or target_story.link)
if link then
UIManager:show(InfoMessage:new{ text = string.format(_("Opening: %s"), link) })
end
end
end, { rss_return = true, story_key = utils.storyUniqueKey(target_story) })
return
end
if action == "next_story" then
local next_index = utils.findNextIndex(stories, index, function()
return true
end)
if next_index then
self:showStory(stories, next_index, function(next_action, next_payload)
self:handleStoryAction(stories, next_index, next_action, next_payload, context)
end, nil, nil, context)
end
return
end
if action == "next_unread" then
local next_index = utils.findNextIndex(stories, index, function(story)
return utils.isUnread(story)
end)
if next_index then
self:showStory(stories, next_index, function(next_action, next_payload)
self:handleStoryAction(stories, next_index, next_action, next_payload, context)
end, nil, nil, context)
else
UIManager:show(InfoMessage:new{ text = _("No unread stories found.") })
end
return
end
if action == "prev_story" or action == "prev_unread" then
local only_unread = action == "prev_unread"
local prev_index = utils.findPrevIndex(stories, index, function(candidate)
return not only_unread or utils.isUnread(candidate)
end)
if prev_index then
self:showStory(stories, prev_index, function(prev_action, prev_payload)
self:handleStoryAction(stories, prev_index, prev_action, prev_payload, context)
end, nil, nil, context)
elseif only_unread then
UIManager:show(InfoMessage:new{ text = _("No unread stories found.") })
end
return
end
if action == "open_link" then
local link = payload
if link and util.openFileWithCRE then
util.openFileWithCRE(link)
elseif link then
UIManager:show(InfoMessage:new{ text = string.format(_("Opening: %s"), link) })
end
return
end
if action == "mark_read" then
if story then
utils.setStoryReadState(story, true)
self:_updateStoryEntry(context, stories, index)
self:_updateFeedCache(context)
if context and context.feed_type == "local" then
local feed_identifier = context.feed_identifier or (context.feed_node and (context.feed_node.url or context.feed_node.id))
if feed_identifier then
local story_local_key = story._rss_local_key or utils.storyUniqueKey(story)
if story_local_key then
story._rss_local_key = story_local_key
context.local_read_map = context.local_read_map or {}
context.local_read_map = self.local_read_state.markRead(feed_identifier, story_local_key, context.local_read_map)
if context.feed_node then
context.feed_node._rss_local_read_map = context.local_read_map
end
end
end
end
local remote_feed_id = utils.resolveStoryFeedId(context, story)
if context and context.client and remote_feed_id and type(context.client.markStoryAsRead) == "function" then
NetworkMgr:runWhenOnline(function()
local ok, err_or_data = context.client:markStoryAsRead(remote_feed_id, story)
if not ok then
utils.setStoryReadState(story, false)
self:_updateStoryEntry(context, stories, index)
self:_updateFeedCache(context)
UIManager:show(InfoMessage:new{ text = err_or_data or _("Failed to update story state."), timeout = 3 })
end
end)
end
end
return
end
if action == "mark_unread" then
if story then
local remote_feed_id = utils.resolveStoryFeedId(context, story)
if context and context.feed_type ~= "local" and context.client and remote_feed_id and type(context.client.markStoryAsUnread) == "function" then
NetworkMgr:runWhenOnline(function()
local ok, err_or_data = context.client:markStoryAsUnread(remote_feed_id, story)
if ok then
utils.setStoryReadState(story, false)
self:_updateStoryEntry(context, stories, index)
self:_updateFeedCache(context)
else
UIManager:show(InfoMessage:new{ text = err_or_data or _("Failed to update story state."), timeout = 3 })
end
end)
return
end
utils.setStoryReadState(story, false)
if context and context.feed_type == "local" then
local feed_identifier = context.feed_identifier or (context.feed_node and (context.feed_node.url or context.feed_node.id))
if feed_identifier then
local story_local_key = story._rss_local_key or utils.storyUniqueKey(story)
if story_local_key then
story._rss_local_key = story_local_key
context.local_read_map = context.local_read_map or {}
context.local_read_map = self.local_read_state.markUnread(feed_identifier, story_local_key, context.local_read_map)
if context.feed_node then
context.feed_node._rss_local_read_map = context.local_read_map
end
end
end
self:_updateStoryEntry(context, stories, index)
self:_updateFeedCache(context)
return
end
self:_updateStoryEntry(context, stories, index)
self:_updateFeedCache(context)
end
return
end
if action == "mark_star" or action == "mark_unstar" then
if story then
local should_star = action == "mark_star"
local remote_feed_id = utils.resolveStoryFeedId(context, story)
if context and context.client and remote_feed_id
and type(context.client.markStoryAsStarred) == "function"
and type(context.client.markStoryAsUnstarred) == "function" then
NetworkMgr:runWhenOnline(function()
local ok, err_or_data
if should_star then
ok, err_or_data = context.client:markStoryAsStarred(remote_feed_id, story)
else
ok, err_or_data = context.client:markStoryAsUnstarred(remote_feed_id, story)
end
if ok then
utils.setStoryStarredState(story, should_star)
self:_updateStoryEntry(context, stories, index)
else
UIManager:show(InfoMessage:new{ text = err_or_data or _("Failed to update star state."), timeout = 3 })
end
end)
end
end
return
end
if action == "save_story" then
local payload = type(payload) == "table" and payload or {}
local target_story = payload.story or story
if not target_story then
UIManager:show(InfoMessage:new{ text = _("Could not save story."), timeout = 3 })
return
end
utils.normalizeStoryLink(target_story)
UIManager:show(InfoMessage:new{ text = _("Saving story..."), timeout = 1 })
utils.fetchStoryContent(target_story, self, function(content, err, download_info)
if not content then
UIManager:show(InfoMessage:new{ text = _("Failed to download story."), timeout = 3 })
return
end
local directory = utils.determineSaveDirectory(self)
if not directory or directory == "" then
UIManager:show(InfoMessage:new{ text = _("No target folder available."), timeout = 3 })
return
end
util.makePath(directory)
local filename = utils.safeFilenameFromStory(target_story)
local metadata = type(download_info) == "table" and download_info or {}
local include_images = metadata.images_requested and true or false
local html_for_epub = metadata.html_for_epub
local should_create_epub = include_images and type(html_for_epub) == "string" and html_for_epub ~= ""
local assets_root = metadata.assets_root or (metadata.assets and metadata.assets.assets_root)
local function cleanupAssets()
if assets_root then
HtmlResources.cleanupAssets(assets_root)
assets_root = nil
end
end
if should_create_epub and utils.EpubDownloadBackend then
local base_name = filename:gsub("%.html$", "")
local epub_path = utils.buildUniqueTargetPathWithExtension(directory, base_name, "epub")
local story_url = metadata.original_url or target_story.permalink or target_story.href or target_story.link or ""
local feed_title = target_story.feed_title or target_story.feedTitle
local local_assets = metadata.local_assets
local ok, result_or_err = pcall(function()
return utils.EpubDownloadBackend:createEpub(epub_path, html_for_epub, story_url, include_images, nil, nil, nil, feed_title, local_assets, utils.EpubDownloadBackend:storyMetadata(target_story))
end)
local success = ok and result_or_err ~= false
if success then
cleanupAssets()
UIManager:show(InfoMessage:new{ text = string.format(_("Saved to: %s"), epub_path), timeout = 3 })
return
else
logger.warn("RSSReader", "Failed to create EPUB", result_or_err)
cleanupAssets()
-- Fall back to HTML save below
end
end
local target_path = utils.buildUniqueTargetPath(directory, filename)
local story_url_for_html = metadata.original_url or target_story.permalink or target_story.href or target_story.link or ""
if not utils.writeStoryHtmlFile(content, target_path, utils.resolveStoryDocumentTitle(target_story), story_url_for_html) then
cleanupAssets()
UIManager:show(InfoMessage:new{ text = _("Failed to save story."), timeout = 3 })
return
end
cleanupAssets()
UIManager:show(InfoMessage:new{ text = string.format(_("Saved to: %s"), target_path), timeout = 3 })
end, { silent = true })
return
end
if self.reader and type(self.reader.handleStoryAction) == "function" then
self.reader:handleStoryAction(stories, index, action, payload, context)
end
end
function MenuBuilder:collectFeedIdsForNode(node)
local feed_ids = {}
if not node then
return feed_ids
end
local function collectFromNode(current_node)
if current_node.kind == "feed" then
-- Skip virtual feeds (CommaFeed, Fever, etc.)
if current_node.id and not current_node._virtual and not current_node.is_virtual then
table.insert(feed_ids, current_node.id)
end
elseif current_node.kind == "folder" or current_node.kind == "root" then
if current_node.children then
for _, child in ipairs(current_node.children) do
collectFromNode(child)
end
end
end
end
collectFromNode(node)
return feed_ids
end
-- The "Open on startup" row of the long-press dialogs; nil (no row) when the
-- item has nothing to be found again by.
function MenuBuilder:startTargetRow(get_dialog, target)
if not target then
return nil
end
return {{
text = _("Open on startup"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(get_dialog())
utils.setStartTarget(target)
end,
}}
end
function MenuBuilder:createLongPressMenuForNode(account, client, node, normal_callback)
if not node or node.kind ~= "feed" then
return
end
local account_type = account and account.type
if account_type ~= "newsblur" and account_type ~= "commafeed" and account_type ~= "fever" and account_type ~= "freshrss" and account_type ~= "miniflux"
and account_type ~= "feedbin" then
return
end
local dialog
dialog = ButtonDialog:new{
title = node.title or _("Feed"),
buttons = {{
{
text = _("Open"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
if type(normal_callback) == "function" then
normal_callback()
end
end,
},
{
text = _("Mark all as read"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
self:performMarkAllAsRead(account, client, node)
end,
},
{
text = _("Close"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
end,
},
}, self:startTargetRow(function() return dialog end, utils.treeStartTarget(account, "feed", node))},
}
UIManager:show(dialog)
return dialog
end
function MenuBuilder:createLongPressMenuForFolder(account, client, node, normal_callback)
if not node or node.kind ~= "folder" then
return
end
local account_type = account and account.type
if account_type ~= "newsblur" and account_type ~= "commafeed" and account_type ~= "fever" and account_type ~= "freshrss" and account_type ~= "miniflux"
and account_type ~= "feedbin" then
return
end
local dialog
dialog = ButtonDialog:new{
title = node.title or _("Category"),
buttons = {{
{
text = _("Open"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
if type(normal_callback) == "function" then
normal_callback()
end
end,
},
{
text = _("Mark all as read"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
self:showMarkAllAsReadDialog(account, client, node)
end,
},
{
text = _("Close"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
end,
},
}, self:startTargetRow(function() return dialog end, utils.treeStartTarget(account, "folder", node))},
}
UIManager:show(dialog)
return dialog
end
function MenuBuilder:createLongPressMenuForLocalGroup(group, account_name, normal_callback)
if not group then
return
end
local dialog
dialog = ButtonDialog:new{
title = group.title or _("Group"),
buttons = {{
{
text = _("Open"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
if type(normal_callback) == "function" then
normal_callback()
end
end,
},
{
text = _("Mark all as read"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
self:showMarkAllAsReadDialogForLocalGroup(group, account_name)
end,
},
{
text = _("Close"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
end,
},
}, self:startTargetRow(function() return dialog end, account_name and group.title and {
account = account_name, kind = "local_group", id = group.title, title = group.title,
})},
}
UIManager:show(dialog)
return dialog
end
function MenuBuilder:createStoryLongPressMenu(stories, index, context, open_callback)
local story = stories and stories[index]
if not story then
return
end
utils.normalizeStoryReadState(story)
local dialog
local is_unread = utils.isUnread(story)
local function closeDialog()
if dialog then
UIManager:close(dialog)
end
end
local function markStoryReadIfNeeded()
if is_unread then
self:handleStoryAction(stories, index, "mark_read", story, context)
is_unread = false
end
end
local story_link = story.permalink or story.href or story.link
local buttons = {{
{
text = _("Preview"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
closeDialog()
markStoryReadIfNeeded()
if type(open_callback) == "function" then
open_callback()
end
end,
},
{
text = _("Open"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
closeDialog()
markStoryReadIfNeeded()
self:handleStoryAction(stories, index, "go_to_link", { story = story }, context)
end,
},
{
text = _("Save"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
closeDialog()
markStoryReadIfNeeded()
self:handleStoryAction(stories, index, "save_story", { story = story }, context)
end,
},
}}
local add_to_list_row = {
{
text = _("Add to List"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
closeDialog()
local ok, err = Pool.addStory(story)
if ok then
UIManager:show(InfoMessage:new{ text = _("Added to List."), timeout = 1 })
markStoryReadIfNeeded()
-- Refresh list after a short delay to avoid timing conflict
if context and type(context.refresh) == "function" then
UIManager:scheduleIn(0.1, function()
context.refresh()
end)
end
elseif err == "duplicate" then
UIManager:show(InfoMessage:new{ text = _("Already in List."), timeout = 1 })
elseif err == "pool_full" then
UIManager:show(InfoMessage:new{ text = _("List is full."), timeout = 1 })
else
UIManager:show(InfoMessage:new{ text = _("Could not add to List."), timeout = 1 })
end
end,
},
}
local has_star_capability = context and context.client
and type(context.client.markStoryAsStarred) == "function"
and type(context.client.markStoryAsUnstarred) == "function"
if has_star_capability then
local is_starred = utils.isStarred(story)
local star_text = is_starred and _("Unstar") or _("Star")
local star_action = is_starred and "mark_unstar" or "mark_star"
table.insert(add_to_list_row, {
text = star_text,
background = Blitbuffer.COLOR_WHITE,
callback = function()
closeDialog()
self:handleStoryAction(stories, index, star_action, story, context)
end,
})
end
local has_tag_capability = context and context.client
and type(context.client.setEntryTags) == "function"
if has_tag_capability then
table.insert(add_to_list_row, {
text = _("Edit Tags"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
closeDialog()
self:showTagEditDialog(stories, index, context)
end,
})
end
table.insert(buttons, add_to_list_row)
local mark_text
local mark_action
if is_unread then
mark_text = _("Mark as read")
mark_action = "mark_read"
else
mark_text = _("Mark as unread")
mark_action = "mark_unread"
end
table.insert(buttons, {
{
text = mark_text,
background = Blitbuffer.COLOR_WHITE,
callback = function()
closeDialog()
self:handleStoryAction(stories, index, mark_action, story, context)
end,
},
{
text = _("Show QR Code"),
background = Blitbuffer.COLOR_WHITE,
enabled = story_link ~= nil,
callback = function()
closeDialog()
local qr_size = math.min(Screen:getWidth(), Screen:getHeight()) * 0.6
UIManager:show(QRMessage:new{
text = story_link,
width = qr_size,
height = qr_size,
})
end,
},
{
text = _("Close"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
closeDialog()
end,
},
})
local menu_title = story.story_title or story.title or _("Story")
local date_str = utils.formatStoryDate(story, true)
if (story._from_virtual_feed or story._is_from_virtual_feed) and story.feed_title and story.feed_title ~= "" then
if date_str then
menu_title = string.format("%s\n[%s] • %s", menu_title, story.feed_title, date_str)
else
menu_title = string.format("%s\n[%s]", menu_title, story.feed_title)
end
elseif date_str then
menu_title = string.format("%s\n%s", menu_title, date_str)
end
if type(story.tags) == "table" and #story.tags > 0 then
menu_title = string.format("%s\n%s: %s", menu_title, _("Tags"), table.concat(story.tags, ", "))
end
local snippet = utils.storySnippet(story, 500)
if snippet then
menu_title = menu_title .. "\n" .. string.rep("─", 20) .. "\n" .. snippet
end
dialog = ButtonDialog:new{
title = menu_title,
buttons = buttons,
}
UIManager:show(dialog)
return dialog
end
function MenuBuilder:showTagEditDialog(stories, index, context)
local story = stories and stories[index]
if not story or not context or not context.client or type(context.client.setEntryTags) ~= "function" then
return
end
local story_id = story.story_id or story.id
if not story_id then
UIManager:show(InfoMessage:new{ text = _("Missing story identifier."), timeout = 3 })
return
end
local current_text = table.concat(story.tags or {}, ", ")
local input_dialog
input_dialog = InputDialog:new{
title = _("Edit tags (comma separated)"),
input = current_text,
buttons = {{
{
text = _("Cancel"),
id = "close",
callback = function()
UIManager:close(input_dialog)
end,
},
{
text = _("Save"),
is_enter_default = true,
callback = function()
local text = input_dialog:getInputText() or ""
UIManager:close(input_dialog)
local tags = {}
local seen = {}
for part in text:gmatch("([^,]+)") do
local trimmed = part:gsub("^%s+", ""):gsub("%s+$", "")
if trimmed ~= "" and not seen[trimmed] then
seen[trimmed] = true
table.insert(tags, trimmed)
end
end
NetworkMgr:runWhenOnline(function()
local ok, err = context.client:setEntryTags(story_id, tags)
if ok then
story.tags = tags
UIManager:show(InfoMessage:new{ text = _("Tags updated."), timeout = 2 })
else
UIManager:show(InfoMessage:new{ text = err or _("Failed to update tags."), timeout = 3 })
end
end)
end,
},
}},
}
UIManager:show(input_dialog)
input_dialog:onShowKeyboard()
end
function MenuBuilder:createLongPressMenuForLocalFeed(feed, account_name, normal_callback)
if not feed then
return
end
local dialog
dialog = ButtonDialog:new{
title = feed.title or feed.url or _("Feed"),
buttons = {{
{
text = _("Open"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
if type(normal_callback) == "function" then
normal_callback()
end
end,
},
{
text = _("Mark all as read"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
self:performLocalMarkAllAsRead(feed, account_name)
end,
},
{
text = _("Close"),
background = Blitbuffer.COLOR_WHITE,
callback = function()
UIManager:close(dialog)
end,
},
}, self:startTargetRow(function() return dialog end, account_name and feed.url and {
account = account_name, kind = "local_feed", id = feed.url, title = feed.title or feed.url,
})},
}
UIManager:show(dialog)
return dialog
end
function MenuBuilder:performLocalMarkAllAsRead(feed, account_name, on_done)
if not feed or not feed.url then
UIManager:show(InfoMessage:new{
text = _("Feed URL is missing."),
timeout = 3,
})
return
end
local title = feed.title or feed.url or _("Feed")
UIManager:show(InfoMessage:new{
text = string.format(_("Marking feed '%s' as read..."), title),
timeout = 1,
})
local feed_identifier = feed.url or feed.id or feed.title or "local_feed"
account_name = account_name or feed._rss_account_name or "local"