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 net.ktnx.mobileledger.utils.Logger;
import org.jetbrains.annotations.NotNull;
-import java.util.ArrayList;
import java.util.Locale;
import static net.ktnx.mobileledger.utils.Logger.debug;
* in a ProfileListActivity (not really).
*/
public class ProfileDetailActivity extends CrashReportingActivity {
- private MobileLedgerProfile profile = null;
+ private static final String TAG = "profile-det-act";
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_HUE, profile.getTheme());
+ Logger.debug(TAG,
+ String.format(Locale.ROOT, "Starting profile editor for profile %d, theme %d",
+ profile.getId(), profile.getTheme()));
+ }
+ else
+ Logger.debug(TAG, "Starting empty profile editor");
+ 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<MobileLedgerProfile> 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.getUuid(), profile.getThemeHue()));
- }
- }
+ final long id = getIntent().getLongExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
+
+ 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);
// 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);
.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);