Skip to content

Commit

Permalink
feat: can see the current association Logo
Browse files Browse the repository at this point in the history
  • Loading branch information
zizouet committed Jun 2, 2024
1 parent 3a37a5f commit e4331b6
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 10 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/com/github/se/assocify/model/CurrentUser.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package com.github.se.assocify.model

import android.net.Uri
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

object CurrentUser {
var userUid: String? = null
var associationUid: String? = null
private var _associationLogo : StateFlow<Uri?> = MutableStateFlow(null)
val associationLogo: StateFlow<Uri?> = _associationLogo
fun setAssociationLogo(uri: Uri?) {
(_associationLogo as MutableStateFlow).value = uri
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.se.assocify.model.database

import android.net.Uri
import android.util.Log
import com.github.se.assocify.model.CurrentUser
import com.github.se.assocify.model.entities.Association
import com.github.se.assocify.model.entities.AssociationMember
import com.github.se.assocify.model.entities.PermissionRole
Expand All @@ -25,9 +27,11 @@ import kotlinx.serialization.json.JsonObject
class AssociationAPI(private val db: SupabaseClient, cachePath: Path) : SupabaseApi() {
private var associationCache = mapOf<String, Association>()
private val imageCacher = ImageCacher(60 * 60_000, cachePath, db.storage["association"])
private var currentAssociationCache : String? = null

init {
updateCache({}, {}) // Try and fill the cache as quickly as possible
currentAssociationCache = CurrentUser.associationUid
}

/**
Expand All @@ -41,6 +45,7 @@ class AssociationAPI(private val db: SupabaseClient, cachePath: Path) : Supabase
val assoc = db.from("association").select().decodeList<SupabaseAssociation>()
associationCache = assoc.associateBy { it.uid!! }.mapValues { it.value.toAssociation() }
memberCache = null
currentAssociationCache = CurrentUser.associationUid
onSuccess(associationCache)
}
}
Expand Down Expand Up @@ -438,7 +443,11 @@ class AssociationAPI(private val db: SupabaseClient, cachePath: Path) : Supabase
* @param onFailure called on failure
*/
fun getLogo(associationId: String, onSuccess: (Uri) -> Unit, onFailure: (Exception) -> Unit) {
imageCacher.fetchImage(associationId, { onSuccess(Uri.fromFile(it.toFile())) }, onFailure)
Log.d("image", "getLogo from association $associationId")
if (associationId != currentAssociationCache) {
currentAssociationCache = associationId
imageCacher.fetchImage(associationId, {onSuccess(Uri.fromFile(it.toFile())) }, onFailure)
}
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class ImageCacher(val timeout: Long, val cacheDir: Path, private val bucket: Buc
if (!renamed) {
Log.w("IMG", "Failed to rename temporary image cache file ($pathInBucket)")
}

onSuccess(imageFile)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ fun NavGraphBuilder.mainNavGraph(
receiptsAPI,
accountingCategoriesAPI,
accountingSubCategoryAPI,
userAPI)
userAPI,
associationAPI)
eventGraph(navActions, eventAPI, taskAPI)
profileGraph(navActions, userAPI, associationAPI, accountingCategoriesAPI, eventAPI)
loginGraph(navActions, userAPI)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
package com.github.se.assocify.ui.composables

import android.util.Log
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.outlined.AccountCircle
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.SearchBar
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.github.se.assocify.model.CurrentUser

/**
* Main tab top bar
Expand Down Expand Up @@ -49,6 +60,9 @@ fun MainTopBar(
// Page state
var currentPage by remember { mutableIntStateOf(page) }

val associationLogoUri = CurrentUser.associationLogo.collectAsState()
val associationLogoUriValue = associationLogoUri.value

if (currentPage != page) {
currentPage = page
searchBarVisible = false
Expand All @@ -62,9 +76,25 @@ fun MainTopBar(
IconButton(
modifier = Modifier.testTag("accountIconButton"),
onClick = { /*TODO On assoc account click */}) {
Icon(
imageVector = Icons.Filled.AccountCircle,
contentDescription = "Association Account")
// profile picture
if (associationLogoUriValue != null) {
Log.d("image", "CurrentUser.associationLogo: ${associationLogoUriValue}")
AsyncImage(
modifier =
Modifier.size(80.dp)
.clip(CircleShape) // Clip the image to a circle shape
.aspectRatio(1f)
.testTag("profilePicture"),
model = associationLogoUriValue,
contentDescription = "profile picture",
contentScale = ContentScale.Crop)
} else {
Log.d("image", "CurrentUser.associationLogo: ${associationLogoUriValue}")
Icon(
modifier = Modifier.fillMaxSize(),
imageVector = Icons.Outlined.AccountCircle,
contentDescription = "default profile icon")
}
}
},
actions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ class ProfileViewModel(
}
val oldAssociationUid = CurrentUser.associationUid
CurrentUser.associationUid = association.uid
assoAPI.getLogo(
CurrentUser.associationUid!!,
{ uri -> CurrentUser.setAssociationLogo(uri) },
{ CurrentUser.setAssociationLogo(null) })
userAPI.getCurrentUserRole(
{ role ->
_uiState.value = _uiState.value.copy(selectedAssociation = association)
Expand All @@ -200,6 +204,7 @@ class ProfileViewModel(
CurrentUser.associationUid = oldAssociationUid
snackbarSystem.showSnackbar("Couldn't switch association")
})

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import com.github.se.assocify.model.database.AccountingCategoryAPI
import com.github.se.assocify.model.database.AccountingSubCategoryAPI
import com.github.se.assocify.model.database.AssociationAPI
import com.github.se.assocify.model.database.BalanceAPI
import com.github.se.assocify.model.database.BudgetAPI
import com.github.se.assocify.model.database.ReceiptAPI
Expand All @@ -22,7 +23,8 @@ fun NavGraphBuilder.treasuryGraph(
receiptsAPI: ReceiptAPI,
accountingCategoryAPI: AccountingCategoryAPI,
accountingSubCategoryAPI: AccountingSubCategoryAPI,
userAPI: UserAPI
userAPI: UserAPI,
associationAPI: AssociationAPI
) {

composable(
Expand All @@ -36,7 +38,9 @@ fun NavGraphBuilder.treasuryGraph(
accountingSubCategoryAPI,
balanceAPI,
budgetAPI,
userAPI)
userAPI,
associationAPI
)
}
TreasuryScreen(navigationActions, treasuryViewModel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import com.github.se.assocify.model.CurrentUser
import com.github.se.assocify.model.entities.RoleType
import com.github.se.assocify.navigation.Destination
import com.github.se.assocify.navigation.MAIN_TABS_LIST
Expand Down Expand Up @@ -99,7 +100,9 @@ fun TreasuryScreen(navActions: NavigationActions, treasuryViewModel: TreasuryVie
snackbar = { snackbarData -> Snackbar(snackbarData = snackbarData) })
},
contentWindowInsets = WindowInsets(20.dp, 0.dp, 20.dp, 0.dp)) { innerPadding ->
Column(modifier = Modifier.padding(innerPadding).fillMaxSize()) {
Column(modifier = Modifier
.padding(innerPadding)
.fillMaxSize()) {
if (receiptState.userCurrentRole.type != RoleType.TREASURY &&
receiptState.userCurrentRole.type != RoleType.PRESIDENCY) {
ReceiptListScreen(viewModel = receiptListViewModel)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.github.se.assocify.ui.screens.treasury

import android.net.Uri
import android.util.Log
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.collectAsState
import com.github.se.assocify.model.CurrentUser
import com.github.se.assocify.model.database.AccountingCategoryAPI
import com.github.se.assocify.model.database.AccountingSubCategoryAPI
import com.github.se.assocify.model.database.AssociationAPI
import com.github.se.assocify.model.database.BalanceAPI
import com.github.se.assocify.model.database.BudgetAPI
import com.github.se.assocify.model.database.ReceiptAPI
Expand All @@ -21,8 +26,18 @@ class TreasuryViewModel(
accountingSubCategoryAPI: AccountingSubCategoryAPI,
balanceAPI: BalanceAPI,
budgetAPI: BudgetAPI,
userAPI: UserAPI
userAPI: UserAPI,
associationAPI: AssociationAPI
) {
init {
Log.d("image", "getting logo in View model at init")
associationAPI.getLogo(CurrentUser.associationUid!!,
{uri ->
Log.d("image", "uri is $uri")
CurrentUser.setAssociationLogo(uri) },
{CurrentUser.setAssociationLogo(null)}
)
}
// ViewModel states
private val _uiState: MutableStateFlow<TreasuryUIState> = MutableStateFlow(TreasuryUIState())
val uiState: StateFlow<TreasuryUIState> = _uiState
Expand Down Expand Up @@ -72,7 +87,7 @@ class TreasuryViewModel(
data class TreasuryUIState(
val snackbarHostState: SnackbarHostState = SnackbarHostState(),
val searchQuery: String = "",
val currentTab: TreasuryPageIndex = TreasuryPageIndex.Receipts
val currentTab: TreasuryPageIndex = TreasuryPageIndex.Receipts,
)

/** Treasury tabs */
Expand Down

0 comments on commit e4331b6

Please sign in to comment.