crossmate

A collaborative crossword app for iOS
Log | Files | Refs | LICENSE

InviteEntity+DisplayName.swift (1318B)


      1 import CoreData
      2 import Foundation
      3 
      4 extension InviteEntity {
      5     /// The name invite rows and alerts should show for the inviter, or `nil`
      6     /// when nothing presentable is known (callers supply their own
      7     /// placeholder — "A player" in rows, "this player" in the block alert).
      8     ///
      9     /// A nickname the user assigned via Rename wins outright — the same rule
     10     /// as `FriendEntity.resolvedDisplayName`, reached here through
     11     /// `inviterAuthorID` because the row renders from the `InviteEntity`,
     12     /// which only carries the name snapshot the `.invite` Ping shipped.
     13     var resolvedInviterName: String? {
     14         if let authorID = inviterAuthorID, !authorID.isEmpty,
     15            let ctx = managedObjectContext {
     16             let req = NSFetchRequest<FriendEntity>(entityName: "FriendEntity")
     17             req.predicate = NSPredicate(format: "authorID == %@", authorID)
     18             req.fetchLimit = 1
     19             if let nickname = (try? ctx.fetch(req).first)?.nickname?
     20                 .trimmingCharacters(in: .whitespacesAndNewlines),
     21                !nickname.isEmpty {
     22                 return nickname
     23             }
     24         }
     25         if let name = inviterName?.trimmingCharacters(in: .whitespacesAndNewlines),
     26            !name.isEmpty {
     27             return name
     28         }
     29         return nil
     30     }
     31 }