this.authUserName = String.valueOf(authUserName);
this.authPassword = String.valueOf(authPassword);
}
- public static List<MobileLedgerProfile> loadAllFromDB() {
- List<MobileLedgerProfile> result = new ArrayList<>();
+ // loads all profiles into Data.profiles
+ // returns the profile with the given UUID
+ public static MobileLedgerProfile loadAllFromDB(String currentProfileUUID) {
+ MobileLedgerProfile result = null;
+ List<MobileLedgerProfile> list = new ArrayList<>();
SQLiteDatabase db = MLDB.getReadableDatabase();
try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
"auth_password FROM profiles order by order_no", null))
{
while (cursor.moveToNext()) {
- result.add(new MobileLedgerProfile(cursor.getString(0), cursor.getString(1),
+ MobileLedgerProfile item =
+ new MobileLedgerProfile(cursor.getString(0), cursor.getString(1),
cursor.getString(2), cursor.getInt(3) == 1, cursor.getString(4),
- cursor.getString(5)));
+ cursor.getString(5));
+ list.add(item);
+ if (item.getUuid().equals(currentProfileUUID)) result = item;
}
}
+ Data.profiles.setList(list);
return result;
}
public static void storeProfilesOrder() {
});
}
private void setupProfile() {
- Data.profiles.setList(MobileLedgerProfile.loadAllFromDB());
- MobileLedgerProfile profile = null;
-
String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
+ MobileLedgerProfile profile;
+
if (profileUUID == null) {
if (Data.profiles.isEmpty()) {
Data.profiles.setList(MobileLedgerProfile.createInitialProfileList());
editor.clear();
editor.apply();
}
+ else profile = Data.profiles.get(0);
}
else {
- profile = MobileLedgerProfile.loadUUIDFromDB(profileUUID);
+ profile = MobileLedgerProfile.loadAllFromDB(profileUUID);
}
if (profile == null) profile = Data.profiles.get(0);
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.profile_list_content, parent, false);
ProfileListViewHolder holder = new ProfileListViewHolder(view);
- Data.profile.addObserver((o, arg) -> {
- MobileLedgerProfile newProfile = Data.profile.get();
- MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
- holder.mRadioView.setChecked(profile.equals(newProfile));
- });
holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (!isChecked) return;
+ Log.d("profiles",
+ String.format("Item %d got checked", holder.getAdapterPosition()));
MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
- if (profile != null) Data.setCurrentProfile(profile);
+ if (profile != null) {
+ Log.d("profiles",
+ String.format("Setting current profile to %s", profile.getUuid()));
+ Data.setCurrentProfile(profile);
+ }
});
View.OnClickListener profileSelector = v -> holder.mRadioView.setChecked(true);
holder.mTitle.setOnClickListener(profileSelector);
holder.mSubTitle.setOnClickListener(profileSelector);
Data.profile.addObserver((o, arg) -> {
- MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag();
- holder.mRadioView.setChecked(Data.profile.get().equals(profile));
+ MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag();
+ final MobileLedgerProfile currentProfile = Data.profile.get();
+ final boolean sameProfile = currentProfile.equals(myProfile);
+ if (holder.mRadioView.isChecked() != sameProfile) {
+ holder.mRadioView.setChecked(sameProfile);
+ }
});
return holder;
}
final MobileLedgerProfile currentProfile = Data.profile.get();
Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
currentProfile.getUuid()));
+ holder.itemView.setTag(profile);
holder.mTitle.setText(profile.getName());
holder.mSubTitle.setText(profile.getUrl());
- holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid()));
+ holder.mRadioView.setChecked(profile.equals(currentProfile));
- holder.itemView.setTag(profile);
holder.mEditButton.setOnClickListener(mOnClickListener);
}
@Override