Skip to content

Commit

Permalink
Merge branch 'master' into camera
Browse files Browse the repository at this point in the history
  • Loading branch information
suhailsaqan authored Mar 25, 2024
2 parents 7a174f0 + 68409f3 commit 7f54204
Show file tree
Hide file tree
Showing 35 changed files with 596 additions and 506 deletions.
56 changes: 30 additions & 26 deletions damus.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

30 changes: 26 additions & 4 deletions damus/Components/ImageCarousel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,31 @@ class CarouselModel: ObservableObject {

// MARK: - Image Carousel
@MainActor
struct ImageCarousel: View {
struct ImageCarousel<Content: View>: View {
var urls: [MediaUrl]

let evid: NoteId

let state: DamusState
@ObservedObject var model: CarouselModel
let content: ((_ dismiss: @escaping (() -> Void)) -> Content)?

init(state: DamusState, evid: NoteId, urls: [MediaUrl]) {
self.urls = urls
self.evid = evid
self.state = state
let media_model = state.events.get_cache_data(evid).media_metadata_model
self._model = ObservedObject(initialValue: CarouselModel(image_fill: media_model.fill))
self.content = nil
}

init(state: DamusState, evid: NoteId, urls: [MediaUrl], @ViewBuilder content: @escaping (_ dismiss: @escaping (() -> Void)) -> Content) {
self.urls = urls
self.evid = evid
self.state = state
let media_model = state.events.get_cache_data(evid).media_metadata_model
self._model = ObservedObject(initialValue: CarouselModel(image_fill: media_model.fill))
self.content = content
}

var filling: Bool {
Expand Down Expand Up @@ -132,7 +143,7 @@ struct ImageCarousel: View {
model.open_sheet = true
}
case .video(let url):
DamusVideoPlayer(url: url, video_size: $model.video_size, controller: state.video)
DamusVideoPlayer(url: url, video_size: $model.video_size, controller: state.video, style: .preview(on_tap: { model.open_sheet = true }))
.onChange(of: model.video_size) { size in
guard let size else { return }

Expand Down Expand Up @@ -201,7 +212,16 @@ struct ImageCarousel: View {
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.fullScreenCover(isPresented: $model.open_sheet) {
ImageView(video_controller: state.video, urls: urls, settings: state.settings, selectedIndex: $model.selectedIndex)
if let content {
FullScreenCarouselView<Content>(video_controller: state.video, urls: urls, settings: state.settings, selectedIndex: $model.selectedIndex) {
content({ // Dismiss closure
model.open_sheet = false
})
}
}
else {
FullScreenCarouselView<AnyView>(video_controller: state.video, urls: urls, settings: state.settings, selectedIndex: $model.selectedIndex)
}
}
.frame(height: height)
.onChange(of: model.selectedIndex) { value in
Expand Down Expand Up @@ -296,7 +316,9 @@ public struct ImageFill {
struct ImageCarousel_Previews: PreviewProvider {
static var previews: some View {
let url: MediaUrl = .image(URL(string: "https://jb55.com/red-me.jpg")!)
ImageCarousel(state: test_damus_state, evid: test_note.id, urls: [url, url])
let test_video_url: MediaUrl = .video(URL(string: "http://cdn.jb55.com/s/zaps-build.mp4")!)
ImageCarousel<AnyView>(state: test_damus_state, evid: test_note.id, urls: [test_video_url, url])
.environmentObject(OrientationTracker())
}
}

7 changes: 6 additions & 1 deletion damus/Components/TruncatedText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import SwiftUI

struct TruncatedText: View {
let text: CompatibleText
let maxChars: Int = 280
let maxChars: Int

init(text: CompatibleText, maxChars: Int = 280) {
self.text = text
self.maxChars = maxChars
}

var body: some View {
let truncatedAttributedString: AttributedString? = text.attributed.truncateOrNil(maxLength: maxChars)
Expand Down
3 changes: 2 additions & 1 deletion damus/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,8 @@ struct ContentView: View {
nav: self.navigationCoordinator,
music: MusicController(onChange: music_changed),
video: VideoController(),
ndb: ndb
ndb: ndb,
quote_reposts: .init(our_pubkey: pubkey)
)

home.damus_state = self.damus_state!
Expand Down
12 changes: 11 additions & 1 deletion damus/Models/ActionBarModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ enum Zapped {
class ActionBarModel: ObservableObject {
@Published var our_like: NostrEvent?
@Published var our_boost: NostrEvent?
@Published var our_quote_repost: NostrEvent?
@Published var our_reply: NostrEvent?
@Published var our_zap: Zapping?
@Published var likes: Int
@Published var boosts: Int
@Published var quote_reposts: Int
@Published private(set) var zaps: Int
@Published var zap_total: Int64
@Published var replies: Int
Expand All @@ -28,7 +30,7 @@ class ActionBarModel: ObservableObject {
return ActionBarModel(likes: 0, boosts: 0, zaps: 0, zap_total: 0, replies: 0, our_like: nil, our_boost: nil, our_zap: nil, our_reply: nil)
}

init(likes: Int = 0, boosts: Int = 0, zaps: Int = 0, zap_total: Int64 = 0, replies: Int = 0, our_like: NostrEvent? = nil, our_boost: NostrEvent? = nil, our_zap: Zapping? = nil, our_reply: NostrEvent? = nil) {
init(likes: Int = 0, boosts: Int = 0, zaps: Int = 0, zap_total: Int64 = 0, replies: Int = 0, our_like: NostrEvent? = nil, our_boost: NostrEvent? = nil, our_zap: Zapping? = nil, our_reply: NostrEvent? = nil, our_quote_repost: NostrEvent? = nil, quote_reposts: Int = 0) {
self.likes = likes
self.boosts = boosts
self.zaps = zaps
Expand All @@ -38,18 +40,22 @@ class ActionBarModel: ObservableObject {
self.our_boost = our_boost
self.our_zap = our_zap
self.our_reply = our_reply
self.our_quote_repost = our_quote_repost
self.quote_reposts = quote_reposts
}

func update(damus: DamusState, evid: NoteId) {
self.likes = damus.likes.counts[evid] ?? 0
self.boosts = damus.boosts.counts[evid] ?? 0
self.zaps = damus.zaps.event_counts[evid] ?? 0
self.replies = damus.replies.get_replies(evid)
self.quote_reposts = damus.quote_reposts.counts[evid] ?? 0
self.zap_total = damus.zaps.event_totals[evid] ?? 0
self.our_like = damus.likes.our_events[evid]
self.our_boost = damus.boosts.our_events[evid]
self.our_zap = damus.zaps.our_zaps[evid]?.first
self.our_reply = damus.replies.our_reply(evid)
self.our_quote_repost = damus.quote_reposts.our_events[evid]
self.objectWillChange.send()
}

Expand All @@ -68,4 +74,8 @@ class ActionBarModel: ObservableObject {
var boosted: Bool {
return our_boost != nil
}

var quoted: Bool {
return our_quote_repost != nil
}
}
4 changes: 4 additions & 0 deletions damus/Models/ContentFilters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ struct ContentFilters {
}

extension ContentFilters {
static func default_filters(damus_state: DamusState) -> ContentFilters {
return ContentFilters(filters: ContentFilters.defaults(damus_state: damus_state))
}

static func defaults(damus_state: DamusState) -> [(NostrEvent) -> Bool] {
var filters = Array<(NostrEvent) -> Bool>()
if damus_state.settings.hide_nsfw_tagged_content {
Expand Down
7 changes: 5 additions & 2 deletions damus/Models/DamusState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class DamusState: HeadlessDamusState {
let keypair: Keypair
let likes: EventCounter
let boosts: EventCounter
let quote_reposts: EventCounter
let contacts: Contacts
let mutelist_manager: MutelistManager
let profiles: Profiles
Expand All @@ -36,7 +37,7 @@ class DamusState: HeadlessDamusState {
let ndb: Ndb
var purple: DamusPurple

init(pool: RelayPool, keypair: Keypair, likes: EventCounter, boosts: EventCounter, contacts: Contacts, mutelist_manager: MutelistManager, profiles: Profiles, dms: DirectMessagesModel, previews: PreviewCache, zaps: Zaps, lnurls: LNUrls, settings: UserSettingsStore, relay_filters: RelayFilters, relay_model_cache: RelayModelCache, drafts: Drafts, events: EventCache, bookmarks: BookmarksManager, postbox: PostBox, bootstrap_relays: [String], replies: ReplyCounter, wallet: WalletModel, nav: NavigationCoordinator, music: MusicController?, video: VideoController, ndb: Ndb, purple: DamusPurple? = nil) {
init(pool: RelayPool, keypair: Keypair, likes: EventCounter, boosts: EventCounter, contacts: Contacts, mutelist_manager: MutelistManager, profiles: Profiles, dms: DirectMessagesModel, previews: PreviewCache, zaps: Zaps, lnurls: LNUrls, settings: UserSettingsStore, relay_filters: RelayFilters, relay_model_cache: RelayModelCache, drafts: Drafts, events: EventCache, bookmarks: BookmarksManager, postbox: PostBox, bootstrap_relays: [String], replies: ReplyCounter, wallet: WalletModel, nav: NavigationCoordinator, music: MusicController?, video: VideoController, ndb: Ndb, purple: DamusPurple? = nil, quote_reposts: EventCounter) {
self.pool = pool
self.keypair = keypair
self.likes = likes
Expand Down Expand Up @@ -66,6 +67,7 @@ class DamusState: HeadlessDamusState {
settings: settings,
keypair: keypair
)
self.quote_reposts = quote_reposts
}

@discardableResult
Expand Down Expand Up @@ -129,7 +131,8 @@ class DamusState: HeadlessDamusState {
nav: NavigationCoordinator(),
music: nil,
video: VideoController(),
ndb: .empty
ndb: .empty,
quote_reposts: .init(our_pubkey: empty_pub)
)
}
}
68 changes: 51 additions & 17 deletions damus/Models/EventsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,62 @@

import Foundation


class EventsModel: ObservableObject {
let state: DamusState
let target: NoteId
let kind: NostrKind
let kind: QueryKind
let sub_id = UUID().uuidString
let profiles_id = UUID().uuidString

@Published var events: [NostrEvent] = []

var events: EventHolder
@Published var loading: Bool

enum QueryKind {
case kind(NostrKind)
case quotes
}

init(state: DamusState, target: NoteId, kind: NostrKind) {
self.state = state
self.target = target
self.kind = kind
self.kind = .kind(kind)
self.loading = true
self.events = EventHolder(on_queue: { ev in
preload_events(state: state, events: [ev])
})
}

init(state: DamusState, target: NoteId, query: EventsModel.QueryKind) {
self.state = state
self.target = target
self.kind = query
self.loading = true
self.events = EventHolder(on_queue: { ev in
preload_events(state: state, events: [ev])
})
}

public static func quotes(state: DamusState, target: NoteId) -> EventsModel {
EventsModel(state: state, target: target, query: .quotes)
}

public static func reposts(state: DamusState, target: NoteId) -> EventsModel {
EventsModel(state: state, target: target, kind: .boost)
}

public static func likes(state: DamusState, target: NoteId) -> EventsModel {
EventsModel(state: state, target: target, kind: .like)
}

private func get_filter() -> NostrFilter {
var filter = NostrFilter(kinds: [kind])
filter.referenced_ids = [target]
var filter: NostrFilter
switch kind {
case .kind(let k):
filter = NostrFilter(kinds: [k])
filter.referenced_ids = [target]
case .quotes:
filter = NostrFilter(kinds: [.text])
filter.quotes = [target]
}
filter.limit = 500
return filter
}
Expand All @@ -41,21 +78,17 @@ class EventsModel: ObservableObject {
}

private func handle_event(relay_id: String, ev: NostrEvent) {
guard ev.kind == kind.rawValue,
ev.referenced_ids.last == target else {
return
}

if insert_uniq_sorted_event(events: &self.events, new_ev: ev, cmp: { a, b in a.created_at < b.created_at } ) {
if events.insert(ev) {
objectWillChange.send()
}
}

func handle_nostr_event(relay_id: String, ev: NostrConnectionEvent) {
guard case .nostr_event(let nev) = ev else {
guard case .nostr_event(let nev) = ev, nev.subid == self.sub_id
else {
return
}

switch nev {
case .event(_, let ev):
handle_event(relay_id: relay_id, ev: ev)
Expand All @@ -66,10 +99,11 @@ class EventsModel: ObservableObject {
case .auth:
break
case .eose:
self.loading = false
guard let txn = NdbTxn(ndb: self.state.ndb) else {
return
}
load_profiles(context: "events_model", profiles_subid: profiles_id, relay_id: relay_id, load: .from_events(events), damus_state: state, txn: txn)
load_profiles(context: "events_model", profiles_subid: profiles_id, relay_id: relay_id, load: .from_events(events.all_events), damus_state: state, txn: txn)
}
}
}
15 changes: 13 additions & 2 deletions damus/Models/HomeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,19 @@ class HomeModel {
case .already_counted:
break
case .success(let n):
let boosted = Counted(event: ev, id: e, total: n)
notify(.reposted(boosted))
notify(.update_stats(note_id: e))
}
}

func handle_quote_repost_event(_ ev: NostrEvent, target: NoteId) {
switch damus_state.quote_reposts.add_event(ev, target: target) {
case .already_counted:
break
case .success(let n):
notify(.update_stats(note_id: target))
}
}

func handle_like_event(_ ev: NostrEvent) {
guard let e = ev.last_refid() else {
// no id ref? invalid like event
Expand Down Expand Up @@ -672,6 +679,10 @@ class HomeModel {
damus_state.replies.count_replies(ev, keypair: self.damus_state.keypair)
damus_state.events.insert(ev)

if let quoted_event = ev.referenced_quote_ids.first {
handle_quote_repost_event(ev, target: quoted_event.note_id)
}

if sub_id == home_subid {
insert_home_event(ev)
} else if sub_id == notifications_subid {
Expand Down
16 changes: 0 additions & 16 deletions damus/Models/ReactionsModel.swift

This file was deleted.

15 changes: 0 additions & 15 deletions damus/Models/RepostsModel.swift

This file was deleted.

Loading

0 comments on commit 7f54204

Please sign in to comment.