From 99c3bfb3451ebb1fc55d728d8d1741849cf789db Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Thu, 4 Apr 2019 14:35:06 +0300 Subject: [PATCH] add app shortcuts (Android 7.1+) for adding new transaction for top profiles --- .../async/SendTransactionTask.java | 10 +++--- .../ui/activity/MainActivity.java | 31 ++++++++++++++++++- .../ui/activity/NewTransactionActivity.java | 16 ++++++++-- .../ui/activity/ProfileThemedActivity.java | 11 +++++-- 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java index fa69f86c..b6701259 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java @@ -24,9 +24,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import net.ktnx.mobileledger.json.ParsedLedgerTransaction; -import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerTransaction; import net.ktnx.mobileledger.model.LedgerTransactionAccount; +import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.utils.Globals; import net.ktnx.mobileledger.utils.NetworkUtil; import net.ktnx.mobileledger.utils.UrlEncodedFormData; @@ -52,12 +52,14 @@ public class SendTransactionTask extends AsyncTask shortcuts = new ArrayList<>(); + try (LockHolder lh = Data.profiles.lockForReading()) { + for (int i = 0; i < Data.profiles.size(); i++) { + MobileLedgerProfile p = Data.profiles.get(i); + if (!p.isPostingPermitted()) continue; + + ShortcutInfo si = new ShortcutInfo.Builder(this, "new_transaction_" + p.getUuid()) + .setShortLabel(p.getName()) + .setIcon(Icon.createWithResource(this, R.drawable.svg_thick_plus_white)) + .setIntent(new Intent(Intent.ACTION_VIEW, null, this, + NewTransactionActivity.class).putExtra("profile_uuid", p.getUuid())) + .setRank(i) + .build(); + shortcuts.add(si); + } + } + ShortcutManager sm = getSystemService(ShortcutManager.class); + sm.setDynamicShortcuts(shortcuts); + } private void onProfileListChanged(Object arg) { findViewById(R.id.nav_profile_list).setMinimumHeight( (int) (getResources().getDimension(R.dimen.thumb_row_height) * @@ -303,6 +330,8 @@ public class MainActivity extends ProfileThemedActivity { Log.d("profiles", "profile list changed"); if (arg == null) mProfileListAdapter.notifyDataSetChanged(); else mProfileListAdapter.notifyItemChanged((int) arg); + + createShortcuts(); } private void onProfileChanged(Object arg) { MobileLedgerProfile profile = Data.profile.get(); @@ -669,7 +698,7 @@ public class MainActivity extends ProfileThemedActivity { } public void onAccountSummaryRowViewClicked(View view) { ViewGroup row; - if ( view.getId() == R.id.account_expander ) row = (ViewGroup) view.getParent().getParent(); + if (view.getId() == R.id.account_expander) row = (ViewGroup) view.getParent().getParent(); else row = (ViewGroup) view.getParent(); LedgerAccount acc = (LedgerAccount) row.getTag(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java index 845afea5..b4716075 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java @@ -93,7 +93,7 @@ public class NewTransactionActivity extends ProfileThemedActivity setContentView(R.layout.activity_new_transaction); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - toolbar.setSubtitle(Data.profile.get().getName()); + toolbar.setSubtitle(mProfile.getName()); tvDate = findViewById(R.id.new_transaction_date); tvDate.setOnFocusChangeListener((v, hasFocus) -> { @@ -122,7 +122,16 @@ public class NewTransactionActivity extends ProfileThemedActivity // Log.d("swipe", "hooked to row "+i); } } + @Override + protected void initProfile() { + String profileUUID = getIntent().getStringExtra("profile_uuid"); + if (profileUUID != null) { + mProfile = Data.getProfile(profileUUID); + if (mProfile == null) finish(); + } + else super.initProfile(); + } @Override public void finish() { super.finish(); @@ -149,7 +158,7 @@ public class NewTransactionActivity extends ProfileThemedActivity progress.setVisibility(View.VISIBLE); try { - saver = new SendTransactionTask(this); + saver = new SendTransactionTask(this, mProfile); String dateString = tvDate.getText().toString(); Date date; @@ -180,7 +189,8 @@ public class NewTransactionActivity extends ProfileThemedActivity tr.addAccount(item); } - if (emptyAmountAccount != null) emptyAmountAccount.setAmount(-emptyAmountAccountBalance); + if (emptyAmountAccount != null) + emptyAmountAccount.setAmount(-emptyAmountAccountBalance); saver.execute(tr); } catch (ParseException e) { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java index 36e937d1..fb6db900 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java @@ -20,14 +20,17 @@ package net.ktnx.mobileledger.ui.activity; import android.annotation.SuppressLint; import android.os.Bundle; +import net.ktnx.mobileledger.model.Data; +import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.utils.Colors; import androidx.annotation.Nullable; @SuppressLint("Registered") public class ProfileThemedActivity extends CrashReportingActivity { + protected MobileLedgerProfile mProfile; protected void setupProfileColors() { - Colors.setupTheme(this); + Colors.setupTheme(this, mProfile); } @Override protected void onStart() { @@ -35,7 +38,11 @@ public class ProfileThemedActivity extends CrashReportingActivity { Colors.refreshColors(getTheme()); } protected void onCreate(@Nullable Bundle savedInstanceState) { + initProfile(); super.onCreate(savedInstanceState); - Colors.setupTheme(this); + setupProfileColors(); + } + protected void initProfile() { + mProfile = Data.profile.get(); } } -- 2.39.2