NekoX/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/UpdateUtil.kt

80 lines
3.1 KiB
Kotlin

package tw.nekomimi.nekogram.utils
import android.content.Context
import org.telegram.messenger.*
import org.telegram.messenger.browser.Browser
import org.telegram.tgnet.ConnectionsManager
import org.telegram.tgnet.TLObject
import org.telegram.tgnet.TLRPC
import org.telegram.ui.ActionBar.AlertDialog
object UpdateUtil {
const val channelUsername = "NekogramX"
@JvmStatic
fun postCheckFollowChannel(ctx: Context, currentAccount: Int) = UIUtil.runOnIoDispatcher {
if (MessagesController.getMainSettings(currentAccount).getBoolean("update_channel_skip", false)) return@runOnIoDispatcher
val messagesCollector = MessagesController.getInstance(currentAccount)
val connectionsManager = ConnectionsManager.getInstance(currentAccount)
val messagesStorage = MessagesStorage.getInstance(currentAccount)
val updateChannel = messagesCollector.getUserOrChat(channelUsername)
if (updateChannel is TLRPC.Chat) checkFollowChannel(ctx, currentAccount, updateChannel) else {
connectionsManager.sendRequest(TLRPC.TL_contacts_resolveUsername().apply {
username = channelUsername
}) { response: TLObject?, error: TLRPC.TL_error? ->
if (error == null) {
val res = response as TLRPC.TL_contacts_resolvedPeer
if (res.chats.isEmpty()) {
return@sendRequest
}
messagesCollector.putChats(res.chats, false)
messagesStorage.putUsersAndChats(res.users, res.chats, false, true)
checkFollowChannel(ctx, currentAccount, res.chats.find { it.username == channelUsername }!!)
}
}
}
}
private fun checkFollowChannel(ctx: Context, currentAccount: Int, channel: TLRPC.Chat) {
if (!channel.left || channel.kicked) {
// MessagesController.getMainSettings(currentAccount).edit().putBoolean("update_channel_skip", true).apply()
return
}
UIUtil.runOnUIThread {
val messagesCollector = MessagesController.getInstance(currentAccount)
val userConfig = UserConfig.getInstance(currentAccount)
val builder = AlertDialog.Builder(ctx)
builder.setTitle(LocaleController.getString("FCTitle", R.string.FCTitle))
builder.setMessage(LocaleController.getString("FCInfo", R.string.FCInfo))
builder.setPositiveButton(LocaleController.getString("ChannelJoin", R.string.ChannelJoin)) { _, _ ->
messagesCollector.addUserToChat(channel.id, userConfig.currentUser, null, 0, null, null, null)
Browser.openUrl(ctx, "https://t.me/$channelUsername")
}
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null)
builder.setNeutralButton(LocaleController.getString("DoNotRemindAgain", R.string.DoNotRemindAgain)) { _, _ ->
MessagesController.getMainSettings(currentAccount).edit().putBoolean("update_channel_skip", true).apply()
}
builder.show()
}
}
}