1
0
Fork 0
mirror of https://github.com/KokaKiwi/BarInsta synced 2026-03-04 19:41:36 +00:00

Preparation for adding ProfileFragmentViewModel

This commit is contained in:
Ammar Githam 2021-06-05 19:36:00 +09:00
parent 696a8c9c61
commit faf299f6f0
4 changed files with 56 additions and 6 deletions

View file

@ -93,6 +93,7 @@ import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.utils.Utils;
import awais.instagrabber.viewmodels.AppStateViewModel;
import awais.instagrabber.viewmodels.HighlightsViewModel;
import awais.instagrabber.viewmodels.ProfileFragmentViewModel;
import awais.instagrabber.webservices.DirectMessagesService;
import awais.instagrabber.webservices.FriendshipService;
import awais.instagrabber.webservices.GraphQLService;
@ -139,6 +140,12 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
private int downloadChildPosition = -1;
private long myId;
private PostsLayoutPreferences layoutPreferences = Utils.getPostsLayoutPreferences(Constants.PREF_PROFILE_POSTS_LAYOUT);
private LayoutProfileDetailsBinding profileDetailsBinding;
private AccountRepository accountRepository;
private FavoriteRepository favoriteRepository;
private AppStateViewModel appStateViewModel;
private boolean disableDm = false;
private ProfileFragmentViewModel viewModel;
private final ServiceCallback<FriendshipChangeResponse> changeCb = new ServiceCallback<FriendshipChangeResponse>() {
@Override
@ -156,7 +163,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
Log.e(TAG, "Error editing relationship", t);
}
};
private final Runnable usernameSettingRunnable = () -> {
final ActionBar actionBar = fragmentActivity.getSupportActionBar();
if (actionBar != null && !TextUtils.isEmpty(username)) {
@ -318,11 +324,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
}
}
};
private LayoutProfileDetailsBinding profileDetailsBinding;
private AccountRepository accountRepository;
private FavoriteRepository favoriteRepository;
private AppStateViewModel appStateViewModel;
private boolean disableDm = false;
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
@ -344,6 +345,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context));
favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(context));
appStateViewModel = new ViewModelProvider(fragmentActivity).get(AppStateViewModel.class);
viewModel = new ViewModelProvider(this).get(ProfileFragmentViewModel.class);
setHasOptionsMenu(true);
}
@ -373,6 +375,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
shouldRefresh = false;
return root;
}
// appStateViewModel.getCurrentUserLiveData().observe(getViewLifecycleOwner(), user -> viewModel.setCurrentUser(user));
binding = FragmentProfileBinding.inflate(inflater, container, false);
root = binding.getRoot();
profileDetailsBinding = binding.header;

View file

@ -0,0 +1,21 @@
package awais.instagrabber.viewmodels
import androidx.lifecycle.*
import awais.instagrabber.repositories.responses.User
class ProfileFragmentViewModel(
state: SavedStateHandle,
) : ViewModel() {
private val _profile = MutableLiveData<User?>()
val profile: LiveData<User?> = _profile
val username: LiveData<String> = Transformations.map(profile) { return@map it?.username ?: "" }
var currentUser: User? = null
var isLoggedIn = false
get() = currentUser != null
private set
init {
// Log.d(TAG, state.keys().toString())
}
}

View file

@ -0,0 +1,18 @@
package awais.instagrabber.viewmodels
import androidx.lifecycle.SavedStateHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
internal class ProfileFragmentViewModelTest {
@Test
fun testNoUsernameNoCurrentUser() {
val state = SavedStateHandle(mutableMapOf<String, Any>(
"username" to ""
))
val viewModel = ProfileFragmentViewModel(state)
}
}