ref: SharedPref to Splitties
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
/*
|
||||
* Created by sweetbread
|
||||
* Copyright (c) 2025. All rights reserved.
|
||||
* Last modified 03.03.2025, 15:32
|
||||
*/
|
||||
|
||||
package ru.risdeveau.pixeldragon
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.room.Room
|
||||
import io.ktor.client.HttpClient
|
||||
@@ -17,6 +15,7 @@ import io.ktor.client.plugins.logging.Logging
|
||||
import ru.risdeveau.pixeldragon.api.getMe
|
||||
import ru.risdeveau.pixeldragon.db.AppDatabase
|
||||
import splitties.init.appCtx
|
||||
import splitties.preferences.Preferences
|
||||
|
||||
val client = HttpClient {
|
||||
install(Logging) {
|
||||
@@ -31,24 +30,25 @@ val client = HttpClient {
|
||||
install(HttpCache)
|
||||
}
|
||||
|
||||
val accountData = appCtx.getSharedPreferences("settings", Context.MODE_PRIVATE)
|
||||
lateinit var homeserver: String
|
||||
lateinit var baseUrl: String
|
||||
lateinit var token: String
|
||||
|
||||
object AccountData : Preferences("settings") {
|
||||
var token by stringPref("token", "")
|
||||
var homeserver by stringPref("homeserver", "")
|
||||
}
|
||||
|
||||
suspend fun initCheck(): Boolean {
|
||||
Log.d("initCheck", "checking...")
|
||||
|
||||
if (!accountData.contains("token")) return false
|
||||
if (!accountData.contains("homeserver")) return false
|
||||
token = AccountData.token
|
||||
homeserver = AccountData.homeserver
|
||||
|
||||
if (token.isEmpty() or homeserver.isEmpty()) return false
|
||||
|
||||
token = accountData.getString("token", "").toString()
|
||||
homeserver = accountData.getString("homeserver", "").toString()
|
||||
baseUrl = "$homeserver/_matrix/client/v3"
|
||||
|
||||
Log.d("initCheck", "homeserver: $homeserver")
|
||||
|
||||
return getMe() != null
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
/*
|
||||
* Created by sweetbread
|
||||
* Copyright (c) 2025. All rights reserved.
|
||||
* Last modified 03.03.2025, 15:43
|
||||
*/
|
||||
|
||||
package ru.risdeveau.pixeldragon.api
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.util.Log
|
||||
import io.ktor.client.request.bearerAuth
|
||||
import io.ktor.client.request.get
|
||||
@@ -17,12 +15,14 @@ import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.contentType
|
||||
import org.json.JSONObject
|
||||
import ru.risdeveau.pixeldragon.accountData
|
||||
import ru.risdeveau.pixeldragon.AccountData
|
||||
import ru.risdeveau.pixeldragon.baseUrl
|
||||
import ru.risdeveau.pixeldragon.client
|
||||
import ru.risdeveau.pixeldragon.initCheck
|
||||
import ru.risdeveau.pixeldragon.token
|
||||
import splitties.experimental.ExperimentalSplittiesApi
|
||||
import splitties.init.appCtx
|
||||
import splitties.preferences.edit
|
||||
|
||||
data class Me (val userId: String, val deviceId: String)
|
||||
|
||||
@@ -40,9 +40,9 @@ suspend fun getMe(): Me? {
|
||||
return Me(json.getString("user_id"), json.getString("device_id"))
|
||||
}
|
||||
|
||||
@SuppressLint("ApplySharedPref")
|
||||
@OptIn(ExperimentalSplittiesApi::class)
|
||||
suspend fun login(server: String, login: String, pass: String): Boolean {
|
||||
val homeserver = getHomeserver(server)!!
|
||||
val hs = getHomeserver(server)!!
|
||||
|
||||
val pinfo = appCtx.packageManager.getPackageInfo(appCtx.packageName, 0)
|
||||
|
||||
@@ -60,7 +60,7 @@ suspend fun login(server: String, login: String, pass: String): Boolean {
|
||||
json.put("password", pass)
|
||||
|
||||
val r = try {
|
||||
client.post("$homeserver/_matrix/client/v3/login") {
|
||||
client.post("$hs/_matrix/client/v3/login") {
|
||||
setBody(json.toString())
|
||||
contentType(ContentType.Application.Json)
|
||||
}
|
||||
@@ -75,10 +75,10 @@ suspend fun login(server: String, login: String, pass: String): Boolean {
|
||||
}
|
||||
|
||||
val res = JSONObject(r.bodyAsText())
|
||||
val editor = accountData.edit()
|
||||
editor.putString("token", res.getString("access_token"))
|
||||
editor.putString("homeserver", homeserver)
|
||||
editor.commit()
|
||||
AccountData.edit {
|
||||
token = res.getString("access_token")
|
||||
homeserver = hs
|
||||
}
|
||||
|
||||
return initCheck()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user