diff --git a/Yep.xcodeproj/project.pbxproj b/Yep.xcodeproj/project.pbxproj index 0e977e62..9969e86c 100644 --- a/Yep.xcodeproj/project.pbxproj +++ b/Yep.xcodeproj/project.pbxproj @@ -264,7 +264,7 @@ 50CDBE491ACBAD3200459CE0 /* ChatLeftImageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50CDBE471ACBAD3200459CE0 /* ChatLeftImageCell.swift */; }; 50D45FC31C44D2300044C95A /* OpenGraphService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50D45FC21C44D2300044C95A /* OpenGraphService.swift */; }; 50D4D0DB1AFA825B005AEB6F /* MediaControlView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50D4D0DA1AFA825B005AEB6F /* MediaControlView.swift */; }; - 50DBDEF11C48C2240044E21B /* FeedURLInfoType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50DBDEF01C48C2240044E21B /* FeedURLInfoType.swift */; }; + 50DBDEF11C48C2240044E21B /* OpenGraphInfoType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50DBDEF01C48C2240044E21B /* OpenGraphInfoType.swift */; }; 50DBDEFD1C4CCB030044E21B /* ChatLeftTextURLCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50DBDEFB1C4CCB030044E21B /* ChatLeftTextURLCell.swift */; }; 50DBDF011C4CE75E0044E21B /* ChatRightTextURLCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50DBDEFF1C4CE75E0044E21B /* ChatRightTextURLCell.swift */; }; 50DBDF051C4DD9530044E21B /* CGSize+Yep.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50DBDF041C4DD9530044E21B /* CGSize+Yep.swift */; }; @@ -637,7 +637,7 @@ 50CDBE471ACBAD3200459CE0 /* ChatLeftImageCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ChatLeftImageCell.swift; path = Views/Cells/ChatLeftImage/ChatLeftImageCell.swift; sourceTree = ""; }; 50D45FC21C44D2300044C95A /* OpenGraphService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = OpenGraphService.swift; path = Services/OpenGraphService.swift; sourceTree = ""; }; 50D4D0DA1AFA825B005AEB6F /* MediaControlView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MediaControlView.swift; path = Views/Media/MediaControlView.swift; sourceTree = ""; }; - 50DBDEF01C48C2240044E21B /* FeedURLInfoType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FeedURLInfoType.swift; path = Protocols/FeedURLInfoType.swift; sourceTree = ""; }; + 50DBDEF01C48C2240044E21B /* OpenGraphInfoType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = OpenGraphInfoType.swift; path = Protocols/OpenGraphInfoType.swift; sourceTree = ""; }; 50DBDEFB1C4CCB030044E21B /* ChatLeftTextURLCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ChatLeftTextURLCell.swift; path = Views/Cells/ChatLeftTextURL/ChatLeftTextURLCell.swift; sourceTree = ""; }; 50DBDEFF1C4CE75E0044E21B /* ChatRightTextURLCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ChatRightTextURLCell.swift; path = Views/Cells/ChatRightTextURL/ChatRightTextURLCell.swift; sourceTree = ""; }; 50DBDF041C4DD9530044E21B /* CGSize+Yep.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "CGSize+Yep.swift"; path = "Extensions/CGSize+Yep.swift"; sourceTree = ""; }; @@ -2071,7 +2071,7 @@ 50DBDEF21C48C2290044E21B /* Protocols */ = { isa = PBXGroup; children = ( - 50DBDEF01C48C2240044E21B /* FeedURLInfoType.swift */, + 50DBDEF01C48C2240044E21B /* OpenGraphInfoType.swift */, ); name = Protocols; sourceTree = ""; @@ -2872,7 +2872,7 @@ 0A90187E1B01152800AE4B7F /* ProfileSocialAccountCell.swift in Sources */, 50F5AF831BBA67060033C9BC /* NewFeedViewController.swift in Sources */, 50FE29CD1C4786AE0067CA03 /* FeedURLCell.swift in Sources */, - 50DBDEF11C48C2240044E21B /* FeedURLInfoType.swift in Sources */, + 50DBDEF11C48C2240044E21B /* OpenGraphInfoType.swift in Sources */, 50894CF51AEA4A01000981AF /* SettingsUserCell.swift in Sources */, 84385C441B8592120071130D /* ShowStepGeniusViewController.swift in Sources */, 6A5632931BC911CB00397B53 /* DiscoverNormalUserCell.swift in Sources */, diff --git a/Yep/AppDelegate.swift b/Yep/AppDelegate.swift index 53cc5816..4f9a4ebf 100644 --- a/Yep/AppDelegate.swift +++ b/Yep/AppDelegate.swift @@ -43,7 +43,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier(YepConfig.appGroupID)! let realmPath = directory.URLByAppendingPathComponent("db.realm").path! - return Realm.Configuration(path: realmPath, schemaVersion: 22, migrationBlock: { migration, oldSchemaVersion in + return Realm.Configuration(path: realmPath, schemaVersion: 23, migrationBlock: { migration, oldSchemaVersion in }) } diff --git a/Yep/Protocols/FeedURLInfoType.swift b/Yep/Protocols/OpenGraphInfoType.swift similarity index 84% rename from Yep/Protocols/FeedURLInfoType.swift rename to Yep/Protocols/OpenGraphInfoType.swift index f976a754..5f75247a 100644 --- a/Yep/Protocols/FeedURLInfoType.swift +++ b/Yep/Protocols/OpenGraphInfoType.swift @@ -1,5 +1,5 @@ // -// FeedURLInfoType.swift +// OpenGraphInfoType.swift // Yep // // Created by nixzhu on 16/1/15. @@ -8,7 +8,7 @@ import Foundation -protocol FeedURLInfoType { +protocol OpenGraphInfoType { var URL: NSURL { get } diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index d23a47fd..50697fb4 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -525,8 +525,8 @@ class Message: Object { return String(format: NSLocalizedString("%@ recalled a message.", comment: ""), nickname) } - dynamic var openGraphURLDetected: Bool = false - dynamic var openGraphURLInfo: FeedURLInfo? + dynamic var openGraphDetected: Bool = false + dynamic var openGraphInfo: OpenGraphInfo? dynamic var coordinate: Coordinate? @@ -592,8 +592,13 @@ class Message: Object { realm.delete(mediaMetaData) } - if let openGraphURLInfo = openGraphURLInfo { - realm.delete(openGraphURLInfo) + // 除非没有谁指向 openGraphInfo,不然不能删除它 + if let openGraphInfo = openGraphInfo { + if openGraphInfo.feeds.isEmpty { + if openGraphInfo.messages.count == 1, let first = openGraphInfo.messages.first where first == self { + realm.delete(openGraphInfo) + } + } } switch mediaType { @@ -821,7 +826,7 @@ class FeedLocation: Object { dynamic var coordinate: Coordinate? } -class FeedURLInfo: Object { +class OpenGraphInfo: Object { dynamic var URLString: String = "" dynamic var siteName: String = "" @@ -829,6 +834,13 @@ class FeedURLInfo: Object { dynamic var infoDescription: String = "" dynamic var thumbnailImageURLString: String = "" + var messages: [Message] { + return linkingObjects(Message.self, forProperty: "openGraphInfo") + } + var feeds: [Feed] { + return linkingObjects(Feed.self, forProperty: "openGraphInfo") + } + override class func primaryKey() -> String? { return "URLString" } @@ -847,12 +859,12 @@ class FeedURLInfo: Object { self.thumbnailImageURLString = thumbnailImageURLString } - class func withURLString(URLString: String, inRealm realm: Realm) -> FeedURLInfo? { - return realm.objects(FeedURLInfo).filter("URLString = %@", URLString).first + class func withURLString(URLString: String, inRealm realm: Realm) -> OpenGraphInfo? { + return realm.objects(OpenGraphInfo).filter("URLString = %@", URLString).first } } -extension FeedURLInfo: FeedURLInfoType { +extension OpenGraphInfo: OpenGraphInfoType { var URL: NSURL { return NSURL(string: URLString)! @@ -877,7 +889,7 @@ class Feed: Object { dynamic var socialWork: MessageSocialWork? dynamic var audio: FeedAudio? dynamic var location: FeedLocation? - dynamic var URLInfo: FeedURLInfo? + dynamic var openGraphInfo: OpenGraphInfo? dynamic var skill: UserSkill? @@ -926,6 +938,15 @@ class Feed: Object { realm.delete(location) } + // 除非没有谁指向 openGraphInfo,不然不能删除它 + if let openGraphInfo = openGraphInfo { + if openGraphInfo.messages.isEmpty { + if openGraphInfo.feeds.count == 1, let first = openGraphInfo.messages.first where first == self { + realm.delete(openGraphInfo) + } + } + } + realm.delete(self) } } @@ -1307,17 +1328,17 @@ func saveFeedWithDiscoveredFeed(feedData: DiscoveredFeed, group: Group, inRealm feed.location = feedLocation - case .URL(let URLInfo): + case .URL(let info): - guard feed.URLInfo == nil else { + guard feed.openGraphInfo == nil else { break } - let feedURLInfo = FeedURLInfo(URLString: URLInfo.URL.absoluteString, siteName: URLInfo.siteName, title: URLInfo.title, infoDescription: URLInfo.infoDescription, thumbnailImageURLString: URLInfo.thumbnailImageURLString) + let openGraphInfo = OpenGraphInfo(URLString: info.URL.absoluteString, siteName: info.siteName, title: info.title, infoDescription: info.infoDescription, thumbnailImageURLString: info.thumbnailImageURLString) - realm.add(feedURLInfo, update: true) + realm.add(openGraphInfo, update: true) - feed.URLInfo = feedURLInfo + feed.openGraphInfo = openGraphInfo } } } diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index 2ea09385..57a161d7 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -2959,7 +2959,7 @@ struct DiscoveredFeed: Hashable { } } - struct URLInfo: FeedURLInfoType { + struct OpenGraphInfo: OpenGraphInfoType { let URL: NSURL @@ -2968,7 +2968,7 @@ struct DiscoveredFeed: Hashable { let infoDescription: String let thumbnailImageURLString: String - static func fromJSONDictionary(json: JSONDictionary) -> URLInfo? { + static func fromJSONDictionary(json: JSONDictionary) -> OpenGraphInfo? { guard let URLString = json["url"] as? String, URL = NSURL(string: URLString), @@ -2979,7 +2979,7 @@ struct DiscoveredFeed: Hashable { return nil } - return URLInfo(URL: URL, siteName: siteName, title: title, infoDescription: infoDescription, thumbnailImageURLString: thumbnailImageURLString) + return OpenGraphInfo(URL: URL, siteName: siteName, title: title, infoDescription: infoDescription, thumbnailImageURLString: thumbnailImageURLString) } } @@ -2989,7 +2989,7 @@ struct DiscoveredFeed: Hashable { case Dribbble(DribbbleShot) case Audio(AudioInfo) case Location(LocationInfo) - case URL(URLInfo) + case URL(OpenGraphInfo) } let attachment: Attachment? @@ -3069,10 +3069,10 @@ struct DiscoveredFeed: Hashable { case .URL: if let - URLsData = feedInfo["attachments"] as? [JSONDictionary], - URLInfoDic = URLsData.first, - URLInfo = DiscoveredFeed.URLInfo.fromJSONDictionary(URLInfoDic) { - attachment = .URL(URLInfo) + openGraphInfosData = feedInfo["attachments"] as? [JSONDictionary], + openGraphInfoDict = openGraphInfosData.first, + openGraphInfo = DiscoveredFeed.OpenGraphInfo.fromJSONDictionary(openGraphInfoDict) { + attachment = .URL(openGraphInfo) } case .Image: diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 700cad60..4fc2149b 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -259,18 +259,18 @@ enum ConversationFeed { return nil } - var URLInfo: FeedURLInfoType? { + var openGraphInfo: OpenGraphInfoType? { switch self { case .DiscoveredFeedType(let discoveredFeed): if let attachment = discoveredFeed.attachment { - if case let .URL(URLInfo) = attachment { - return URLInfo + if case let .URL(openGraphInfo) = attachment { + return openGraphInfo } } case .FeedType(let feed): - if let URLInfo = feed.URLInfo { - return URLInfo + if let openGraphInfo = feed.openGraphInfo { + return openGraphInfo } } @@ -1887,7 +1887,7 @@ class ConversationViewController: BaseViewController { height = max(ceil(rect.height) + (11 * 2), YepConfig.chatCellAvatarSize()) - if message.openGraphURLInfo != nil { + if message.openGraphInfo != nil { height += 100 + 10 } @@ -3762,7 +3762,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi return cell } else { - if message.openGraphURLInfo != nil { + if message.openGraphInfo != nil { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatLeftTextURLCellIdentifier, forIndexPath: indexPath) as! ChatLeftTextURLCell return cell @@ -3799,7 +3799,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi default: - if message.openGraphURLInfo != nil { + if message.openGraphInfo != nil { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatRightTextURLCellIdentifier, forIndexPath: indexPath) as! ChatRightTextURLCell return cell @@ -3989,7 +3989,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi } } else { - if message.openGraphURLInfo != nil { + if message.openGraphInfo != nil { if let cell = cell as? ChatLeftTextURLCell { @@ -4191,7 +4191,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi }) } - if message.openGraphURLInfo != nil { + if message.openGraphInfo != nil { if let cell = cell as? ChatRightTextURLCell { @@ -4229,19 +4229,19 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi private func tryDetectOpenGraphForMessage(message: Message) { - guard !message.openGraphURLDetected else { + guard !message.openGraphDetected else { return } - func markMessageOpenGraphURLDetected() { + func markMessageOpenGraphDetected() { let _ = try? realm.write { - message.openGraphURLDetected = true + message.openGraphDetected = true } } let text = message.textContent guard let fisrtURL = text.yep_embeddedURLs.first else { - markMessageOpenGraphURLDetected() + markMessageOpenGraphDetected() return } @@ -4249,7 +4249,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi defaultFailureHandler(reason, errorMessage: errorMessage) dispatch_async(dispatch_get_main_queue()) { - markMessageOpenGraphURLDetected() + markMessageOpenGraphDetected() } }, completion: { _openGraph in @@ -4265,14 +4265,14 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi return } - let openGraphURLInfo = FeedURLInfo(URLString: _openGraph.URL.absoluteString, siteName: _openGraph.siteName ?? "", title: _openGraph.title ?? "", infoDescription: _openGraph.description ?? "", thumbnailImageURLString: _openGraph.previewImageURLString ?? "") + let openGraphInfo = OpenGraphInfo(URLString: _openGraph.URL.absoluteString, siteName: _openGraph.siteName ?? "", title: _openGraph.title ?? "", infoDescription: _openGraph.description ?? "", thumbnailImageURLString: _openGraph.previewImageURLString ?? "") let _ = try? strongSelf.realm.write { - strongSelf.realm.add(openGraphURLInfo, update: true) - message.openGraphURLInfo = openGraphURLInfo + strongSelf.realm.add(openGraphInfo, update: true) + message.openGraphInfo = openGraphInfo } - markMessageOpenGraphURLDetected() + markMessageOpenGraphDetected() // update UI strongSelf.clearHeightOfMessageWithKey(message.messageID) diff --git a/Yep/Views/Cells/ChatLeftTextURL/ChatLeftTextURLCell.swift b/Yep/Views/Cells/ChatLeftTextURL/ChatLeftTextURLCell.swift index 0120f19c..a63a9790 100644 --- a/Yep/Views/Cells/ChatLeftTextURL/ChatLeftTextURLCell.swift +++ b/Yep/Views/Cells/ChatLeftTextURL/ChatLeftTextURLCell.swift @@ -51,9 +51,9 @@ class ChatLeftTextURLCell: ChatLeftTextCell { let feedURLContainerViewFrame = CGRect(x: textContentTextView.frame.origin.x - 12 + 1, y: CGRectGetMaxY(textContentTextView.frame) + 8, width: width, height: 100) feedURLContainerView.frame = feedURLContainerViewFrame - if let openGraphURLInfo = message.openGraphURLInfo { - feedURLContainerView.configureWithFeedURLInfoType(openGraphURLInfo) - openGraphURL = openGraphURLInfo.URL + if let openGraphInfo = message.openGraphInfo { + feedURLContainerView.configureWithOpenGraphInfoType(openGraphInfo) + openGraphURL = openGraphInfo.URL } } } diff --git a/Yep/Views/Cells/ChatRightTextURL/ChatRightTextURLCell.swift b/Yep/Views/Cells/ChatRightTextURL/ChatRightTextURLCell.swift index 6ad41e2e..4637ab70 100644 --- a/Yep/Views/Cells/ChatRightTextURL/ChatRightTextURLCell.swift +++ b/Yep/Views/Cells/ChatRightTextURL/ChatRightTextURLCell.swift @@ -52,9 +52,9 @@ class ChatRightTextURLCell: ChatRightTextCell { let feedURLContainerViewFrame = CGRect(x: fullWidth - 65 - width - 1, y: CGRectGetMaxY(textContainerView.frame) + 8, width: width, height: 100) feedURLContainerView.frame = feedURLContainerViewFrame - if let openGraphURLInfo = message.openGraphURLInfo { - feedURLContainerView.configureWithFeedURLInfoType(openGraphURLInfo) - openGraphURL = openGraphURLInfo.URL + if let openGraphInfo = message.openGraphInfo { + feedURLContainerView.configureWithOpenGraphInfoType(openGraphInfo) + openGraphURL = openGraphInfo.URL } } } diff --git a/Yep/Views/Cells/Feed/FeedURLCell.swift b/Yep/Views/Cells/Feed/FeedURLCell.swift index 06bc41bc..1885fdd1 100644 --- a/Yep/Views/Cells/Feed/FeedURLCell.swift +++ b/Yep/Views/Cells/Feed/FeedURLCell.swift @@ -60,12 +60,12 @@ class FeedURLCell: FeedBasicCell { } if let attachment = feed.attachment { - if case let .URL(URLInfo) = attachment { + if case let .URL(openGraphInfo) = attachment { - feedURLContainerView.configureWithFeedURLInfoType(URLInfo) + feedURLContainerView.configureWithOpenGraphInfoType(openGraphInfo) feedURLContainerView.tapAction = { [weak self] in - self?.tapURLInfoAction?(URL: URLInfo.URL) + self?.tapURLInfoAction?(URL: openGraphInfo.URL) } } } diff --git a/Yep/Views/ContainerViews/FeedURLContainerView.swift b/Yep/Views/ContainerViews/FeedURLContainerView.swift index a887d44e..432789f5 100644 --- a/Yep/Views/ContainerViews/FeedURLContainerView.swift +++ b/Yep/Views/ContainerViews/FeedURLContainerView.swift @@ -146,13 +146,13 @@ class FeedURLContainerView: UIView { tapAction?() } - func configureWithFeedURLInfoType(URLInfo: FeedURLInfoType) { + func configureWithOpenGraphInfoType(openGraphInfo: OpenGraphInfoType) { - siteNameLabel.text = URLInfo.siteName - titleLabel.text = URLInfo.title - descriptionLabel.text = URLInfo.infoDescription + siteNameLabel.text = openGraphInfo.siteName + titleLabel.text = openGraphInfo.title + descriptionLabel.text = openGraphInfo.infoDescription - if let thumbnailImageURL = NSURL(string: URLInfo.thumbnailImageURLString) { + if let thumbnailImageURL = NSURL(string: openGraphInfo.thumbnailImageURLString) { thumbnailImageView.kf_setImageWithURL(thumbnailImageURL, placeholderImage: nil) } else { thumbnailImageView.image = nil diff --git a/Yep/Views/Feed/FeedView.swift b/Yep/Views/Feed/FeedView.swift index c0d541b0..716b04f5 100644 --- a/Yep/Views/Feed/FeedView.swift +++ b/Yep/Views/Feed/FeedView.swift @@ -360,8 +360,8 @@ class FeedView: UIView { socialWorkContainerViewHeightConstraint.constant = 80 - if let URLInfo = feed.URLInfo { - feedURLContainerView.configureWithFeedURLInfoType(URLInfo) + if let openGraphInfo = feed.openGraphInfo { + feedURLContainerView.configureWithOpenGraphInfoType(openGraphInfo) } case .Image: @@ -512,7 +512,7 @@ class FeedView: UIView { } func tapURLInfo(sender: UITapGestureRecognizer) { - guard let URL = feed?.URLInfo?.URL else { + guard let URL = feed?.openGraphInfo?.URL else { return }