diff --git a/example/build.gradle b/example/build.gradle index b8796b2..957959f 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -32,7 +32,7 @@ android { } dependencies { -// implementation project(path: ':fcl-android') + implementation project(path: ':fcl-android') implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.4.1' @@ -42,7 +42,7 @@ dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2' - implementation 'com.github.Outblock:fcl-android:0.03' +// implementation 'com.github.Outblock:fcl-android:0.03' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' diff --git a/example/src/main/java/io/outblock/fcl/example/MainActivity.kt b/example/src/main/java/io/outblock/fcl/example/MainActivity.kt index c421276..bba13e3 100644 --- a/example/src/main/java/io/outblock/fcl/example/MainActivity.kt +++ b/example/src/main/java/io/outblock/fcl/example/MainActivity.kt @@ -15,8 +15,9 @@ import androidx.core.content.ContextCompat import com.google.android.material.tabs.TabLayout import io.outblock.fcl.Fcl import io.outblock.fcl.provider.WalletProvider -import io.outblock.fcl.utils.ioScope -import io.outblock.fcl.utils.uiScope +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -46,7 +47,7 @@ class MainActivity : AppCompatActivity() { """.trimIndent() ) findViewById(R.id.button_query).setOnClickListener { - ioScope { + CoroutineScope(Dispatchers.IO).launch { val cadence = edittext.text.toString() val result = Fcl.query { cadence(cadence) @@ -54,17 +55,17 @@ class MainActivity : AppCompatActivity() { arg { int(3) } arg { address("0xba1132bc08f82fe2") } } - uiScope { findViewById(R.id.query_result_view).text = result } + CoroutineScope(Dispatchers.Main).launch { findViewById(R.id.query_result_view).text = result } } } } private fun setupSignMessage() { findViewById(R.id.button_sign_message).setOnClickListener { - ioScope { + CoroutineScope(Dispatchers.IO).launch { val message = findViewById(R.id.sign_message_edittext).text.toString() val signature = Fcl.signMessage(message) - uiScope { findViewById(R.id.signed_message_view).text = signature } + CoroutineScope(Dispatchers.Main).launch { findViewById(R.id.signed_message_view).text = signature } } } } @@ -91,11 +92,11 @@ class MainActivity : AppCompatActivity() { findViewById(R.id.auth_button).setOnClickListener { val provider = if (tabLayout.selectedTabPosition == 0) WalletProvider.DAPPER else WalletProvider.BLOCTO - ioScope { + CoroutineScope(Dispatchers.IO).launch { val auth = Fcl.authenticate(provider) Log.d(TAG, "authenticate complete:$auth") - uiScope { - Toast.makeText(this, "authenticate complete", Toast.LENGTH_SHORT).show() + CoroutineScope(Dispatchers.Main).launch { + Toast.makeText(this@MainActivity, "authenticate complete", Toast.LENGTH_SHORT).show() findViewById(R.id.address).text = auth.address } } @@ -123,7 +124,7 @@ class MainActivity : AppCompatActivity() { ) button.setOnClickListener { - ioScope { + CoroutineScope(Dispatchers.IO).launch { val tid = Fcl.mutate { cadence(editText.text.toString()) arg { string("Test2") } @@ -131,7 +132,7 @@ class MainActivity : AppCompatActivity() { gaslimit(1000) } Log.d(TAG, "tid:$tid") - uiScope { + CoroutineScope(Dispatchers.Main).launch { txidView.text = tid txidLayout.visibility = View.VISIBLE viewOnFlowScanView.setOnClickListener { @@ -144,7 +145,7 @@ class MainActivity : AppCompatActivity() { } } - fun String.openInSystemBrowser(context: Context) { + private fun String.openInSystemBrowser(context: Context) { ContextCompat.startActivity(context, Intent(Intent.ACTION_VIEW, Uri.parse(this)), null) } diff --git a/fcl-android/src/main/java/io/outblock/fcl/utils/CoroutineUtils.kt b/fcl-android/src/main/java/io/outblock/fcl/utils/CoroutineUtils.kt index 4beb1fb..fd12815 100644 --- a/fcl-android/src/main/java/io/outblock/fcl/utils/CoroutineUtils.kt +++ b/fcl-android/src/main/java/io/outblock/fcl/utils/CoroutineUtils.kt @@ -3,20 +3,20 @@ package io.outblock.fcl.utils import io.outblock.fcl.BuildConfig import kotlinx.coroutines.* -fun ioScope(unit: suspend () -> Unit) = CoroutineScope(Dispatchers.IO).launch { execute(unit) } +internal fun ioScope(unit: suspend () -> Unit) = CoroutineScope(Dispatchers.IO).launch { execute(unit) } -suspend fun contextScope(unit: suspend () -> Unit) = withContext(Dispatchers.Main) { execute(unit) } +internal suspend fun contextScope(unit: suspend () -> Unit) = withContext(Dispatchers.Main) { execute(unit) } -fun uiScope(unit: suspend () -> Unit) = CoroutineScope(Dispatchers.Main).launch { execute(unit) } +internal fun uiScope(unit: suspend () -> Unit) = CoroutineScope(Dispatchers.Main).launch { execute(unit) } -fun uiDelay(delayMs: Long, unit: suspend () -> Unit) = CoroutineScope(Dispatchers.Main).launch { +internal fun uiDelay(delayMs: Long, unit: suspend () -> Unit) = CoroutineScope(Dispatchers.Main).launch { delay(delayMs) execute(unit) } -fun cpuScope(unit: suspend () -> Unit) = CoroutineScope(Dispatchers.Default).launch { execute(unit) } +internal fun cpuScope(unit: suspend () -> Unit) = CoroutineScope(Dispatchers.Default).launch { execute(unit) } -suspend fun repeatWhen( +internal suspend fun repeatWhen( predicate: suspend () -> Boolean, block: suspend () -> Unit, ) { @@ -25,7 +25,7 @@ suspend fun repeatWhen( } } -suspend fun runBlockDelay(timeMillis: Long, block: suspend () -> Unit) { +internal suspend fun runBlockDelay(timeMillis: Long, block: suspend () -> Unit) { val startTime = System.currentTimeMillis() block.invoke() val elapsedTime = System.currentTimeMillis() - startTime diff --git a/fcl-android/src/main/java/io/outblock/fcl/utils/Log.kt b/fcl-android/src/main/java/io/outblock/fcl/utils/Log.kt index 3d74dea..06d3235 100644 --- a/fcl-android/src/main/java/io/outblock/fcl/utils/Log.kt +++ b/fcl-android/src/main/java/io/outblock/fcl/utils/Log.kt @@ -5,27 +5,27 @@ import io.outblock.fcl.BuildConfig private val PRINT_LOG = BuildConfig.DEBUG -fun logv(tag: String?, msg: Any?) { +internal fun logv(tag: String?, msg: Any?) { log(tag, msg, Log.VERBOSE) } -fun logd(tag: String?, msg: Any?) { +internal fun logd(tag: String?, msg: Any?) { log(tag, msg, Log.DEBUG) } -fun logi(tag: String?, msg: Any?) { +internal fun logi(tag: String?, msg: Any?) { log(tag, msg, Log.INFO) } -fun logw(tag: String?, msg: Any?) { +internal fun logw(tag: String?, msg: Any?) { log(tag, msg, Log.WARN) } -fun loge(tag: String?, msg: Any?) { +internal fun loge(tag: String?, msg: Any?) { log(tag, msg, Log.ERROR) } -fun loge(msg: Throwable?, printStackTrace: Boolean = true) { +internal fun loge(msg: Throwable?, printStackTrace: Boolean = true) { log("Exception", msg?.message ?: "", Log.ERROR) if (PRINT_LOG && printStackTrace) { msg?.printStackTrace()