Browse Source

Add FriendshipRepository to ProfileFragmentViewModel constructor

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
8f218b9515
  1. 1
      app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java
  2. 7
      app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt
  3. 33
      app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt

1
app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java

@ -346,6 +346,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
appStateViewModel = new ViewModelProvider(fragmentActivity).get(AppStateViewModel.class); appStateViewModel = new ViewModelProvider(fragmentActivity).get(AppStateViewModel.class);
viewModel = new ViewModelProvider(this, new ProfileFragmentViewModelFactory( viewModel = new ViewModelProvider(this, new ProfileFragmentViewModelFactory(
UserRepository.Companion.getInstance(), UserRepository.Companion.getInstance(),
FriendshipRepository.Companion.getInstance(),
this, this,
getArguments() getArguments()
)).get(ProfileFragmentViewModel.class); )).get(ProfileFragmentViewModel.class);

7
app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt

@ -6,11 +6,13 @@ import androidx.lifecycle.*
import androidx.savedstate.SavedStateRegistryOwner import androidx.savedstate.SavedStateRegistryOwner
import awais.instagrabber.repositories.responses.User import awais.instagrabber.repositories.responses.User
import awais.instagrabber.utils.extensions.TAG import awais.instagrabber.utils.extensions.TAG
import awais.instagrabber.webservices.FriendshipRepository
import awais.instagrabber.webservices.UserRepository import awais.instagrabber.webservices.UserRepository
class ProfileFragmentViewModel( class ProfileFragmentViewModel(
state: SavedStateHandle, state: SavedStateHandle,
userRepository: UserRepository, userRepository: UserRepository,
friendshipRepository: FriendshipRepository,
) : ViewModel() { ) : ViewModel() {
private val _profile = MutableLiveData<User?>() private val _profile = MutableLiveData<User?>()
val profile: LiveData<User?> = _profile val profile: LiveData<User?> = _profile
@ -22,13 +24,14 @@ class ProfileFragmentViewModel(
private set private set
init { init {
Log.d(TAG, "${state.keys()} $userRepository")
Log.d(TAG, "${state.keys()} $userRepository $friendshipRepository")
} }
} }
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
class ProfileFragmentViewModelFactory( class ProfileFragmentViewModelFactory(
private val userRepository: UserRepository, private val userRepository: UserRepository,
private val friendshipRepository: FriendshipRepository,
owner: SavedStateRegistryOwner, owner: SavedStateRegistryOwner,
defaultArgs: Bundle? = null, defaultArgs: Bundle? = null,
) : AbstractSavedStateViewModelFactory(owner, defaultArgs) { ) : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
@ -37,6 +40,6 @@ class ProfileFragmentViewModelFactory(
modelClass: Class<T>, modelClass: Class<T>,
handle: SavedStateHandle, handle: SavedStateHandle,
): T { ): T {
return ProfileFragmentViewModel(handle, userRepository) as T
return ProfileFragmentViewModel(handle, userRepository, friendshipRepository) as T
} }
} }

33
app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt

@ -2,10 +2,10 @@ package awais.instagrabber.viewmodels
import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.SavedStateHandle
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import awais.instagrabber.repositories.FriendshipService
import awais.instagrabber.repositories.UserService import awais.instagrabber.repositories.UserService
import awais.instagrabber.repositories.responses.FriendshipStatus
import awais.instagrabber.repositories.responses.UserSearchResponse
import awais.instagrabber.repositories.responses.WrappedUser
import awais.instagrabber.repositories.responses.*
import awais.instagrabber.webservices.FriendshipRepository
import awais.instagrabber.webservices.UserRepository import awais.instagrabber.webservices.UserRepository
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
@ -30,12 +30,33 @@ internal class ProfileFragmentViewModelTest {
} }
} }
private val friendshipService = object : FriendshipService {
override suspend fun change(action: String, id: Long, form: Map<String, String>): FriendshipChangeResponse {
TODO("Not yet implemented")
}
override suspend fun toggleRestrict(action: String, form: Map<String, String>): FriendshipRestrictResponse {
TODO("Not yet implemented")
}
override suspend fun getList(userId: Long, type: String, queryParams: Map<String, String>): String {
TODO("Not yet implemented")
}
override suspend fun changeMute(action: String, form: Map<String, String>): FriendshipChangeResponse {
TODO("Not yet implemented")
}
}
@Test @Test
fun testNoUsernameNoCurrentUser() { fun testNoUsernameNoCurrentUser() {
val state = SavedStateHandle(mutableMapOf<String, Any>(
val state = SavedStateHandle(
mutableMapOf<String, Any>(
"username" to "" "username" to ""
))
)
)
val userRepository = UserRepository(userService) val userRepository = UserRepository(userService)
val viewModel = ProfileFragmentViewModel(state, userRepository)
val friendshipRepository = FriendshipRepository(friendshipService)
val viewModel = ProfileFragmentViewModel(state, userRepository, friendshipRepository)
} }
} }
Loading…
Cancel
Save