From f5d585d79f8c263476c8fb78ada15f04472d9a91 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 18 Apr 2021 12:42:54 +0300 Subject: [PATCH] fix creation of a new profile after Room-y of Profile adoption --- .../java/net/ktnx/mobileledger/model/Data.java | 16 ++++++++-------- .../ui/profiles/ProfileDetailActivity.java | 3 --- .../ui/profiles/ProfilesRecyclerViewAdapter.java | 3 ++- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 35f39358..c14bc3f1 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -18,6 +18,7 @@ package net.ktnx.mobileledger.model; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; @@ -35,7 +36,6 @@ import java.text.ParsePosition; import java.util.Date; import java.util.List; import java.util.Locale; -import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import static net.ktnx.mobileledger.utils.Logger.debug; @@ -45,7 +45,9 @@ public final class Data { new MutableLiveData<>(false); public static final MutableLiveData backgroundTaskProgress = new MutableLiveData<>(); - public static final LiveData> profiles = DB.get().getProfileDAO().getAllOrdered(); + public static final LiveData> profiles = DB.get() + .getProfileDAO() + .getAllOrdered(); public static final MutableLiveData currencySymbolPosition = new MutableLiveData<>(); public static final MutableLiveData currencyGap = new MutableLiveData<>(true); @@ -58,8 +60,7 @@ public final class Data { public static final MutableLiveData lastTransactionsUpdateText = new MutableLiveData<>(); public static final MutableLiveData lastAccountsUpdateText = new MutableLiveData<>(); - private static final MutableLiveData profile = - new InertMutableLiveData<>(); + private static final MutableLiveData profile = new InertMutableLiveData<>(); private static final AtomicInteger backgroundTaskCount = new AtomicInteger(0); private static final Locker profilesLocker = new Locker(); private static NumberFormat numberFormatter; @@ -68,9 +69,9 @@ public final class Data { locale.setValue(Locale.getDefault()); } - @NonNull + @Nullable public static Profile getProfile() { - return Objects.requireNonNull(profile.getValue()); + return profile.getValue(); } public static void backgroundTaskStarted() { int cnt = backgroundTaskCount.incrementAndGet(); @@ -135,8 +136,7 @@ public final class Data { public static String formatNumber(float number) { return numberFormatter.format(number); } - public static void observeProfile(LifecycleOwner lifecycleOwner, - Observer observer) { + public static void observeProfile(LifecycleOwner lifecycleOwner, Observer observer) { profile.observe(lifecycleOwner, observer); } public static void removeProfileObservers(LifecycleOwner owner) { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java index d389dd8a..1b88f480 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java @@ -65,9 +65,6 @@ public class ProfileDetailActivity extends CrashReportingActivity { protected void onCreate(Bundle savedInstanceState) { final long id = getIntent().getLongExtra(ProfileDetailFragment.ARG_ITEM_ID, -1); - if (id == -1) - throw new RuntimeException("Invalid or missing profile ID"); - DB.get() .getProfileDAO() .getById(id) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java index 688cd6d4..d1cceb42 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java @@ -175,7 +175,8 @@ public class ProfilesRecyclerViewAdapter ProfileDetailActivity.start(view.getContext(), p); }); - final boolean sameProfile = currentProfile.getId() == profile.getId(); + final boolean sameProfile = + currentProfile != null && currentProfile.getId() == profile.getId(); holder.itemView.setBackground( sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null); if (editingProfiles()) { -- 2.39.5