1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 05:16:58 +00:00

Update ProfileFragmentViewModel

This commit is contained in:
Ammar Githam 2021-06-23 22:07:38 +09:00
parent 1ebf7a2e4b
commit 976c9a86b6
3 changed files with 80 additions and 15 deletions

View File

@ -41,7 +41,7 @@ class ProfileFragmentViewModel(
value = currentUser to stateUsername
}
addSource(state.getLiveData<String?>("username")) { username ->
this.stateUsername = Resource.success(username)
this.stateUsername = Resource.success(username.substringAfter('@'))
value = user to this.stateUsername
}
// trigger currentUserAndStateUsernameLiveData switch map with a state username success resource
@ -87,6 +87,7 @@ class ProfileFragmentViewModel(
Resource.Status.SUCCESS -> it.data?.username ?: ""
}
}
init {
// Log.d(TAG, "${state.keys()} $userRepository $friendshipRepository $storiesRepository $mediaRepository")
}

View File

@ -7,14 +7,14 @@ import awais.instagrabber.repositories.responses.UserSearchResponse
import awais.instagrabber.webservices.RetrofitFactory.retrofit
import java.util.*
class UserRepository(private val service: UserService) {
open class UserRepository(private val service: UserService) {
suspend fun getUserInfo(uid: Long): User {
val response = service.getUserInfo(uid)
return response.user
}
suspend fun getUsernameInfo(username: String): User {
open suspend fun getUsernameInfo(username: String): User {
val response = service.getUsernameInfo(username)
return response.user
}

View File

@ -23,12 +23,6 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
internal class ProfileFragmentViewModelTest {
private val testPublicUser = User(
pk = 100,
username = "test",
fullName = "Test user"
)
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
@ -36,9 +30,15 @@ internal class ProfileFragmentViewModelTest {
@get:Rule
val coroutineScope = MainCoroutineScopeRule()
private val testPublicUser = User(
pk = 100,
username = "test",
fullName = "Test user"
)
@ExperimentalCoroutinesApi
@Test
fun testNoUsernameNoCurrentUser() {
fun `no state username and null current user`() {
val viewModel = ProfileFragmentViewModel(
SavedStateHandle(),
UserRepository(UserServiceAdapter()),
@ -60,7 +60,7 @@ internal class ProfileFragmentViewModelTest {
@ExperimentalCoroutinesApi
@Test
fun testNoUsernameWithCurrentUser() {
fun `no state username with current user provided`() {
val viewModel = ProfileFragmentViewModel(
SavedStateHandle(),
UserRepository(UserServiceAdapter()),
@ -86,17 +86,32 @@ internal class ProfileFragmentViewModelTest {
@ExperimentalCoroutinesApi
@Test
fun testPublicUsernameWithNoCurrentUser() {
fun `state username without '@' and no current user`() {
// username without `@`
val state = SavedStateHandle(
mutableMapOf<String, Any?>(
"username" to testPublicUser.username
)
)
testPublicUsernameNoCurrentUserCommon(state)
}
@ExperimentalCoroutinesApi
@Test
fun `state username with '@' and no current user`() {
// username with `@`
val state = SavedStateHandle(
mutableMapOf<String, Any?>(
"username" to "@${testPublicUser.username}"
)
)
testPublicUsernameNoCurrentUserCommon(state)
}
@ExperimentalCoroutinesApi
private fun testPublicUsernameNoCurrentUserCommon(state: SavedStateHandle) {
val graphQLRepository = object : GraphQLRepository(GraphQLServiceAdapter()) {
override suspend fun fetchUser(username: String): User {
return testPublicUser
}
override suspend fun fetchUser(username: String): User = testPublicUser
}
val viewModel = ProfileFragmentViewModel(
state,
@ -117,4 +132,53 @@ internal class ProfileFragmentViewModelTest {
}
assertEquals(testPublicUser, profile.data)
}
@ExperimentalCoroutinesApi
@Test
fun `state username without '@' and current user provided`() {
// username without `@`
val state = SavedStateHandle(
mutableMapOf<String, Any?>(
"username" to testPublicUser.username
)
)
testPublicUsernameCurrentUserCommon(state)
}
@ExperimentalCoroutinesApi
@Test
fun `state username with '@' and current user provided`() {
// username with `@`
val state = SavedStateHandle(
mutableMapOf<String, Any?>(
"username" to "@${testPublicUser.username}"
)
)
testPublicUsernameCurrentUserCommon(state)
}
@ExperimentalCoroutinesApi
private fun testPublicUsernameCurrentUserCommon(state: SavedStateHandle) {
val userRepository = object: UserRepository(UserServiceAdapter()) {
override suspend fun getUsernameInfo(username: String): User = testPublicUser
}
val viewModel = ProfileFragmentViewModel(
state,
userRepository,
FriendshipRepository(FriendshipServiceAdapter()),
StoriesRepository(StoriesServiceAdapter()),
MediaRepository(MediaServiceAdapter()),
GraphQLRepository(GraphQLServiceAdapter()),
AccountRepository(AccountDataSource(AccountDaoAdapter())),
FavoriteRepository(FavoriteDataSource(FavoriteDaoAdapter())),
coroutineScope.dispatcher,
)
viewModel.setCurrentUser(Resource.success(User()))
assertEquals(true, viewModel.isLoggedIn.getOrAwaitValue())
var profile = viewModel.profile.getOrAwaitValue()
while (profile.status == Resource.Status.LOADING) {
profile = viewModel.profile.getOrAwaitValue()
}
assertEquals(testPublicUser, profile.data)
}
}