From faf299f6f0fb1a60d08b5819c353bbbe31c8ee82 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 5 Jun 2021 19:36:00 +0900 Subject: [PATCH] Preparation for adding ProfileFragmentViewModel --- app/build.gradle | 8 +++++++ .../fragments/main/ProfileFragment.java | 15 +++++++------ .../viewmodels/ProfileFragmentViewModel.kt | 21 +++++++++++++++++++ .../ProfileFragmentViewModelTest.kt | 18 ++++++++++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt create mode 100644 app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt diff --git a/app/build.gradle b/app/build.gradle index 815ff708..17609e6d 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -147,6 +147,11 @@ android { exclude 'META-INF/LICENSE.md' exclude 'META-INF/LICENSE-notice.md' } + + testOptions.unitTests { + includeAndroidResources = true + } + } configurations.all { @@ -231,6 +236,9 @@ dependencies { githubImplementation 'io.sentry:sentry-android:4.3.0' testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2' + testImplementation "androidx.test.ext:junit-ktx:1.1.2" + testImplementation "androidx.test:core-ktx:1.3.0" + testImplementation "org.robolectric:robolectric:4.5.1" androidTestImplementation 'org.junit.jupiter:junit-jupiter:5.7.1' androidTestImplementation 'androidx.test:core:1.3.0' diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index 10365ef1..6146a89c 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -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 changeCb = new ServiceCallback() { @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; diff --git a/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt b/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt new file mode 100644 index 00000000..47f33264 --- /dev/null +++ b/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt @@ -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() + val profile: LiveData = _profile + val username: LiveData = 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()) + } +} diff --git a/app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt b/app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt new file mode 100644 index 00000000..32013e9d --- /dev/null +++ b/app/src/test/java/awais/instagrabber/viewmodels/ProfileFragmentViewModelTest.kt @@ -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( + "username" to "" + )) + val viewModel = ProfileFragmentViewModel(state) + + } +} \ No newline at end of file