Skip to content

Commit

Permalink
fix: buggy at edit release
Browse files Browse the repository at this point in the history
  • Loading branch information
coaguila committed Dec 20, 2024
1 parent 4009b2f commit a87cd97
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 190 deletions.
2 changes: 0 additions & 2 deletions app/src/main/java/com/android/periodpals/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import io.github.jan.supabase.auth.Auth
import io.github.jan.supabase.createSupabaseClient
import io.github.jan.supabase.postgrest.Postgrest
import io.github.jan.supabase.storage.Storage
import kotlinx.coroutines.runBlocking
import org.osmdroid.config.Configuration

private const val TAG = "MainActivity"
Expand Down Expand Up @@ -209,7 +208,6 @@ fun PeriodPalsApp(
val dbDriverFactory = rememberDatabaseDriverFactory()
val db = remember { PowerSyncDatabase(dbDriverFactory, schema = localSchema) }
val supabaseConnector = remember { SupabaseConnector(supabase, BuildConfig.POWERSYNC_URL) }
runBlocking { db.connect(supabaseConnector) }

val userModelPowerSync = remember { UserModelPowerSync(db, supabaseConnector, supabase) }
val userViewModelPowerSync = remember { UserViewModel(userModelPowerSync) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ private const val TAG = "UserModelPowerSync"
private const val USERS = "users"

class UserModelPowerSync(
private val db: PowerSyncDatabase,
private val connector: SupabaseConnector,
private val supabase: SupabaseClient
private val db: PowerSyncDatabase,
private val connector: SupabaseConnector,
private val supabase: SupabaseClient
) : UserRepository {

private suspend fun sync() {
Expand All @@ -35,24 +35,27 @@ class UserModelPowerSync(
}

override suspend fun loadUserProfile(
idUser: String,
onSuccess: (UserDto) -> Unit,
onFailure: (Exception) -> Unit
idUser: String,
onSuccess: (UserDto) -> Unit,
onFailure: (Exception) -> Unit
) {
try {
db.connect(connector)
sync()
val user: UserDto =
db.get(
"SELECT name, imageUrl, description, dob, preferred_distance, fcm_token, locationGIS FROM $USERS WHERE user_id = ?",
listOf(idUser)) {
UserDto(
name = it.getString(0)!!,
imageUrl = it.getString(1)!!,
description = it.getString(2)!!,
dob = it.getString(3)!!,
preferred_distance = it.getLong(4)!!.toInt(),
fcm_token = it.getString(5))
}
db.get(
"SELECT name, imageUrl, description, dob, preferred_distance, fcm_token FROM $USERS WHERE user_id = ?",
listOf(idUser)
) {
UserDto(
name = it.getString(0)!!,
imageUrl = it.getString(1)!!,
description = it.getString(2)!!,
dob = it.getString(3)!!,
preferred_distance = it.getLong(4)!!.toInt(),
fcm_token = it.getString(5)
)
}

Log.d(TAG, "loadUserProfile: Success")
onSuccess(user)
Expand All @@ -63,23 +66,26 @@ class UserModelPowerSync(
}

override suspend fun loadUserProfiles(
onSuccess: (List<UserDto>) -> Unit,
onFailure: (Exception) -> Unit
onSuccess: (List<UserDto>) -> Unit,
onFailure: (Exception) -> Unit
) {
try {
db.connect(connector)
sync()
val users: List<UserDto> =
db.getAll(
"SELECT name, imageUrl, description, dob, preferred_distance, fcm_token, locationGIS FROM $USERS",
listOf()) {
UserDto(
name = it.getString(0)!!,
imageUrl = it.getString(1)!!,
description = it.getString(2)!!,
dob = it.getString(3)!!,
preferred_distance = it.getLong(4)!!.toInt(),
fcm_token = it.getString(5))
}
db.getAll(
"SELECT name, imageUrl, description, dob, preferred_distance, fcm_token, locationGIS FROM $USERS",
listOf()
) {
UserDto(
name = it.getString(0)!!,
imageUrl = it.getString(1)!!,
description = it.getString(2)!!,
dob = it.getString(3)!!,
preferred_distance = it.getLong(4)!!.toInt(),
fcm_token = it.getString(5)
)
}

Log.d(TAG, "loadUserProfiles: Success")
onSuccess(users)
Expand All @@ -90,15 +96,17 @@ class UserModelPowerSync(
}

override suspend fun createUserProfile(
user: User,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit
user: User,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit
) {
try {
db.connect(connector)
db.writeTransaction { tx ->
tx.execute(
"INSERT INTO $USERS (name, imageUrl, description, dob, preferred_distance, fcm_token) VALUES (?, ?, ?, ?, ?, ?);",
user.asList())
"INSERT INTO $USERS (name, imageUrl, description, dob, preferred_distance, fcm_token) VALUES (?, ?, ?, ?, ?, ?);",
user.asList()
)
}
Log.d(TAG, "createUserProfile: Success")
onSuccess()
Expand All @@ -110,35 +118,42 @@ class UserModelPowerSync(
}

override suspend fun upsertUserProfile(
userDto: UserDto,
onSuccess: (UserDto) -> Unit,
onFailure: (Exception) -> Unit
userDto: UserDto,
onSuccess: (UserDto) -> Unit,
onFailure: (Exception) -> Unit
) {
try {
db.connect(connector)
val currUser: String? = supabase.auth.currentUserOrNull()?.id

Log.d(TAG, "b4")
sync()
db.writeTransaction { tx ->
tx.execute(
"""
INSERT INTO $USERS (user_id, name, imageUrl, description, dob, preferred_distance, fcm_token)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (user_id)
DO UPDATE SET name = ?, imageUrl = ?, description = ?, dob = ?, preferred_distance = ?, fcm_token = ?;
"""
INSERT INTO $USERS(user_id, name, imageUrl, description, dob, preferred_distance, fcm_token)
VALUES(?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(user_id)
DO
UPDATE SET name = ?, imageUrl = ?, description = ?, dob = ?, preferred_distance = ?, fcm_token = ?
WHERE user_id = ?;
""",
listOf(
currUser,
userDto.name,
userDto.imageUrl,
userDto.description,
userDto.dob,
userDto.preferred_distance,
userDto.fcm_token,
userDto.name,
userDto.imageUrl,
userDto.description,
userDto.dob,
userDto.preferred_distance,
userDto.fcm_token))
listOf(
currUser,
userDto.name,
userDto.imageUrl,
userDto.description,
userDto.dob,
userDto.preferred_distance,
userDto.fcm_token,
userDto.name,
userDto.imageUrl,
userDto.description,
userDto.dob,
userDto.preferred_distance,
userDto.fcm_token,
currUser
)
)
}
Log.d(TAG, "upsertUserProfile: Success")
sync()
Expand All @@ -150,14 +165,14 @@ class UserModelPowerSync(
}

override suspend fun deleteUserProfile(
idUser: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit
idUser: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit
) {
try {
val currUser =
supabase.auth.currentUserOrNull()?.id
?: throw Exception("Supabase does not have a user logged in")
supabase.auth.currentUserOrNull()?.id
?: throw Exception("Supabase does not have a user logged in")

db.writeTransaction { tx ->
tx.execute("DELETE FROM $USERS WHERE user_id = ?", listOf(currUser))
Expand All @@ -172,10 +187,10 @@ class UserModelPowerSync(
}

override suspend fun uploadFile(
filePath: String,
bytes: ByteArray,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit,
filePath: String,
bytes: ByteArray,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit,
) {
try {
withContext(Dispatchers.Main) {
Expand All @@ -190,9 +205,9 @@ class UserModelPowerSync(
}

override suspend fun downloadFile(
filePath: String,
onSuccess: (bytes: ByteArray) -> Unit,
onFailure: (Exception) -> Unit,
filePath: String,
onSuccess: (bytes: ByteArray) -> Unit,
onFailure: (Exception) -> Unit,
) {
try {
withContext(Dispatchers.Main) {
Expand Down
Loading

0 comments on commit a87cd97

Please sign in to comment.