Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
IBMS
/
nativeview
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d8200ebf
authored
Feb 22, 2022
by
huangzhicong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复storage接口回调没有执行在主线程的问题
parent
ccd0ad5f
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
0 deletions
+14
-0
app/src/main/java/com/maxrocky/nativeview/contract/StorageContract.kt
+14
-0
No files found.
app/src/main/java/com/maxrocky/nativeview/contract/StorageContract.kt
View file @
d8200ebf
...
...
@@ -12,6 +12,7 @@ import kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.flow.collect
import
kotlinx.coroutines.flow.map
import
kotlinx.coroutines.launch
import
kotlinx.coroutines.withContext
class
StorageContract
(
fragment
:
Fragment
,
proxy
:
WebViewProxy
)
:
IContract
(
fragment
,
proxy
){
companion
object
{
...
...
@@ -32,15 +33,19 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm
val
storageKey
=
stringPreferencesKey
(
key
)
maxrocky
[
storageKey
]
=
value
val
data
=
StorageResult
(
key
,
value
)
withContext
(
Dispatchers
.
Main
)
{
proxy
.
success
(
STORAGE_METHOD
,
storageResult
,
data
)
storageResult
.
reset
()
}
}
}
catch
(
e
:
Exception
)
{
withContext
(
Dispatchers
.
Main
)
{
proxy
.
failed
(
STORAGE_METHOD
,
storageResult
,
e
.
message
?:
""
)
storageResult
.
reset
()
}
}
}
}
fun
acquireValue
(
sessionId
:
Int
,
key
:
String
)
{
acquireResult
.
setupSessionId
(
sessionId
)
...
...
@@ -48,15 +53,19 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm
try
{
context
.
dataStore
.
data
.
map
{
it
[
stringPreferencesKey
(
key
)]
?:
""
}.
collect
{
storageValue
->
val
data
=
StorageResult
(
key
,
storageValue
)
withContext
(
Dispatchers
.
Main
)
{
proxy
.
success
(
ACQUIRE_METHOD
,
acquireResult
,
data
)
acquireResult
.
reset
()
}
}
}
catch
(
e
:
Exception
)
{
withContext
(
Dispatchers
.
Main
)
{
proxy
.
failed
(
ACQUIRE_METHOD
,
acquireResult
,
e
.
message
?:
""
)
acquireResult
.
reset
()
}
}
}
}
fun
clearValue
(
sessionId
:
Int
,
key
:
String
)
{
clearResult
.
setupSessionId
(
sessionId
)
...
...
@@ -69,13 +78,17 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm
}
val
storageValue
=
maxrocky
.
remove
(
storageKey
)
val
data
=
StorageResult
(
key
,
storageValue
)
withContext
(
Dispatchers
.
Main
)
{
proxy
.
success
(
CLEAR_METHOD
,
clearResult
,
data
)
clearResult
.
reset
()
}
}
}
catch
(
e
:
Exception
)
{
withContext
(
Dispatchers
.
Main
)
{
proxy
.
failed
(
CLEAR_METHOD
,
clearResult
,
e
.
message
?:
""
)
clearResult
.
reset
()
}
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment