Commit b59ae076 authored by Him188's avatar Him188

Add context getter

parent aed2f3f7
...@@ -43,7 +43,7 @@ internal abstract class QQAndroidBotBase constructor( ...@@ -43,7 +43,7 @@ internal abstract class QQAndroidBotBase constructor(
context: Context, context: Context,
account: BotAccount, account: BotAccount,
configuration: BotConfiguration configuration: BotConfiguration
) : BotImpl<QQAndroidBotNetworkHandler>(account, configuration) { ) : BotImpl<QQAndroidBotNetworkHandler>(context, account, configuration) {
val client: QQAndroidClient = val client: QQAndroidClient =
QQAndroidClient( QQAndroidClient(
context, context,
......
...@@ -44,7 +44,8 @@ abstract class Bot : CoroutineScope { ...@@ -44,7 +44,8 @@ abstract class Bot : CoroutineScope {
* 复制一份此时的 [Bot] 实例列表. * 复制一份此时的 [Bot] 实例列表.
*/ */
@JvmStatic @JvmStatic
val instances: List<WeakRef<Bot>> get() = BotImpl.instances.toList() val instances: List<WeakRef<Bot>>
get() = BotImpl.instances.toList()
/** /**
* 遍历每一个 [Bot] 实例 * 遍历每一个 [Bot] 实例
...@@ -58,6 +59,14 @@ abstract class Bot : CoroutineScope { ...@@ -58,6 +59,14 @@ abstract class Bot : CoroutineScope {
fun instanceWhose(qq: Long): Bot = BotImpl.instanceWhose(qq = qq) fun instanceWhose(qq: Long): Bot = BotImpl.instanceWhose(qq = qq)
} }
/**
* [Bot] 运行的 [Context].
*
* 在 JVM 的默认实现为 `class ContextImpl : Context`
* 在 Android 实现为 `android.content.Context`
*/
abstract val context: Context
/** /**
* 账号信息 * 账号信息
*/ */
......
...@@ -32,6 +32,7 @@ import kotlin.coroutines.CoroutineContext ...@@ -32,6 +32,7 @@ import kotlin.coroutines.CoroutineContext
@UseExperimental(MiraiExperimentalAPI::class) @UseExperimental(MiraiExperimentalAPI::class)
@MiraiInternalAPI @MiraiInternalAPI
abstract class BotImpl<N : BotNetworkHandler> constructor( abstract class BotImpl<N : BotNetworkHandler> constructor(
context: Context,
account: BotAccount, account: BotAccount,
val configuration: BotConfiguration val configuration: BotConfiguration
) : Bot(), CoroutineScope { ) : Bot(), CoroutineScope {
...@@ -39,6 +40,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor( ...@@ -39,6 +40,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
override val coroutineContext: CoroutineContext = override val coroutineContext: CoroutineContext =
configuration.parentCoroutineContext + botJob + (configuration.parentCoroutineContext[CoroutineExceptionHandler] configuration.parentCoroutineContext + botJob + (configuration.parentCoroutineContext[CoroutineExceptionHandler]
?: CoroutineExceptionHandler { _, e -> logger.error("An exception was thrown under a coroutine of Bot", e) }) ?: CoroutineExceptionHandler { _, e -> logger.error("An exception was thrown under a coroutine of Bot", e) })
override val context: Context by context.unsafeWeakRef()
@Suppress("CanBePrimaryConstructorProperty") // for logger @Suppress("CanBePrimaryConstructorProperty") // for logger
final override val account: BotAccount = account final override val account: BotAccount = account
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment