Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Jul 9, 2024
2 parents ece1db3 + 0c4e39e commit 3ec283a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion library/library.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'library'
spec.version = 'main-SNAPSHOT'
spec.version = 'dev-SNAPSHOT'
spec.homepage = ''
spec.source = { :http=> ''}
spec.authors = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import kotlinx.datetime.TimeZone
import org.w3c.dom.HTMLInputElement
import org.w3c.dom.HTMLScriptElement
import org.w3c.dom.url.URL
import org.w3c.files.File
import org.w3c.files.FileList
import org.w3c.files.FilePropertyBag
import kotlin.coroutines.resume
import kotlin.js.json

Expand Down Expand Up @@ -42,17 +45,23 @@ actual object ExternalServices {
}

private val validDownloadName = Regex("[a-zA-Z0-9.\\-_]+")
actual suspend fun download(name: String, url: String, preferredDestination: DownloadLocation, onDownloadProgress: ((progress: Float) -> Unit)?) {
if(!name.matches(validDownloadName)) throw IllegalArgumentException("Name $name has invalid characters!")
actual suspend fun download(
name: String,
url: String,
preferredDestination: DownloadLocation,
onDownloadProgress: ((progress: Float) -> Unit)?
) {
if (!name.matches(validDownloadName)) throw IllegalArgumentException("Name $name has invalid characters!")
val a = document.createElement("a") as HTMLAnchorElement
a.href = url
a.download = name
a.target = "_blank"
a.click()
}

@JsName("downloadBlob")
actual suspend fun download(name: String, blob: Blob, preferredDestination: DownloadLocation) {
if(!name.matches(validDownloadName)) throw IllegalArgumentException("Name $name has invalid characters!")
if (!name.matches(validDownloadName)) throw IllegalArgumentException("Name $name has invalid characters!")
val a = document.createElement("a") as HTMLAnchorElement
val url = URL.Companion.createObjectURL(blob)
a.href = url
Expand All @@ -66,7 +75,24 @@ actual object ExternalServices {

@JsName("shareBlob")
actual suspend fun share(namesToBlobs: List<Pair<String, Blob>>) {
TODO()
val files = namesToBlobs.map {
val name = it.first
val blob = it.second

val type = blob.type.split("/").lastOrNull() ?: "png"
val fileName = if (name.endsWith(".$type")) name else "${name}.${type}"

File(
fileBits = arrayOf(blob),
fileName = fileName,
options = FilePropertyBag(type = blob.type)
)
}

window.navigator.asDynamic().share(
json("files" to files.toTypedArray())
)

}

actual fun share(title: String, message: String?, url: String?) {
Expand Down Expand Up @@ -101,7 +127,7 @@ actual object ExternalServices {
zone: TimeZone
) {
fun LocalDateTime.format() = buildString {
if(zone.id != "SYSTEM") {
if (zone.id != "SYSTEM") {
append("TZID=")
append(zone.id)
append(':')
Expand All @@ -114,7 +140,9 @@ actual object ExternalServices {
append(minute.toString().padStart(2, '0'))
append(second.toString().padStart(2, '0'))
}
var calText = "BEGIN:VCALENDAR\nVERSION:2.0\nCALSCALE:GREGORIAN\nPRODID:adamgibbons/ics\nMETHOD:PUBLISH\nBEGIN:VEVENT\n";

var calText =
"BEGIN:VCALENDAR\nVERSION:2.0\nCALSCALE:GREGORIAN\nPRODID:adamgibbons/ics\nMETHOD:PUBLISH\nBEGIN:VEVENT\n";
calText += "UID:" + window.asDynamic().crypto.randomUUID() + "\n";
calText += "SUMMARY:" + title + "\n";
calText += "DTSTART:" + start.format() + "\n";
Expand Down

0 comments on commit 3ec283a

Please sign in to comment.