Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
- MobileLedgerProfile p = Data.profile.getValue();
- if ((p != null) && p.isAuthEnabled()) {
+ MobileLedgerProfile p = Data.getProfile();
+ if (p.isAuthEnabled()) {
try {
final URL url = new URL(p.getUrl());
final String requestingHost = getRequestingHost();
public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String[] filterAccName) {
- final MobileLedgerProfile profile = Data.profile.getValue();
- if (profile == null)
- return "Profile not configured";
+ final MobileLedgerProfile profile = Data.getProfile();
String profile_uuid = profile.getUuid();
Data.backgroundTaskStarted();
this.hasGap = hasGap;
}
public static Currency loadByName(String name) {
- MobileLedgerProfile profile = Data.profile.getValue();
+ MobileLedgerProfile profile = Data.getProfile();
return profile.loadCurrencyByName(name);
}
public int getId() {
public static final MutableLiveData<Boolean> backgroundTasksRunning =
new MutableLiveData<>(false);
public static final MutableLiveData<Date> lastUpdateDate = new MutableLiveData<>();
- public static final MutableLiveData<MobileLedgerProfile> profile = new InertMutableLiveData<>();
public static final MutableLiveData<ArrayList<MobileLedgerProfile>> profiles =
new MutableLiveData<>(null);
public static final MutableLiveData<String> accountFilter = new MutableLiveData<>();
new MutableLiveData<>();
public static final MutableLiveData<Boolean> currencyGap = new MutableLiveData<>(true);
public static final MutableLiveData<Locale> locale = new MutableLiveData<>(Locale.getDefault());
+ private static final MutableLiveData<MobileLedgerProfile> profile =
+ new InertMutableLiveData<>();
private static final AtomicInteger backgroundTaskCount = new AtomicInteger(0);
private static final Locker profilesLocker = new Locker();
public static MutableLiveData<Integer> foundTransactionItemIndex = new MutableLiveData<>(null);
private static RetrieveTransactionsTask retrieveTransactionsTask;
+ @NonNull
+ public static MobileLedgerProfile getProfile() {
+ return Objects.requireNonNull(profile.getValue());
+ }
public static final MutableLiveData<Boolean> drawerOpen = new MutableLiveData<>(false);
public static void backgroundTaskStarted() {
int cnt = backgroundTaskCount.incrementAndGet();
cnt));
backgroundTasksRunning.postValue(cnt > 0);
}
- public static void setCurrentProfile(MobileLedgerProfile newProfile) {
- MLDB.setOption(MLDB.OPT_PROFILE_UUID, (newProfile == null) ? null : newProfile.getUuid());
+ public static void setCurrentProfile(@NonNull MobileLedgerProfile newProfile) {
+ MLDB.setOption(MLDB.OPT_PROFILE_UUID, newProfile.getUuid());
stopTransactionsRetrieval();
profile.setValue(newProfile);
}
return -1;
}
+ @Nullable
public static MobileLedgerProfile getProfile(String profileUUID) {
MobileLedgerProfile profile;
try (LockHolder readLock = profilesLocker.lockForReading()) {
currencySymbolPosition.setValue(Currency.Position.none);
}
+ public static void observeProfile(LifecycleOwner lifecycleOwner,
+ Observer<MobileLedgerProfile> observer) {
+ profile.observe(lifecycleOwner, observer);
+ }
+ public synchronized static MobileLedgerProfile initProfile() {
+ MobileLedgerProfile currentProfile = profile.getValue();
+ if (currentProfile != null)
+ return currentProfile;
+
+ String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
+ MobileLedgerProfile startupProfile = getProfile(profileUUID);
+ if (startupProfile != null)
+ setCurrentProfile(startupProfile);
+ return startupProfile;
+ }
+
+ public static void removeProfileObservers(LifecycleOwner owner) {
+ profile.removeObservers(owner);
+ }
}
\ No newline at end of file
dataLoaded = false;
}
public LedgerTransaction(Integer id, SimpleDate date, String description) {
- this(id, date, description, Data.profile.getValue());
+ this(id, date, description, Data.getProfile());
}
public LedgerTransaction(SimpleDate date, String description) {
this(null, date, description);
model = new ViewModelProvider(this).get(CurrencySelectorModel.class);
if (onCurrencySelectedListener != null)
model.setOnCurrencySelectedListener(onCurrencySelectedListener);
- MobileLedgerProfile profile = Objects.requireNonNull(Data.profile.getValue());
+ MobileLedgerProfile profile = Objects.requireNonNull(Data.getProfile());
model.currencies.setValue(new CopyOnWriteArrayList<>(profile.getCurrencies()));
CurrencySelectorRecyclerViewAdapter adapter = new CurrencySelectorRecyclerViewAdapter();
Data.scheduleTransactionListRetrieval(mainActivity);
});
- MobileLedgerProfile profile = Data.profile.getValue();
- if (profile != null) {
- profile.getDisplayedAccounts()
- .observe(getViewLifecycleOwner(),
- (accounts) -> onAccountsChanged(profile, accounts));
- }
+ MobileLedgerProfile profile = Data.getProfile();
+ profile.getDisplayedAccounts()
+ .observe(getViewLifecycleOwner(),
+ (accounts) -> onAccountsChanged(profile, accounts));
}
private void onAccountsChanged(MobileLedgerProfile profile, List<LedgerAccount> accounts) {
Logger.debug("async-acc",
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
- Data.profile.observe(this, this::onProfileChanged);
+ Data.observeProfile(this, this::onProfileChanged);
Data.profiles.observe(this, this::onProfileListChanged);
storeThemeIdInPrefs(profile.getThemeHue());
// un-hook all observed LiveData
- Data.profile.removeObservers(this);
+ Data.removeProfileObservers(this);
Data.profiles.removeObservers(this);
Data.lastUpdateDate.removeObservers(this);
setContentView(R.layout.activity_new_transaction);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
- Data.profile.observe(this,
+ Data.observeProfile(this,
mobileLedgerProfile -> toolbar.setSubtitle(mobileLedgerProfile.getName()));
navController = Navigation.findNavController(this, R.id.new_transaction_nav);
viewModel = new ViewModelProvider(activity).get(NewTransactionModel.class);
viewModel.observeDataProfile(this);
- mProfile = Data.profile.getValue();
+ mProfile = Data.getProfile();
listAdapter = new NewTransactionItemsAdapter(viewModel, mProfile);
list.setAdapter(listAdapter);
list.setLayoutManager(new LinearLayoutManager(activity));
- Data.profile.observe(getViewLifecycleOwner(), profile -> {
+
+ Data.observeProfile(getViewLifecycleOwner(), profile -> {
mProfile = profile;
listAdapter.setProfile(profile);
});
tvTransactionComment.requestFocus();
});
- mProfile = Data.profile.getValue();
- if (mProfile == null)
- throw new AssertionError();
+ mProfile = Data.getProfile();
View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
final int id = v.getId();
}
void observeDataProfile(LifecycleOwner activity) {
if (!observingDataProfile)
- Data.profile.observe(activity, profileObserver);
+ Data.observeProfile(activity, profileObserver);
observingDataProfile = true;
}
boolean getSimulateSave() {
import net.ktnx.mobileledger.model.Data;
import net.ktnx.mobileledger.model.MobileLedgerProfile;
import net.ktnx.mobileledger.utils.Colors;
-import net.ktnx.mobileledger.utils.MLDB;
@SuppressLint("Registered")
public class ProfileThemedActivity extends CrashReportingActivity {
super.onCreate(savedInstanceState);
}
protected void initProfile() {
- mProfile = Data.profile.getValue();
- if (mProfile == null) {
- String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
- MobileLedgerProfile startupProfile;
-
-
- startupProfile = Data.getProfile(profileUUID);
- Data.setCurrentProfile(startupProfile);
- mProfile = startupProfile;
- }
+ mProfile = Data.initProfile();
}
}
public static final String ARG_HUE = "hue";
@NonNls
- /**
- * The content this fragment is presenting.
- */ private MobileLedgerProfile mProfile;
+ private MobileLedgerProfile mProfile;
private TextView url;
private TextView defaultCommodity;
private View defaultCommodityLayout;
ArrayList<MobileLedgerProfile> newList = new ArrayList<>(oldList);
newList.remove(mProfile);
Data.profiles.setValue(newList);
- if (mProfile.equals(Data.profile.getValue())) {
+ if (mProfile.equals(Data.getProfile())) {
debug("profiles", "[fragment] setting current profile to 0");
Data.setCurrentProfile(newList.get(0));
}
private boolean onWipeDataMenuClicked() {
// this is a development option, so no confirmation
mProfile.wipeAllData();
- if (mProfile.equals(Data.profile.getValue()))
+ if (mProfile.equals(Data.getProfile()))
triggerProfileChange();
return true;
}
if (viewAdapter != null)
viewAdapter.notifyItemChanged(index);
- if (mProfile.equals(Data.profile.getValue()))
- Data.profile.setValue(newProfile);
+ if (mProfile.equals(Data.getProfile()))
+ Data.setCurrentProfile(newProfile);
}
private void hookTextChangeSyncRoutine(TextView view, TextChangeSyncProc syncRoutine) {
view.addTextChangedListener(new TextWatcher() {
// first profile ever?
if (newList.size() == 1)
- Data.profile.setValue(mProfile);
+ Data.setCurrentProfile(mProfile);
}
Activity activity = getActivity();
if (profile == null)
throw new IllegalStateException("Profile row without associated profile");
debug("profiles", "Setting profile to " + profile.getName());
- if (Data.profile.getValue() != profile )
+ if (Data.getProfile() != profile)
Data.drawerOpen.setValue(false);
Data.setCurrentProfile(profile);
}
final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
if (profiles == null) throw new AssertionError();
final MobileLedgerProfile profile = profiles.get(position);
- final MobileLedgerProfile currentProfile = Data.profile.getValue();
+ final MobileLedgerProfile currentProfile = Data.getProfile();
debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position,
- profile.getUuid(), (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
+ profile.getUuid(), currentProfile.getUuid()));
holder.itemView.setTag(profile);
int hue = profile.getThemeHue();
holder.mEditButton.setOnClickListener(mOnClickListener);
- final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile);
+ final boolean sameProfile = currentProfile.equals(profile);
holder.itemView
.setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
if (editingProfiles.getValue()) {
public static ObservableValue<String> updateError = new ObservableValue<>();
public static void scheduleTransactionListReload() {
- if (Data.profile.getValue() == null) return;
String filter = Data.accountFilter.getValue();
AsyncTask<String, Void, String> task = new UTT();
return result;
}
public static void setupTheme(Activity activity) {
- MobileLedgerProfile profile = Data.profile.getValue();
+ MobileLedgerProfile profile = Data.getProfile();
setupTheme(activity, profile);
}
- public static void setupTheme(Activity activity, MobileLedgerProfile profile) {
+ public static void setupTheme(Activity activity, @Nullable MobileLedgerProfile profile) {
final int themeHue = (profile == null) ? -1 : profile.getThemeHue();
setupTheme(activity, themeHue);
}
String sql;
String[] params;
if (profileSpecific) {
- MobileLedgerProfile p = (profile == null) ? Data.profile.getValue() : profile;
- if (p == null)
- throw new AssertionError();
+ MobileLedgerProfile p = (profile == null) ? Data.getProfile() : profile;
sql = String.format(
"SELECT rowid as _id, %s, CASE WHEN %s_upper LIKE ?||'%%' THEN 1 " +
"WHEN %s_upper LIKE '%%:'||?||'%%' then 2 " +