Skip to content

Commit

Permalink
feat: retrieve file type, file name and auto detect link in a text sh…
Browse files Browse the repository at this point in the history
…areintent (#31)

* feat: auto detect link in a text shareintent

* retreive sharing type from native module when available

* retreive filename and media type from ios extension
  • Loading branch information
achorein committed Mar 21, 2024
1 parent b54176f commit 25c08ae
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 396 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ export default const App = () => {
const { shareIntent } = useShareIntent();
```

| attribute | description | example |
| ------------------- | ----------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `shareIntent.text` | raw text from text/weburl (ios) and text/\* (android) | "`some text`", "`http://example.com`", "`Hey, Click on my link : http://example.com`" |
| `shareIntent.files` | image / movies / audio / files with path and type | ios: `[{ path: "file:///local/path/filename.jpg", type: "0" }]`<br/>android: `[{ path: "file:///local/path/filename"; type: "image/jpeg"; fileName: "originalFilename.jpg"; }]` |
| attribute | description | example |
| -------------------- | ----------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `shareIntent.text` | raw text from text/weburl (ios) and text/\* (android) | "`some text`", "`http://example.com`", "`Hey, Click on my link : http://example.com/nickname`" |
| `shareIntent.webUrl` | link extracted from raw text | `null`, "`http://example.com`", "`http://example.com/nickname`" |
| `shareIntent.files` | image / movies / audio / files with path and type | ios: `[{ path: "file:///local/path/filename.jpg", type: "media", fileName: "originalFilename.jpg" }]`<br/>android: `[{ path: "file:///local/path/filename", type: "image/jpeg", fileName: "originalFilename.jpg" }]` |

#### Customize Content Types in `app.json`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ class ExpoShareIntentModule : Module() {
if (intent.type!!.startsWith("text")) {
// text / urls
if (intent.action == Intent.ACTION_SEND) {
notifyShareIntent(mapOf( "text" to intent.getStringExtra(Intent.EXTRA_TEXT)))
notifyShareIntent(mapOf( "text" to intent.getStringExtra(Intent.EXTRA_TEXT), "type" to "text"))
} else if (intent.action == Intent.ACTION_VIEW) {
notifyShareIntent(mapOf( "text" to intent.dataString))
notifyShareIntent(mapOf( "text" to intent.dataString, "type" to "text"))
} else {
notifyError("Invalid action for text sharing: " + intent.action)
}
Expand All @@ -85,15 +85,15 @@ class ExpoShareIntentModule : Module() {
@Suppress("DEPRECATION") // see inline function at the end of the file
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM);
if (uri != null) {
notifyShareIntent(arrayOf(getFileInfo(uri)))
notifyShareIntent(mapOf( "files" to arrayOf(getFileInfo(uri), "type" to "file")))
} else {
notifyError("empty uri for file sharing: " + intent.action)
}
} else if (intent.action == Intent.ACTION_SEND_MULTIPLE) {
@Suppress("DEPRECATION") // see inline function at the end of the file
val uris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
if (uris != null) {
notifyShareIntent(uris.map { getFileInfo(it) })
notifyShareIntent(mapOf( "files" to uris.map { getFileInfo(it) }, "type" to "file"))
} else {
notifyError("empty uris array for file sharing: " + intent.action)
}
Expand Down
38 changes: 20 additions & 18 deletions ios/ExpoShareIntentModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ public class ExpoShareIntentModule: Module {
if let json = userDefaults?.object(forKey: key) as? Data {
let sharedArray = decode(data: json)
let sharedMediaFiles: [SharedMediaFile] = sharedArray.compactMap {
guard let path = getAbsolutePath(for: $0.path) else {
return nil
if let path = getAbsolutePath(for: $0.path) {
if ($0.type == .video && $0.thumbnail != nil) {
let thumbnail = getAbsolutePath(for: $0.thumbnail!)
return SharedMediaFile.init(path: path, thumbnail: thumbnail, fileName: $0.fileName, duration: $0.duration, type: $0.type)
} else if ($0.type == .video && $0.thumbnail == nil) {
return SharedMediaFile.init(path: path, thumbnail: nil, fileName: $0.fileName, duration: $0.duration, type: $0.type)
}
return SharedMediaFile.init(path: path, thumbnail: nil, fileName: $0.fileName, duration: $0.duration, type: $0.type)
}
if ($0.type == .video && $0.thumbnail != nil) {
let thumbnail = getAbsolutePath(for: $0.thumbnail!)
return SharedMediaFile.init(path: path, thumbnail: thumbnail, duration: $0.duration, type: $0.type)
} else if ($0.type == .video && $0.thumbnail == nil) {
return SharedMediaFile.init(path: path, thumbnail: nil, duration: $0.duration, type: $0.type)
}
return SharedMediaFile.init(path: path, thumbnail: nil, duration: $0.duration, type: $0.type)
return nil
}
guard let json = toJson(data: sharedMediaFiles) else { return "[]"};
return "{ \"files\": \(json) }";
return "{ \"files\": \(json), \"type\": \"\(url.fragment!)\" }";
} else {
return "empty"
}
Expand All @@ -81,24 +81,24 @@ public class ExpoShareIntentModule: Module {
if let json = userDefaults?.object(forKey: key) as? Data {
let sharedArray = decode(data: json)
let sharedMediaFiles: [SharedMediaFile] = sharedArray.compactMap{
guard let path = getAbsolutePath(for: $0.path) else {
return nil
if let path = getAbsolutePath(for: $0.path) {
return SharedMediaFile.init(path: path, thumbnail: nil, fileName: $0.fileName, duration: nil, type: $0.type)
}
return SharedMediaFile.init(path: path, thumbnail: nil, duration: nil, type: $0.type)
return nil
}
guard let json = toJson(data: sharedMediaFiles) else { return "[]"};
return "{ \"files\": \(json) }";
return "{ \"files\": \(json), \"type\": \"\(url.fragment!)\" }";
} else {
return "empty"
}
}
} else if url.fragment == "text" {
} else if url.fragment == "text" || url.fragment == "weburl" {
if let key = url.host?.components(separatedBy: "=").last {
if let sharedArray = userDefaults?.object(forKey: key) as? [String] {
latestText = sharedArray.joined(separator: ",")
let optionalString = latestText;
if let unwrapped = optionalString {
return "{ \"text\": \"\(unwrapped)\" }";
return "{ \"text\": \"\(unwrapped)\", \"type\": \"\(url.fragment!)\" }";
}
return latestText!;
} else {
Expand All @@ -110,7 +110,7 @@ public class ExpoShareIntentModule: Module {
let optionalString = latestText;
// now unwrap it
if let unwrapwebUrl = optionalString {
return "{ \"text\": \"\(unwrapwebUrl)\" }";
return "{ \"text\": \"\(unwrapwebUrl)\", \"type\": \"\(url.fragment!)\" }";
} else {
return "empty"
}
Expand Down Expand Up @@ -170,12 +170,14 @@ public class ExpoShareIntentModule: Module {
class SharedMediaFile: Codable {
var path: String;
var thumbnail: String?; // video thumbnail
var fileName: String?; // video thumbnail
var duration: Double?; // video duration in milliseconds
var type: SharedMediaType;

init(path: String, thumbnail: String?, duration: Double?, type: SharedMediaType) {
init(path: String, thumbnail: String?, fileName: String?, duration: Double?, type: SharedMediaType) {
self.path = path
self.thumbnail = thumbnail
self.fileName = fileName
self.duration = duration
self.type = type
}
Expand Down
Loading

0 comments on commit 25c08ae

Please sign in to comment.