Browse Source

proof of concept - sharing of simple text to selfchat

cyberta 5 years ago
parent
commit
d9af080216

+ 4 - 0
DcCore/DcCore.xcodeproj/project.pbxproj

@@ -22,6 +22,7 @@
 		30421986243F209E00516852 /* events.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30421985243F209E00516852 /* events.swift */; };
 		30421988243F23E500516852 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30421987243F23E500516852 /* Constants.swift */; };
 		306C324824460CDE001D89F3 /* DateUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 306C324724460CDE001D89F3 /* DateUtils.swift */; };
+		30E8F2212447357500CE2C90 /* DatabaseHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30E8F2202447357500CE2C90 /* DatabaseHelper.swift */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -53,6 +54,7 @@
 		30421985243F209E00516852 /* events.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = events.swift; path = ../../DcCore/DcCore/DC/events.swift; sourceTree = "<group>"; };
 		30421987243F23E500516852 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
 		306C324724460CDE001D89F3 /* DateUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateUtils.swift; sourceTree = "<group>"; };
+		30E8F2202447357500CE2C90 /* DatabaseHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseHelper.swift; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -149,6 +151,7 @@
 		30421977243F1AF400516852 /* Helper */ = {
 			isa = PBXGroup;
 			children = (
+				30E8F2202447357500CE2C90 /* DatabaseHelper.swift */,
 				30421987243F23E500516852 /* Constants.swift */,
 				3042195C243E23F100516852 /* DcUtils.swift */,
 				306C324724460CDE001D89F3 /* DateUtils.swift */,
@@ -294,6 +297,7 @@
 				30421951243DE15D00516852 /* Wrapper.swift in Sources */,
 				306C324824460CDE001D89F3 /* DateUtils.swift in Sources */,
 				30421952243DE15D00516852 /* wrapper.c in Sources */,
+				30E8F2212447357500CE2C90 /* DatabaseHelper.swift in Sources */,
 				3042195D243E23F100516852 /* DcUtils.swift in Sources */,
 				30421964243F0B8400516852 /* String+Extensions.swift in Sources */,
 				30421960243E257100516852 /* UIColor+Extensions.swift in Sources */,

+ 13 - 9
deltachat-ios/Helper/DatabaseHelper.swift → DcCore/DcCore/Helper/DatabaseHelper.swift

@@ -1,11 +1,11 @@
 import Foundation
-class DatabaseHelper {
+public class DatabaseHelper {
 
     /// The application group identifier defines a group of apps or extensions that have access to a shared container.
     /// The ID is created in the apple developer portal and can be changed there.
     static let applicationGroupIdentifier = "group.chat.delta.ios"
 
-    var sharedDbFile: String {
+    public var sharedDbFile: String {
         guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: DatabaseHelper.applicationGroupIdentifier) else {
             return ""
         }
@@ -33,7 +33,7 @@ class DatabaseHelper {
         return URL(fileURLWithPath: paths[0], isDirectory: true)
     }
 
-    var currentDatabaseLocation: String {
+    public var currentDatabaseLocation: String {
         let filemanager = FileManager.default
         if filemanager.fileExists(atPath: localDbFile) {
             return localDbFile
@@ -49,6 +49,10 @@ class DatabaseHelper {
         return sharedDbBlobsDir
     }
 
+    public init() {
+
+    }
+
     func clearDbBlobsDir(at path: String) {
         let fileManager = FileManager.default
         do {
@@ -61,7 +65,7 @@ class DatabaseHelper {
                 try fileManager.removeItem(atPath: path)
             }
         } catch {
-          logger.error("Could not clean shared blobs dir, it might be it didn't exist")
+            DcContext.shared.logger?.error("Could not clean shared blobs dir, it might be it didn't exist")
         }
     }
 
@@ -71,12 +75,12 @@ class DatabaseHelper {
             do {
                 try filemanager.removeItem(atPath: path)
             } catch {
-                logger.error("Failed to delete db: \(error)")
+                DcContext.shared.logger?.error("Failed to delete db: \(error)")
             }
         }
     }
 
-    func clearAccountData() {
+    public func clearAccountData() {
         clearDb(at: currentDatabaseLocation)
         clearDbBlobsDir(at: currentBlobsDirLocation)
     }
@@ -88,12 +92,12 @@ class DatabaseHelper {
                 clearDbBlobsDir(at: sharedDbBlobsDir)
                 try filemanager.moveItem(at: URL(fileURLWithPath: localDbBlobsDir), to: URL(fileURLWithPath: sharedDbBlobsDir))
             } catch let error {
-                logger.error("Could not move db blobs directory to shared space: \(error.localizedDescription)")
+                DcContext.shared.logger?.error("Could not move db blobs directory to shared space: \(error.localizedDescription)")
             }
         }
     }
 
-    func updateDatabaseLocation() -> String? {
+    public func updateDatabaseLocation() -> String? {
       let filemanager = FileManager.default
       if filemanager.fileExists(atPath: localDbFile) {
           do {
@@ -101,7 +105,7 @@ class DatabaseHelper {
               try filemanager.moveItem(at: URL(fileURLWithPath: localDbFile), to: URL(fileURLWithPath: sharedDbFile))
               moveBlobsFolder()
           } catch let error {
-              logger.error("Could not update DB location. Share extension will probably not work. \n\(error.localizedDescription)")
+              DcContext.shared.logger?.error("Could not update DB location. Share extension will probably not work. \n\(error.localizedDescription)")
               return localDbFile
           }
       }

+ 20 - 10
DcShare/ShareViewController.swift

@@ -1,24 +1,34 @@
-//
-//  ShareViewController.swift
-//  DcShare
-//
-//  Created by Macci on 15.04.20.
-//  Copyright © 2020 Jonas Reinsch. All rights reserved.
-//
-
 import UIKit
 import Social
+import DcCore
 
 class ShareViewController: SLComposeServiceViewController {
 
+    let dcContext = DcContext.shared
+
+    override func presentationAnimationDidFinish() {
+        let dbHelper = DatabaseHelper()
+        if dbHelper.currentDatabaseLocation == dbHelper.sharedDbFile {
+            dcContext.openDatabase(dbFile: dbHelper.sharedDbFile)
+        } else {
+            cancel()
+        }
+    }
+
     override func isContentValid() -> Bool {
         // Do validation of contentText and/or NSExtensionContext attachments here
-        return true
+        return  !(contentText?.isEmpty ?? true)
     }
 
     override func didSelectPost() {
         // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
-    
+
+        let selfchatId = dcContext.getChatIdByContactId(contactId: Int(DC_CONTACT_ID_SELF))
+        let message = DcMsg(viewType: DC_MSG_TEXT)
+        message.text = self.contentText
+        message.sendInChat(id: selfchatId)
+
+        dcContext.closeDatabase()
         // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
         self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
     }

+ 0 - 4
deltachat-ios.xcodeproj/project.pbxproj

@@ -97,7 +97,6 @@
 		30E8F2132447285600CE2C90 /* ShareViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30E8F2122447285600CE2C90 /* ShareViewController.swift */; };
 		30E8F2162447285600CE2C90 /* MainInterface.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 30E8F2142447285600CE2C90 /* MainInterface.storyboard */; };
 		30E8F21A2447285600CE2C90 /* DcShare.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 30E8F2102447285600CE2C90 /* DcShare.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
-		30ECA5472436026F006F2E7A /* DatabaseHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30ECA5462436026F006F2E7A /* DatabaseHelper.swift */; };
 		30F9B9EC235F2116006E7ACF /* MessageCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30F9B9EB235F2116006E7ACF /* MessageCounter.swift */; };
 		6795F63A82E94FF7CD2CEC0F /* Pods_deltachat_iosTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2F7009234DB9408201A6CDCB /* Pods_deltachat_iosTests.framework */; };
 		7070FB9B2101ECBB000DC258 /* NewGroupController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7070FB9A2101ECBB000DC258 /* NewGroupController.swift */; };
@@ -364,7 +363,6 @@
 		30E8F2152447285600CE2C90 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/MainInterface.storyboard; sourceTree = "<group>"; };
 		30E8F2172447285600CE2C90 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		30E8F21F24472AAE00CE2C90 /* DcShare.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = DcShare.entitlements; sourceTree = "<group>"; };
-		30ECA5462436026F006F2E7A /* DatabaseHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseHelper.swift; sourceTree = "<group>"; };
 		30F9B9EB235F2116006E7ACF /* MessageCounter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageCounter.swift; sourceTree = "<group>"; };
 		6241BE1534A653E79AD5D01D /* Pods_deltachat_ios.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_deltachat_ios.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		7070FB9A2101ECBB000DC258 /* NewGroupController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewGroupController.swift; sourceTree = "<group>"; };
@@ -873,7 +871,6 @@
 				AE1988A423EB2FBA00B4CD5F /* Errors.swift */,
 				AEFBE23023FF09B20045327A /* TypeAlias.swift */,
 				307D822D241669C7006D2490 /* LocationManager.swift */,
-				30ECA5462436026F006F2E7A /* DatabaseHelper.swift */,
 			);
 			path = Helper;
 			sourceTree = "<group>";
@@ -1269,7 +1266,6 @@
 				3040F45E234DFBC000FA34D5 /* Audio.swift in Sources */,
 				305961FE2346125100C80F33 /* InsetLabel.swift in Sources */,
 				3095A351237DD1F700AB07F7 /* MediaPicker.swift in Sources */,
-				30ECA5472436026F006F2E7A /* DatabaseHelper.swift in Sources */,
 				B21005DB23383664004C70C5 /* SettingsClassicViewController.swift in Sources */,
 				305961F62346125100C80F33 /* MessageContentCell.swift in Sources */,
 				305961E42346125100C80F33 /* MessageKitDateFormatter.swift in Sources */,