-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from akiomik/followers-list
Followers list
- Loading branch information
Showing
11 changed files
with
274 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
app/src/main/java/io/github/akiomik/seiun/model/app/bsky/graph/Followers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.github.akiomik.seiun.model.app.bsky.graph | ||
|
||
import com.squareup.moshi.JsonClass | ||
import io.github.akiomik.seiun.model.app.bsky.actor.RefWithInfo | ||
|
||
@JsonClass(generateAdapter = true) | ||
data class Followers( | ||
val subject: RefWithInfo, | ||
val followers: List<RefWithInfo>, | ||
val cursor: String? = null | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
app/src/main/java/io/github/akiomik/seiun/ui/user/FollowersListModal.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package io.github.akiomik.seiun.ui.user | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.foundation.shape.CircleShape | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.Divider | ||
import androidx.compose.material3.ListItem | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import coil.compose.AsyncImage | ||
import io.github.akiomik.seiun.R | ||
import io.github.akiomik.seiun.model.app.bsky.actor.RefWithInfo | ||
import io.github.akiomik.seiun.ui.dialog.FullScreenDialog | ||
import io.github.akiomik.seiun.viewmodels.FollowersViewModel | ||
|
||
@Composable | ||
private fun Avatar(user: RefWithInfo, onClicked: (String) -> Unit) { | ||
AsyncImage( | ||
model = user.avatar, | ||
contentDescription = null, | ||
modifier = Modifier | ||
.width(56.dp) | ||
.height(56.dp) | ||
.clip(CircleShape) | ||
.clickable { onClicked(user.did) } | ||
) | ||
} | ||
|
||
@Composable | ||
private fun FollowersListItem(user: RefWithInfo, onProfileClick: (String) -> Unit) { | ||
ListItem( | ||
leadingContent = { Avatar(user = user, onClicked = onProfileClick) }, | ||
headlineContent = { Text(text = user.displayName ?: "@${user.handle}") }, | ||
supportingContent = { | ||
Text( | ||
text = "@${user.handle}", | ||
color = Color.Gray, | ||
style = MaterialTheme.typography.labelMedium | ||
) | ||
}, | ||
trailingContent = { | ||
// TODO | ||
// if (user.viewer?.following == null) { | ||
// Button(onClick = { /*TODO*/ }) { | ||
// Text(stringResource(R.string.follow)) | ||
// } | ||
// } else { | ||
// Button(onClick = { /*TODO*/ }) { | ||
// Text(stringResource(R.string.unfollow)) | ||
// } | ||
// } | ||
} | ||
) | ||
} | ||
|
||
@Composable | ||
private fun FollowersListContent( | ||
followers: List<RefWithInfo>, | ||
viewModel: FollowersViewModel, | ||
onProfileClick: (String) -> Unit | ||
) { | ||
val seenAllFollowers by viewModel.seenAllFollowers.collectAsState() | ||
|
||
LazyColumn { | ||
items(followers) { | ||
FollowersListItem(user = it, onProfileClick = onProfileClick) | ||
Divider(color = Color.Gray) | ||
} | ||
|
||
if (followers.isEmpty()) { | ||
item { Text(stringResource(R.string.followers_no_followers_yet)) } | ||
} else if (!seenAllFollowers) { | ||
item { FollowersLoadingIndicator(viewModel) } | ||
} | ||
} | ||
} | ||
|
||
// TODO: Implement as Screen | ||
@Composable | ||
fun FollowersListModal( | ||
viewModel: FollowersViewModel, | ||
onClose: () -> Unit, | ||
onProfileClick: (String) -> Unit | ||
) { | ||
val state by viewModel.state.collectAsState() | ||
val followers by viewModel.followers.collectAsState() | ||
|
||
FullScreenDialog(onClose = onClose, actions = {}) { | ||
when (state) { | ||
is FollowersViewModel.State.Loading -> { | ||
CircularProgressIndicator() | ||
} | ||
is FollowersViewModel.State.Loaded -> { | ||
FollowersListContent(followers, viewModel, onProfileClick) | ||
} | ||
is FollowersViewModel.State.Error -> { | ||
Text((state as FollowersViewModel.State.Error).error.toString()) | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/io/github/akiomik/seiun/ui/user/FollowersLoadingIndicator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.github.akiomik.seiun.ui.user | ||
|
||
import android.widget.Toast | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.unit.dp | ||
import io.github.akiomik.seiun.viewmodels.FollowersViewModel | ||
|
||
@Composable | ||
fun FollowersLoadingIndicator(viewModel: FollowersViewModel) { | ||
val context = LocalContext.current | ||
|
||
Box( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(8.dp), | ||
contentAlignment = Alignment.Center | ||
) { | ||
CircularProgressIndicator() | ||
} | ||
|
||
LaunchedEffect(key1 = true) { | ||
viewModel.loadMoreFollowers(onError = { | ||
Toast.makeText(context, it.toString(), Toast.LENGTH_LONG).show() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/src/main/java/io/github/akiomik/seiun/viewmodels/FollowersViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.github.akiomik.seiun.viewmodels | ||
|
||
import android.util.Log | ||
import io.github.akiomik.seiun.SeiunApplication | ||
import io.github.akiomik.seiun.model.app.bsky.actor.RefWithInfo | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
|
||
class FollowersViewModel(val did: String) : ApplicationViewModel() { | ||
sealed class State { | ||
object Loading : State() | ||
object Loaded : State() | ||
data class Error(val error: Throwable) : State() | ||
} | ||
|
||
private val userRepository = SeiunApplication.instance!!.userRepository | ||
private var _cursor: String? = null | ||
|
||
private val _seenAllFollows = MutableStateFlow(false) | ||
private val _state = MutableStateFlow<State>(State.Loading) | ||
private val _followers = MutableStateFlow<List<RefWithInfo>>(emptyList()) | ||
val seenAllFollowers = _seenAllFollows.asStateFlow() | ||
val state = _state.asStateFlow() | ||
val followers = _followers.asStateFlow() | ||
|
||
init { | ||
wrapError( | ||
run = { userRepository.getFollowers(did) }, | ||
onSuccess = { | ||
Log.d(SeiunApplication.TAG, it.toString()) | ||
_followers.value = it.followers | ||
_cursor = it.cursor | ||
_state.value = State.Loaded | ||
}, | ||
onError = { | ||
Log.d(SeiunApplication.TAG, it.toString()) | ||
_state.value = State.Error(it) | ||
} | ||
) | ||
} | ||
|
||
fun loadMoreFollowers(onError: (Throwable) -> Unit = {}) { | ||
wrapError(run = { | ||
val data = userRepository.getFollowers(did = did, before = _cursor) | ||
|
||
if (data.cursor != _cursor) { | ||
if (data.followers.isNotEmpty()) { | ||
_followers.value = _followers.value + data.followers | ||
_cursor = data.cursor | ||
Log.d(SeiunApplication.TAG, "Followers are updated") | ||
} else { | ||
Log.d(SeiunApplication.TAG, "No followers") | ||
_seenAllFollows.value = true | ||
} | ||
} | ||
}, onError = onError) | ||
} | ||
} |
Oops, something went wrong.