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
6 changed files
with
1,156 additions
and
1,370 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
.../jsMain/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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package io.michaelrocks.libphonenumber.kotlin.metadata | ||
|
||
import io.michaelrocks.libphonenumber.kotlin.MetadataLoader | ||
import io.michaelrocks.libphonenumber.kotlin.metadata.init.MokoAssetResourceMetadataLoader | ||
import io.michaelrocks.libphonenumber.kotlin.metadata.init.ComposeResourceMetadataLoader | ||
|
||
actual fun defaultMetadataLoader(): MetadataLoader { | ||
return MokoAssetResourceMetadataLoader() | ||
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 { | ||
console.error("Failed to fetch file. Status: ${xhr.status}") | ||
null | ||
} | ||
} | ||
|
||
|
||
override fun loadMetadata(phoneMetadataResource: String): InputStream? { | ||
return try { | ||
val buffer = Buffer() | ||
val path = Res.getUri(phoneMetadataResource) | ||
console.log("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() | ||
) | ||
} | ||
} |
53 changes: 0 additions & 53 deletions
53
...in/io/michaelrocks/libphonenumber/kotlin/metadata/init/MokoAssetResourceMetadataLoader.kt
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file modified
BIN
+155 Bytes
(100%)
.../project.xcworkspace/xcuserdata/lucaspinazzola.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.