1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-14 02:37:30 +00:00

Add FriendshipRepository to ProfileFragmentViewModel constructor

This commit is contained in:
Ammar Githam 2021-06-10 17:03:24 +09:00
parent e1532eb4b8
commit 8f218b9515
3 changed files with 35 additions and 10 deletions

View File

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

View File

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

View File

@ -2,17 +2,17 @@ package awais.instagrabber.viewmodels
import androidx.lifecycle.SavedStateHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import awais.instagrabber.repositories.FriendshipService
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 org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
internal class ProfileFragmentViewModelTest {
private val userService = object: UserService {
private val userService = object : UserService {
override suspend fun getUserInfo(uid: Long): WrappedUser {
TODO("Not yet implemented")
}
@ -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
fun testNoUsernameNoCurrentUser() {
val state = SavedStateHandle(mutableMapOf<String, Any>(
val state = SavedStateHandle(
mutableMapOf<String, Any>(
"username" to ""
))
)
)
val userRepository = UserRepository(userService)
val viewModel = ProfileFragmentViewModel(state, userRepository)
val friendshipRepository = FriendshipRepository(friendshipService)
val viewModel = ProfileFragmentViewModel(state, userRepository, friendshipRepository)
}
}