diff --git a/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt b/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt index 3094f4f6..98e2d27c 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt +++ b/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt @@ -41,7 +41,7 @@ class ProfileFragmentViewModel( value = currentUser to stateUsername } addSource(state.getLiveData("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") } diff --git a/app/src/main/java/awais/instagrabber/webservices/UserRepository.kt b/app/src/main/java/awais/instagrabber/webservices/UserRepository.kt index 1a3b7b05..9ef594a4 100644 --- a/app/src/main/java/awais/instagrabber/webservices/UserRepository.kt +++ b/app/src/main/java/awais/instagrabber/webservices/UserRepository.kt @@ -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 } diff --git a/app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt b/app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt index 63372112..b85b90e4 100644 --- a/app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt +++ b/app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt @@ -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( "username" to testPublicUser.username ) ) + testPublicUsernameNoCurrentUserCommon(state) + } + + @ExperimentalCoroutinesApi + @Test + fun `state username with '@' and no current user`() { + // username with `@` + val state = SavedStateHandle( + mutableMapOf( + "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( + "username" to testPublicUser.username + ) + ) + testPublicUsernameCurrentUserCommon(state) + } + + @ExperimentalCoroutinesApi + @Test + fun `state username with '@' and current user provided`() { + // username with `@` + val state = SavedStateHandle( + mutableMapOf( + "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) + } } \ No newline at end of file