mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-22 06:37:30 +00:00
Update ProfileFragmentViewModel
This commit is contained in:
parent
1ebf7a2e4b
commit
976c9a86b6
@ -41,7 +41,7 @@ class ProfileFragmentViewModel(
|
|||||||
value = currentUser to stateUsername
|
value = currentUser to stateUsername
|
||||||
}
|
}
|
||||||
addSource(state.getLiveData<String?>("username")) { username ->
|
addSource(state.getLiveData<String?>("username")) { username ->
|
||||||
this.stateUsername = Resource.success(username)
|
this.stateUsername = Resource.success(username.substringAfter('@'))
|
||||||
value = user to this.stateUsername
|
value = user to this.stateUsername
|
||||||
}
|
}
|
||||||
// trigger currentUserAndStateUsernameLiveData switch map with a state username success resource
|
// trigger currentUserAndStateUsernameLiveData switch map with a state username success resource
|
||||||
@ -87,6 +87,7 @@ class ProfileFragmentViewModel(
|
|||||||
Resource.Status.SUCCESS -> it.data?.username ?: ""
|
Resource.Status.SUCCESS -> it.data?.username ?: ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
// Log.d(TAG, "${state.keys()} $userRepository $friendshipRepository $storiesRepository $mediaRepository")
|
// Log.d(TAG, "${state.keys()} $userRepository $friendshipRepository $storiesRepository $mediaRepository")
|
||||||
}
|
}
|
||||||
|
@ -7,14 +7,14 @@ import awais.instagrabber.repositories.responses.UserSearchResponse
|
|||||||
import awais.instagrabber.webservices.RetrofitFactory.retrofit
|
import awais.instagrabber.webservices.RetrofitFactory.retrofit
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class UserRepository(private val service: UserService) {
|
open class UserRepository(private val service: UserService) {
|
||||||
|
|
||||||
suspend fun getUserInfo(uid: Long): User {
|
suspend fun getUserInfo(uid: Long): User {
|
||||||
val response = service.getUserInfo(uid)
|
val response = service.getUserInfo(uid)
|
||||||
return response.user
|
return response.user
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getUsernameInfo(username: String): User {
|
open suspend fun getUsernameInfo(username: String): User {
|
||||||
val response = service.getUsernameInfo(username)
|
val response = service.getUsernameInfo(username)
|
||||||
return response.user
|
return response.user
|
||||||
}
|
}
|
||||||
|
@ -23,12 +23,6 @@ import org.junit.runner.RunWith
|
|||||||
@RunWith(AndroidJUnit4::class)
|
@RunWith(AndroidJUnit4::class)
|
||||||
internal class ProfileFragmentViewModelTest {
|
internal class ProfileFragmentViewModelTest {
|
||||||
|
|
||||||
private val testPublicUser = User(
|
|
||||||
pk = 100,
|
|
||||||
username = "test",
|
|
||||||
fullName = "Test user"
|
|
||||||
)
|
|
||||||
|
|
||||||
@get:Rule
|
@get:Rule
|
||||||
var instantExecutorRule = InstantTaskExecutorRule()
|
var instantExecutorRule = InstantTaskExecutorRule()
|
||||||
|
|
||||||
@ -36,9 +30,15 @@ internal class ProfileFragmentViewModelTest {
|
|||||||
@get:Rule
|
@get:Rule
|
||||||
val coroutineScope = MainCoroutineScopeRule()
|
val coroutineScope = MainCoroutineScopeRule()
|
||||||
|
|
||||||
|
private val testPublicUser = User(
|
||||||
|
pk = 100,
|
||||||
|
username = "test",
|
||||||
|
fullName = "Test user"
|
||||||
|
)
|
||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
@ExperimentalCoroutinesApi
|
||||||
@Test
|
@Test
|
||||||
fun testNoUsernameNoCurrentUser() {
|
fun `no state username and null current user`() {
|
||||||
val viewModel = ProfileFragmentViewModel(
|
val viewModel = ProfileFragmentViewModel(
|
||||||
SavedStateHandle(),
|
SavedStateHandle(),
|
||||||
UserRepository(UserServiceAdapter()),
|
UserRepository(UserServiceAdapter()),
|
||||||
@ -60,7 +60,7 @@ internal class ProfileFragmentViewModelTest {
|
|||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
@ExperimentalCoroutinesApi
|
||||||
@Test
|
@Test
|
||||||
fun testNoUsernameWithCurrentUser() {
|
fun `no state username with current user provided`() {
|
||||||
val viewModel = ProfileFragmentViewModel(
|
val viewModel = ProfileFragmentViewModel(
|
||||||
SavedStateHandle(),
|
SavedStateHandle(),
|
||||||
UserRepository(UserServiceAdapter()),
|
UserRepository(UserServiceAdapter()),
|
||||||
@ -86,17 +86,32 @@ internal class ProfileFragmentViewModelTest {
|
|||||||
|
|
||||||
@ExperimentalCoroutinesApi
|
@ExperimentalCoroutinesApi
|
||||||
@Test
|
@Test
|
||||||
fun testPublicUsernameWithNoCurrentUser() {
|
fun `state username without '@' and no current user`() {
|
||||||
// username without `@`
|
// username without `@`
|
||||||
val state = SavedStateHandle(
|
val state = SavedStateHandle(
|
||||||
mutableMapOf<String, Any?>(
|
mutableMapOf<String, Any?>(
|
||||||
"username" to testPublicUser.username
|
"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()) {
|
val graphQLRepository = object : GraphQLRepository(GraphQLServiceAdapter()) {
|
||||||
override suspend fun fetchUser(username: String): User {
|
override suspend fun fetchUser(username: String): User = testPublicUser
|
||||||
return testPublicUser
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val viewModel = ProfileFragmentViewModel(
|
val viewModel = ProfileFragmentViewModel(
|
||||||
state,
|
state,
|
||||||
@ -117,4 +132,53 @@ internal class ProfileFragmentViewModelTest {
|
|||||||
}
|
}
|
||||||
assertEquals(testPublicUser, profile.data)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user