Skip to content

Commit

Permalink
🚸 Fix unfollowed private profiles (#430)
Browse files Browse the repository at this point in the history
* try getting user info from search api if user api returns 403

* 🚸 Fix unfollowed private profiles

---------

Co-authored-by: noantiq <>
Co-authored-by: Jonas Heubuch <jonas.heubuch@outlook.com>
  • Loading branch information
noantiq and jheubuch authored Oct 25, 2024
1 parent b22cb64 commit b860e7a
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ fun List<String>.toNavDeepLinks(): List<NavDeepLink> {

@Serializable
data class PersonalProfile(
val username: String? = null
val username: String? = null,
val isPrivateProfile: Boolean = false,
val isFollowing: Boolean = false
) : MainDestination {
override val icon = R.drawable.ic_account
override val label = R.string.title_user
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/kotlin/de/hbch/traewelling/navigation/NavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ fun TraewelldroidNavHost(
StatusDetails(statusId)
)
}
val navToUserProfile: (String) -> Unit = { username ->
val navToUserProfile: (String, Boolean, Boolean) -> Unit = { username, isPrivateProfile, isFollowing ->
navController.navigate(
PersonalProfile(username)
PersonalProfile(username, isPrivateProfile, isFollowing)
)
}

Expand Down Expand Up @@ -243,6 +243,8 @@ fun TraewelldroidNavHost(

Profile(
username = profile.username,
isPrivateProfile = profile.isPrivateProfile,
isFollowing = profile.isFollowing,
loggedInUserViewModel = loggedInUserViewModel,
stationSelectedAction = navToSearchConnections,
statusSelectedAction = navToStatusDetails,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import de.hbch.traewelling.util.checkInList
fun EnRoute(
loggedInUserViewModel: LoggedInUserViewModel,
joinConnection: (Status) -> Unit,
userSelectedAction: (String) -> Unit = { },
userSelectedAction: (String, Boolean, Boolean) -> Unit = { _, _, _ -> },
statusSelectedAction: (Int) -> Unit = { },
statusDeletedAction: () -> Unit = { },
statusEditAction: (Status) -> Unit = { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fun Dashboard(
loggedInUserViewModel: LoggedInUserViewModel,
joinConnection: (Status) -> Unit,
searchConnectionsAction: (Int, ZonedDateTime?) -> Unit = { _, _ -> },
userSelectedAction: (String) -> Unit = { },
userSelectedAction: (String, Boolean, Boolean) -> Unit = { _, _, _ -> },
statusSelectedAction: (Int) -> Unit = { },
statusDeletedAction: () -> Unit = { },
statusEditAction: (Status) -> Unit = { }
Expand Down Expand Up @@ -87,7 +87,7 @@ fun Dashboard(
homelandStationData = loggedInUserViewModel.home,
recentStationsData = loggedInUserViewModel.lastVisitedStations,
onUserSelected = {
userSelectedAction(it.username)
userSelectedAction(it.username, it.privateProfile, it.following)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fun CheckInCard(
loggedInUserViewModel: LoggedInUserViewModel? = null,
displayLongDate: Boolean = false,
stationSelected: (Int, ZonedDateTime?) -> Unit = { _, _ -> },
userSelected: (String) -> Unit = { },
userSelected: (String, Boolean, Boolean) -> Unit = { _, _, _ -> },
statusSelected: (Int) -> Unit = { },
handleEditClicked: (Status) -> Unit = { },
onDeleted: (Status) -> Unit = { }
Expand Down Expand Up @@ -359,7 +359,7 @@ fun CheckInCardContent(
message: Pair<AnnotatedString?, Map<String, InlineTextContent>>,
operatorCode: String? = null,
lineId: String? = null,
userSelected: (String) -> Unit = { },
userSelected: (String, Boolean, Boolean) -> Unit = { _, _, _ -> },
textClicked: () -> Unit = { }
) {
Column(
Expand Down Expand Up @@ -390,7 +390,7 @@ fun CheckInCardContent(
onClick = {
val annotations = message.first!!.getStringAnnotations(it - 1, it + 1)
if (annotations.isNotEmpty()) {
userSelected(annotations.first().item)
userSelected(annotations.first().item, false, false)
} else {
textClicked()
}
Expand Down Expand Up @@ -460,7 +460,7 @@ private fun CheckInCardFooter(
isOwnStatus: Boolean = false,
displayLongDate: Boolean = false,
defaultVisibility: StatusVisibility = StatusVisibility.PUBLIC,
userSelected: (String) -> Unit = { },
userSelected: (String, Boolean, Boolean) -> Unit = { _, _, _ -> },
handleEditClicked: () -> Unit = { },
handleDeleteClicked: () -> Unit = { }
) {
Expand Down Expand Up @@ -582,7 +582,7 @@ private fun CheckInCardFooter(
)
Text(
modifier = alignmentModifier
.clickable { userSelected(status.user.username) }
.clickable { userSelected(status.user.username, false, false) }
.padding(2.dp),
text = stringResource(
id = R.string.check_in_user_time,
Expand All @@ -599,7 +599,6 @@ private fun CheckInCardFooter(
)
}
var menuExpanded by remember { mutableStateOf(false) }
val context = LocalContext.current
Box {
Icon(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fun StatusDetail(
statusDeleted: (Status) -> Unit = { },
statusEdit: (Status) -> Unit = { },
loggedInUserViewModel: LoggedInUserViewModel? = null,
userSelected: (String) -> Unit = { }
userSelected: (String, Boolean, Boolean) -> Unit = { _, _, _ -> }
) {
val statusDetailViewModel: StatusDetailViewModel = viewModel()
val checkInCardViewModel: CheckInCardViewModel = viewModel()
Expand Down Expand Up @@ -296,7 +296,7 @@ private fun StatusLikes(
likes: Int,
statusDetailViewModel: StatusDetailViewModel,
modifier: Modifier = Modifier,
userSelected: (String) -> Unit = { }
userSelected: (String, Boolean, Boolean) -> Unit = { _, _, _ -> }
) {
var cardExpanded by remember { mutableStateOf(false) }
var isLoading by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -381,13 +381,13 @@ private fun StatusLikes(
private fun Liker(
user: User,
modifier: Modifier = Modifier,
userSelected: (String) -> Unit = { }
userSelected: (String, Boolean, Boolean) -> Unit = { _, _, _ -> }
) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
modifier = modifier
.clickable { userSelected(user.username) }
.clickable { userSelected(user.username, user.privateProfile, user.following) }
.padding(horizontal = 8.dp, vertical = 4.dp)
) {
Row(
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/kotlin/de/hbch/traewelling/ui/user/Profile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -35,17 +37,19 @@ fun Profile(
username: String?,
loggedInUserViewModel: LoggedInUserViewModel,
joinConnection: (Status) -> Unit,
isPrivateProfile: Boolean = false,
isFollowing: Boolean = false,
stationSelectedAction: (Int, ZonedDateTime?) -> Unit = { _, _ -> },
statusSelectedAction: (Int) -> Unit = { },
statusDeletedAction: () -> Unit = { },
statusEditAction: (Status) -> Unit = { },
dailyStatisticsSelectedAction: (LocalDate) -> Unit = { },
userSelectedAction: (String) -> Unit = { },
userSelectedAction: (String, Boolean, Boolean) -> Unit = { _, _, _ -> },
editProfile: () -> Unit = { },
manageFollowerAction: () -> Unit = { }
) {
val user = username ?: loggedInUserViewModel.loggedInUser.value?.username
var currentPage by remember { mutableStateOf(1) }
var currentPage by rememberSaveable { mutableIntStateOf(1) }
val userStatusViewModel: UserStatusViewModel = viewModel()
val checkInCardViewModel: CheckInCardViewModel = viewModel()

Expand All @@ -57,7 +61,7 @@ fun Profile(
refreshing = refreshing,
onRefresh = {
currentPage = 1
userStatusViewModel.loadUser(user)
userStatusViewModel.loadUser(user, (isPrivateProfile && !isFollowing))
}
)
val listState = rememberLazyListState()
Expand All @@ -71,7 +75,7 @@ fun Profile(

LaunchedEffect(Unit) {
if (!initialized) {
userStatusViewModel.loadUser(user)
userStatusViewModel.loadUser(user, isPrivateProfile)
initialized = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package de.hbch.traewelling.ui.user
import androidx.compose.runtime.mutableStateListOf
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import de.hbch.traewelling.api.TraewellingApi
import de.hbch.traewelling.api.models.Data
import de.hbch.traewelling.api.models.status.Status
import de.hbch.traewelling.api.models.status.StatusPage
import de.hbch.traewelling.api.models.user.User
import de.hbch.traewelling.logging.Logger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
Expand All @@ -19,31 +23,58 @@ class UserStatusViewModel : ViewModel() {
val user = MutableLiveData<User?>(null)
var isRefreshing = MutableLiveData(false)

fun loadUser(username: String?) {
fun loadUser(
username: String?,
searchRequired: Boolean = false
) {
if (username != null) {
isRefreshing.postValue(true)
TraewellingApi.userService.getUser(username)
.enqueue(object : Callback<Data<User>> {
override fun onResponse(
call: Call<Data<User>>,
response: Response<Data<User>>
) {
isRefreshing.postValue(false)
if (response.isSuccessful) {
val respUser = response.body()
if (respUser != null) {
user.postValue(respUser.data)
resetStatusesForUser(respUser.data.username)
if (searchRequired) {
searchForUser(username)
} else {
TraewellingApi.userService.getUser(username)
.enqueue(object : Callback<Data<User>> {
override fun onResponse(
call: Call<Data<User>>,
response: Response<Data<User>>
) {
isRefreshing.postValue(false)
if (response.isSuccessful) {
val respUser = response.body()
if (respUser != null) {
user.postValue(respUser.data)
resetStatusesForUser(respUser.data.username)
}
return
} else if (response.code() == 403) {
searchForUser(username)
}
return
}
}

override fun onFailure(call: Call<Data<User>>, t: Throwable) {
isRefreshing.postValue(false)
Logger.captureException(t)
override fun onFailure(call: Call<Data<User>>, t: Throwable) {
isRefreshing.postValue(false)
Logger.captureException(t)
}
})
}
}
}

fun searchForUser(username: String) {
viewModelScope.launch {
withContext(Dispatchers.Main.immediate) {
try {
val respUsers =
TraewellingApi.userService.searchUsers(username).data
if (respUsers.isNotEmpty() && respUsers[0].username == username) {
user.postValue(respUsers[0])
}
})
} catch (e: Exception) {
Logger.captureException(e)
} finally {
isRefreshing.postValue(false)
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/de/hbch/traewelling/util/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fun LazyListScope.checkInList(
statusSelectedAction: (Int) -> Unit = { },
statusEditAction: (Status) -> Unit = { },
statusDeletedAction: () -> Unit = { },
userSelectedAction: (String) -> Unit = { },
userSelectedAction: (String, Boolean, Boolean) -> Unit = { _, _, _ -> },
showDailyStatisticsLink: Boolean = false,
dailyStatisticsSelectedAction: (LocalDate) -> Unit = { },
showDate: Boolean = true
Expand Down

0 comments on commit b860e7a

Please sign in to comment.