-
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.
- Loading branch information
Showing
10 changed files
with
259 additions
and
13 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
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/FollowsListModal.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.FollowsViewModel | ||
|
||
@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 FollowsListItem(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 FollowsListContent( | ||
follows: List<RefWithInfo>, | ||
viewModel: FollowsViewModel, | ||
onProfileClick: (String) -> Unit | ||
) { | ||
val seenAllFollows by viewModel.seenAllFollows.collectAsState() | ||
|
||
LazyColumn { | ||
items(follows) { | ||
FollowsListItem(user = it, onProfileClick = onProfileClick) | ||
Divider(color = Color.Gray) | ||
} | ||
|
||
if (follows.isEmpty()) { | ||
item { Text(stringResource(R.string.follows_no_follows_yet)) } | ||
} else if (!seenAllFollows) { | ||
item { FollowsLoadingIndicator(viewModel) } | ||
} | ||
} | ||
} | ||
|
||
// TODO: Implement as Screen | ||
@Composable | ||
fun FollowsListModal( | ||
viewModel: FollowsViewModel, | ||
onClose: () -> Unit, | ||
onProfileClick: (String) -> Unit | ||
) { | ||
val state by viewModel.state.collectAsState() | ||
val follows by viewModel.follows.collectAsState() | ||
|
||
FullScreenDialog(onClose = onClose, actions = {}) { | ||
when (state) { | ||
is FollowsViewModel.State.Loading -> { | ||
CircularProgressIndicator() | ||
} | ||
is FollowsViewModel.State.Loaded -> { | ||
FollowsListContent(follows, viewModel, onProfileClick) | ||
} | ||
is FollowsViewModel.State.Error -> { | ||
Text((state as FollowsViewModel.State.Error).error.toString()) | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/io/github/akiomik/seiun/ui/user/FollowsLoadingIndicator.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,35 @@ | ||
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 androidx.lifecycle.viewmodel.compose.viewModel | ||
import io.github.akiomik.seiun.viewmodels.FollowsViewModel | ||
|
||
@Composable | ||
fun FollowsLoadingIndicator(viewModel: FollowsViewModel) { | ||
val context = LocalContext.current | ||
|
||
Box( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(8.dp), | ||
contentAlignment = Alignment.Center | ||
) { | ||
CircularProgressIndicator() | ||
} | ||
|
||
LaunchedEffect(key1 = true) { | ||
viewModel.loadMoreFollows(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
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/FollowsViewModel.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 FollowsViewModel(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 _follows = MutableStateFlow<List<RefWithInfo>>(emptyList()) | ||
val seenAllFollows = _seenAllFollows.asStateFlow() | ||
val state = _state.asStateFlow() | ||
val follows = _follows.asStateFlow() | ||
|
||
init { | ||
wrapError( | ||
run = { userRepository.getFollows(did) }, | ||
onSuccess = { | ||
Log.d(SeiunApplication.TAG, it.toString()) | ||
_follows.value = it.follows | ||
_cursor = it.cursor | ||
_state.value = State.Loaded | ||
}, | ||
onError = { | ||
Log.d(SeiunApplication.TAG, it.toString()) | ||
_state.value = State.Error(it) | ||
} | ||
) | ||
} | ||
|
||
fun loadMoreFollows(onError: (Throwable) -> Unit = {}) { | ||
wrapError(run = { | ||
val data = userRepository.getFollows(did = did, before = _cursor) | ||
|
||
if (data.cursor != _cursor) { | ||
if (data.follows.isNotEmpty()) { | ||
_follows.value = _follows.value + data.follows | ||
_cursor = data.cursor | ||
Log.d(SeiunApplication.TAG, "Feed posts are updated") | ||
} else { | ||
Log.d(SeiunApplication.TAG, "No new feed posts") | ||
_seenAllFollows.value = true | ||
} | ||
} | ||
}, onError = onError) | ||
} | ||
} |
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