X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fprofiles%2FProfileDetailActivity.java;h=d389dd8ab9a52cd55e4e56eb610b1ca2ab77524c;hp=d860a678649bc37833f3cf2f98461b863d2c39b8;hb=5df10dc0b58df4d4be4e9ab34f1e0f477ca46766;hpb=7c71910950d18868e1f419eed5234f113ee51776 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 d860a678..d389dd8a 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 @@ -17,25 +17,28 @@ package net.ktnx.mobileledger.ui.profiles; +import android.content.Context; +import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; +import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; import androidx.appcompat.widget.Toolbar; import androidx.lifecycle.ViewModelProvider; +import com.google.android.material.appbar.CollapsingToolbarLayout; + import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.db.DB; +import net.ktnx.mobileledger.db.Profile; import net.ktnx.mobileledger.model.Data; -import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.ui.activity.CrashReportingActivity; import net.ktnx.mobileledger.utils.Colors; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; -import java.util.Locale; - import static net.ktnx.mobileledger.utils.Logger.debug; /** @@ -45,40 +48,41 @@ import static net.ktnx.mobileledger.utils.Logger.debug; * in a ProfileListActivity (not really). */ public class ProfileDetailActivity extends CrashReportingActivity { - private MobileLedgerProfile profile = null; private ProfileDetailFragment mFragment; + public static void start(Context context, @Nullable Profile profile) { + Intent starter = new Intent(context, ProfileDetailActivity.class); + if (profile != null) { + starter.putExtra(ProfileDetailFragment.ARG_ITEM_ID, profile.getId()); + starter.putExtra(ProfileDetailFragment.ARG_ITEM_ID, profile.getTheme()); + } + context.startActivity(starter); + } @NotNull private ProfileDetailModel getModel() { return new ViewModelProvider(this).get(ProfileDetailModel.class); } @Override protected void onCreate(Bundle savedInstanceState) { - final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1); - - if (index != -1) { - ArrayList profiles = Data.profiles.getValue(); - if (profiles != null) { - profile = profiles.get(index); - if (profile == null) - throw new AssertionError( - String.format("Can't get profile " + "(index:%d) from the global list", - index)); - - debug("profiles", String.format(Locale.ENGLISH, "Editing profile %s (%s); hue=%d", - profile.getName(), profile.getId(), profile.getThemeHue())); - } - } + 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) + .observe(this, this::setProfile); + + int themeHue = getIntent().getIntExtra(ProfileDetailFragment.ARG_HUE, -1); super.onCreate(savedInstanceState); - int themeHue; - if (profile != null) - themeHue = profile.getThemeHue(); - else { + if (themeHue == -1) { themeHue = Colors.getNewProfileThemeHue(Data.profiles.getValue()); } Colors.setupTheme(this, themeHue); final ProfileDetailModel model = getModel(); model.initialThemeHue = themeHue; + model.setThemeId(themeHue); setContentView(R.layout.activity_profile_detail); Toolbar toolbar = findViewById(R.id.detail_toolbar); setSupportActionBar(toolbar); @@ -103,7 +107,6 @@ public class ProfileDetailActivity extends CrashReportingActivity { // Create the detail fragment and add it to the activity // using a fragment transaction. Bundle arguments = new Bundle(); - arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index); arguments.putInt(ProfileDetailFragment.ARG_HUE, themeHue); mFragment = new ProfileDetailFragment(); mFragment.setArguments(arguments); @@ -112,6 +115,17 @@ public class ProfileDetailActivity extends CrashReportingActivity { .commit(); } } + private void setProfile(Profile profile) { + ProfileDetailModel model = new ViewModelProvider(this).get(ProfileDetailModel.class); + CollapsingToolbarLayout appBarLayout = findViewById(R.id.toolbar_layout); + if (appBarLayout != null) { + if (profile != null) + appBarLayout.setTitle(profile.getName()); + else + appBarLayout.setTitle(getResources().getString(R.string.new_profile_title)); + } + model.setValuesFromProfile(profile); + } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);