}
public void removeFromDB() {
SQLiteDatabase db = MLDB.getWritableDatabase();
- Log.d("db", String.format("removinf progile %s from DB", uuid));
+ Log.d("db", String.format("removing progile %s from DB", uuid));
db.execSQL("delete from profiles where uuid=?", new Object[]{uuid});
}
}
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
-import java.util.Observable;
-import java.util.Observer;
public class MainActivity extends AppCompatActivity {
public MobileLedgerListFragment currentFragment = null;
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
- Data.profile.addObserver(new Observer() {
- @Override
- public void update(Observable o, Object arg) {
- MobileLedgerProfile profile = Data.profile.get();
- runOnUiThread(() -> {
- if (profile == null) toolbar.setSubtitle("");
- else toolbar.setSubtitle(profile.getName());
- });
- }
+ Data.profile.addObserver((o, arg) -> {
+ MobileLedgerProfile profile = Data.profile.get();
+ runOnUiThread(() -> {
+ if (profile == null) toolbar.setSubtitle("");
+ else toolbar.setSubtitle(profile.getName());
+ });
});
setupProfile();
if (profile == null) throw new AssertionError("profile must have a value");
- Data.profile.set(profile);
- MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid());
+ Data.setCurrentProfile(profile);
if (profile.getUrl().isEmpty()) {
Intent intent = new Intent(this, ProfileListActivity.class);
import net.ktnx.mobileledger.model.MobileLedgerProfile;
import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
-import net.ktnx.mobileledger.utils.MLDB;
import java.util.Collections;
-import java.util.List;
+import java.util.Observable;
+import java.util.Observer;
/**
* An activity representing a list of Profiles. This activity
Log.d("profiles", "got edit profile action");
int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, -1);
if (index >= 0) {
- List<MobileLedgerProfile> list = MobileLedgerProfile.loadAllFromDB();
- if (index < list.size()) {
- ProfilesRecyclerViewAdapter adapter =
- (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
- if (adapter != null) adapter.editProfile(recyclerView, list.get(index));
- }
+ MobileLedgerProfile profile = Data.profiles.get(index);
+ ProfilesRecyclerViewAdapter adapter =
+ (ProfilesRecyclerViewAdapter) recyclerView.getAdapter();
+ if (adapter != null) adapter.editProfile(recyclerView, profile);
}
}
}
private final ProfileListActivity mParentActivity;
private final boolean mTwoPane;
private final View.OnClickListener mOnClickListener = view -> {
- MobileLedgerProfile item = (MobileLedgerProfile) ((View) view.getParent()).getTag();
- editProfile(view, item);
+ MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
+ editProfile(view, profile);
};
ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
mParentActivity = parent;
mTwoPane = twoPane;
Data.profiles.addObserver((o, arg) -> {
Log.d("profiles", "profile list changed");
- notifyDataSetChanged();
+ if (arg == null) notifyDataSetChanged();
+ else notifyItemChanged((int) arg);
});
}
- private void editProfile(View view, MobileLedgerProfile item) {
+ private void editProfile(View view, MobileLedgerProfile profile) {
+ int index = Data.profiles.indexOf(profile);
if (mTwoPane) {
Bundle arguments = new Bundle();
- arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, item.getUuid());
+ arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
ProfileDetailFragment fragment = new ProfileDetailFragment();
fragment.setArguments(arguments);
mParentActivity.getSupportFragmentManager().beginTransaction()
else {
Context context = view.getContext();
Intent intent = new Intent(context, ProfileDetailActivity.class);
- intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID,
- (item == null) ? null : item.getUuid());
+ if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
context.startActivity(intent);
}
Data.profile.addObserver((o, arg) -> {
MobileLedgerProfile newProfile = Data.profile.get();
MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
- holder.mRadioView.setChecked(
- newProfile != null && newProfile.getUuid().equals(profile.getUuid()));
+ holder.mRadioView.setChecked(profile.equals(newProfile));
});
return holder;
}
final MobileLedgerProfile currentProfile = Data.profile.get();
Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
currentProfile.getUuid()));
- View.OnClickListener profileSelector = v -> holder.mRadioView.setChecked(true);
+ View.OnClickListener profileSelector = v -> {
+ holder.mRadioView.setChecked(true);
+ Data.setCurrentProfile(profile);
+ };
+ Data.profile.addObserver(new Observer() {
+ @Override
+ public void update(Observable o, Object arg) {
+ holder.mRadioView.setChecked(Data.profile.get().equals(profile));
+ }
+ });
holder.mTitle.setText(profile.getName());
holder.mTitle.setOnClickListener(profileSelector);
holder.mSubTitle.setText(profile.getUrl());
holder.mSubTitle.setOnClickListener(profileSelector);
holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid()));
- holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> {
- if (!isChecked) return;
- MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid());
- Data.profile.set(profile);
- });
holder.itemView.setTag(profile);
holder.mEditButton.setOnClickListener(mOnClickListener);
-
}
@Override
public int getItemCount() {
// http://developer.android.com/guide/components/fragments.html
//
if (savedInstanceState == null) {
- final String profileUUID =
- getIntent().getStringExtra(ProfileDetailFragment.ARG_ITEM_ID);
+ final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1);
- if (profileUUID != null) {
- int i = 0;
- for (MobileLedgerProfile p : Data.profiles.getList()) {
- if (p.getUuid().equals(profileUUID)) {
- Log.d("profiles", String.format("found profile %s at %d", profileUUID, i));
- profile = p;
- break;
- }
- i++;
- }
+ if (index != -1) {
+ profile = Data.profiles.get(index);
if (profile == null) throw new AssertionError(
- String.format("Can't get profile " + "(uuid:%s) from the " + "global list",
- profileUUID));
+ String.format("Can't get profile " + "(index:%d) from the global list",
+ index));
}
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
- arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, profileUUID);
+ arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index);
ProfileDetailFragment fragment = new ProfileDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
getMenuInflater().inflate(R.menu.profile_details, menu);
MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete);
menuDeleteProfile.setOnMenuItemClickListener(item -> {
- Log.d("profiles", String.format("deleting profile %s", profile.getUuid()));
+ Log.d("profiles", String.format("[activity] deleting profile %s", profile.getUuid()));
profile.removeFromDB();
Data.profiles.remove(profile);
- Data.profile.set(Data.profiles.get(0));
+ if (Data.profile.get().equals(profile)) {
+ Log.d("profiles", "[activity] selecting profile 0");
+ Data.setCurrentProfile(Data.profiles.get(0));
+ }
finish();
return true;
});
Log.d("profiles", String.format("[fragment] removing profile %s", mProfile.getUuid()));
mProfile.removeFromDB();
Data.profiles.remove(mProfile);
- if (Data.profile.get().getUuid().equals(mProfile.getUuid())) {
- Data.profile.set(Data.profiles.get(0));
+ if (Data.profile.get().equals(mProfile)) {
+ Log.d("profiles", "[fragment] setting current profile to 0");
+ Data.setCurrentProfile(Data.profiles.get(0));
}
return false;
});
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
- String uuid = getArguments().getString(ARG_ITEM_ID);
- if (uuid != null) mProfile =
- MobileLedgerProfile.loadUUIDFromDB(getArguments().getString(ARG_ITEM_ID));
+ int index = getArguments().getInt(ARG_ITEM_ID, -1);
+ if (index != -1) mProfile = Data.profiles.get(index);
Activity activity = this.getActivity();
if (activity == null) throw new AssertionError();
mProfile.setAuthUserName(userName.getText());
mProfile.setAuthPassword(password.getText());
mProfile.storeInDB();
+ Log.d("profiles", "profile stored in DB");
+ Data.profiles.triggerItemChangedNotification(mProfile);
if (mProfile.getUuid().equals(Data.profile.get().getUuid())) {
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
+import android.util.Log;
import java.util.Collection;
import java.util.Comparator;
setChanged();
notifyObservers();
}
- private void forceNotify(Object arg) {
+ private void forceNotify(int index) {
setChanged();
- notifyObservers(arg);
+ notifyObservers(index);
}
public int size() {
return list.size();
public void forEach(Consumer<? super T> action) {
list.forEach(action);
}
+ public List<T> getList() {
+ return list;
+ }
public void setList(List<T> aList) {
list = aList;
forceNotify();
}
- public List<T> getList() {
- return list;
+ public void triggerItemChangedNotification(T item) {
+ int index = list.indexOf(item);
+ if (index == -1) {
+ Log.d("ObList", "??? not sending notifications for item not found in the list");
+ return;
+ }
+ Log.d("ObList", "Notifying item change observers");
+ triggerItemChangedNotification(index);
+ }
+ public void triggerItemChangedNotification(int index) {
+ forceNotify(index);
}
}
\ No newline at end of file