forked from MichaelRocks/libphonenumber-android
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
272 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...mJsMain/kotlin/io/michaelrocks/libphonenumber/kotlin/metadata/defaultMetadataLoader.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.michaelrocks.libphonenumber.kotlin.metadata | ||
|
||
import io.michaelrocks.libphonenumber.kotlin.MetadataLoader | ||
import io.michaelrocks.libphonenumber.kotlin.metadata.init.ComposeResourceMetadataLoader | ||
|
||
actual fun defaultMetadataLoader(): MetadataLoader { | ||
return ComposeResourceMetadataLoader() | ||
} |
76 changes: 76 additions & 0 deletions
76
...tlin/io/michaelrocks/libphonenumber/kotlin/metadata/init/ComposeResourceMetadataLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (C) 2022 The Libphonenumber Authors | ||
* Copyright (C) 2022 Michael Rozumyanskiy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.michaelrocks.libphonenumber.kotlin.metadata.init | ||
|
||
import co.touchlab.kermit.Logger | ||
import io.github.luca992.libphonenumber_kotlin.libphonenumber.generated.resources.Res | ||
import io.michaelrocks.libphonenumber.kotlin.MetadataLoader | ||
import io.michaelrocks.libphonenumber.kotlin.io.InputStream | ||
import io.michaelrocks.libphonenumber.kotlin.io.OkioInputStream | ||
import okio.Buffer | ||
import org.w3c.xhr.XMLHttpRequest | ||
|
||
/** | ||
* A [MetadataLoader] implementation that reads phone number metadata files as classpath | ||
* resources. | ||
*/ | ||
|
||
class ComposeResourceMetadataLoader : MetadataLoader { | ||
|
||
fun fetchFileSynchronously(url: String): ByteArray? { | ||
val xhr = XMLHttpRequest() | ||
xhr.open("GET", url, false) // `false` makes it synchronous | ||
xhr.overrideMimeType("text/plain; charset=x-user-defined") // Interpret response as binary | ||
xhr.send() | ||
|
||
return if (xhr.status == 200.toShort()) { | ||
val responseText = xhr.responseText // Read response as text | ||
// Convert text response to ByteArray | ||
responseText.encodeToByteArray().map { it.toInt() and 0xFF } | ||
ByteArray(responseText.length) { i -> responseText[i].code.toByte() } | ||
} else { | ||
println("Failed to fetch file. Status: ${xhr.status}") | ||
null | ||
} | ||
} | ||
|
||
|
||
override fun loadMetadata(phoneMetadataResource: String): InputStream? { | ||
return try { | ||
val buffer = Buffer() | ||
val path = Res.getUri(phoneMetadataResource) | ||
println("loadMetadata path: $path") | ||
val result = fetchFileSynchronously(path) | ||
return if (result != null) { | ||
buffer.write(result) | ||
OkioInputStream(buffer) | ||
} else { | ||
println("Failed to fetch file.") | ||
null | ||
} | ||
} catch (t: Throwable) { | ||
logger.v("Failed to load metadata from $phoneMetadataResource.path", t) | ||
null | ||
} | ||
} | ||
|
||
companion object { | ||
private val logger = Logger.withTag( | ||
ComposeResourceMetadataLoader::class.simpleName.toString() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.window.CanvasBasedWindow | ||
import io.luca992.libphonenumber.sample.App | ||
|
||
@OptIn(ExperimentalComposeUiApi::class) | ||
fun main() { | ||
CanvasBasedWindow("libphonenumber-kotlin wasmJs sample") { | ||
Column(modifier = Modifier.fillMaxSize()) { | ||
App("WasmJs Web") | ||
} | ||
} | ||
} |
Oops, something went wrong.