From 9e8a289a85a65d372b47ccd2c2261299972ae4dd Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 20 Jan 2019 14:08:33 +0000 Subject: [PATCH] a welcome screen directs to the new profile activity when there are no profiles defined --- .../async/UpdateTransactionsTask.java | 6 +- .../net/ktnx/mobileledger/model/Data.java | 11 + .../AccountSummaryViewModel.java | 2 + .../ui/activity/MainActivity.java | 70 +++--- .../ui/activity/ProfileListActivity.java | 23 +- .../ui/profiles/ProfileDetailFragment.java | 12 +- .../TransactionListViewModel.java | 2 + app/src/main/res/layout/activity_main.xml | 222 +++++++++--------- app/src/main/res/layout/no_profiles.xml | 66 ++++++ app/src/main/res/values/colors.xml | 2 +- app/src/main/res/values/strings.xml | 3 + 11 files changed, 258 insertions(+), 161 deletions(-) create mode 100644 app/src/main/res/layout/no_profiles.xml diff --git a/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java index dc02fe9f..0f3d022a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java @@ -24,6 +24,7 @@ import android.util.Log; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerTransaction; +import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.model.TransactionListItem; import net.ktnx.mobileledger.utils.Globals; import net.ktnx.mobileledger.utils.MLDB; @@ -35,7 +36,10 @@ import java.util.Date; public class UpdateTransactionsTask extends AsyncTask { protected String doInBackground(String[] filterAccName) { Data.backgroundTaskCount.incrementAndGet(); - String profile_uuid = Data.profile.get().getUuid(); + final MobileLedgerProfile profile = Data.profile.get(); + if (profile == null) return "Profile not configured"; + + String profile_uuid = profile.getUuid(); try { ArrayList newList = new ArrayList<>(); diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 62aecd62..ffad6473 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -24,6 +24,7 @@ import net.ktnx.mobileledger.utils.ObservableValue; import java.util.ArrayList; import java.util.Date; +import java.util.List; public final class Data { public static TransactionList transactions = new TransactionList(); @@ -39,4 +40,14 @@ public final class Data { MLDB.setOption(MLDB.OPT_PROFILE_UUID, newProfile.getUuid()); profile.set(newProfile); } + public static int getProfileIndex(MobileLedgerProfile profile) { + List list = profiles.getList(); + + for (int i = 0; i < list.size(); i++) { + MobileLedgerProfile p = list.get(i); + if (p.equals(profile)) return i; + } + + return -1; + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java index c3223cc9..2234d3cd 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java @@ -36,6 +36,8 @@ class AccountSummaryViewModel extends ViewModel { new CommitAccountsTaskParams(Data.accounts.get(), Data.optShowOnlyStarred.get())); } void scheduleAccountListReload() { + if (Data.profile.get() == null) return; + UAT task = new UAT(); task.execute(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index 1ccdf3ca..b230c1c1 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -18,7 +18,6 @@ package net.ktnx.mobileledger.ui.activity; import android.content.Intent; -import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.os.Build; import android.os.Bundle; @@ -97,8 +96,6 @@ public class MainActivity extends AppCompatActivity { }); }); - setupProfile(); - drawer = findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, @@ -164,53 +161,42 @@ public class MainActivity extends AppCompatActivity { } }); }); + + findViewById(R.id.btn_no_profiles_add).setOnClickListener(v -> startAddProfileActivity()); + } + @Override + protected void onResume() { + super.onResume(); + setupProfile(); + } + private void startAddProfileActivity() { + Intent intent = new Intent(this, ProfileListActivity.class); + Bundle args = new Bundle(); + args.putInt(ProfileListActivity.ARG_ACTION, ProfileListActivity.ACTION_EDIT_PROFILE); + args.putInt(ProfileListActivity.ARG_PROFILE_INDEX, ProfileListActivity.PROFILE_INDEX_NONE); + intent.putExtras(args); + startActivity(intent, args); } private void setupProfile() { String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null); MobileLedgerProfile profile; - if (profileUUID == null) { - if (Data.profiles.isEmpty()) { - Data.profiles.setList(MobileLedgerProfile.createInitialProfileList()); - profile = Data.profiles.get(0); - - SharedPreferences backend = getSharedPreferences("backend", MODE_PRIVATE); - Log.d("profiles", "Migrating from preferences to profiles"); - // migration to multiple profiles - if (profile.getUrl().isEmpty()) { - // no legacy config - Intent intent = new Intent(this, ProfileListActivity.class); - startActivity(intent); - } - profile.setUrl(backend.getString("backend_url", "")); - profile.setAuthEnabled(backend.getBoolean("backend_use_http_auth", false)); - profile.setAuthUserName(backend.getString("backend_auth_user", null)); - profile.setAuthPassword(backend.getString("backend_auth_password", null)); - profile.storeInDB(); - SharedPreferences.Editor editor = backend.edit(); - editor.clear(); - editor.apply(); - } - else profile = Data.profiles.get(0); - } - else { - profile = MobileLedgerProfile.loadAllFromDB(profileUUID); + profile = MobileLedgerProfile.loadAllFromDB(profileUUID); + + if (Data.profiles.getList().isEmpty()) { + findViewById(R.id.no_profiles_layout).setVisibility(View.VISIBLE); + findViewById(R.id.pager_layout).setVisibility(View.GONE); + return; } + findViewById(R.id.pager_layout).setVisibility(View.VISIBLE); + findViewById(R.id.no_profiles_layout).setVisibility(View.GONE); + if (profile == null) profile = Data.profiles.get(0); if (profile == null) throw new AssertionError("profile must have a value"); Data.setCurrentProfile(profile); - - if (profile.getUrl().isEmpty()) { - Intent intent = new Intent(this, ProfileListActivity.class); - Bundle args = new Bundle(); - args.putInt(ProfileListActivity.ARG_ACTION, ProfileListActivity.ACTION_EDIT_PROFILE); - args.putInt(ProfileListActivity.ARG_PROFILE_INDEX, 0); - intent.putExtras(args); - startActivity(intent, args); - } } public void fabNewTransactionClicked(View view) { Intent intent = new Intent(this, NewTransactionActivity.class); @@ -302,7 +288,9 @@ public class MainActivity extends AppCompatActivity { } public void updateLastUpdateTextFromDB() { { - long last_update = Data.profile.get().getLongOption(MLDB.OPT_LAST_SCRAPE, 0L); + final MobileLedgerProfile profile = Data.profile.get(); + long last_update = + (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0; Log.d("transactions", String.format("Last update = %d", last_update)); if (last_update == 0) { @@ -314,6 +302,8 @@ public class MainActivity extends AppCompatActivity { } } public void scheduleTransactionListRetrieval() { + if (Data.profile.get() == null) return; + retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this)); retrieveTransactionsTask.execute(); @@ -371,7 +361,7 @@ public class MainActivity extends AppCompatActivity { @Override public Fragment getItem(int position) { - Log.d("main", String.format("Switching to gragment %d", position)); + Log.d("main", String.format("Switching to fragment %d", position)); switch (position) { case 0: return new AccountSummaryFragment(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java index 42c832a8..98afa519 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java @@ -53,7 +53,8 @@ import java.util.Collections; public class ProfileListActivity extends AppCompatActivity { public static final String ARG_ACTION = "action"; - public static final String ARG_PROFILE_INDEX = "profile_uuid"; + public static final String ARG_PROFILE_INDEX = "profile_index"; + public static final int PROFILE_INDEX_NONE = -1; public static final int ACTION_EDIT_PROFILE = 1; public static final int ACTION_INVALID = -1; /** @@ -94,12 +95,17 @@ public class ProfileListActivity extends AppCompatActivity { int action = getIntent().getIntExtra(ARG_ACTION, ACTION_INVALID); if (action == ACTION_EDIT_PROFILE) { Log.d("profiles", "got edit profile action"); - int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, -1); - if (index >= 0) { - MobileLedgerProfile profile = Data.profiles.get(index); - ProfilesRecyclerViewAdapter adapter = - (ProfilesRecyclerViewAdapter) recyclerView.getAdapter(); - if (adapter != null) adapter.editProfile(recyclerView, profile); + int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, PROFILE_INDEX_NONE); + + MobileLedgerProfile profile = (index >= 0) ? Data.profiles.get(index) : null; + ProfilesRecyclerViewAdapter adapter = + (ProfilesRecyclerViewAdapter) recyclerView.getAdapter(); + if (adapter != null) { + adapter.editProfile(recyclerView, profile); + + // if invoked from the initial screen, get out so that when the new profile + // activity finishes the user i navigated to the main activity + if ((profile == null) && Data.profiles.getList().isEmpty()) finish(); } } } @@ -170,6 +176,7 @@ public class ProfileListActivity extends AppCompatActivity { else { Context context = view.getContext(); Intent intent = new Intent(context, ProfileDetailActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION); if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index); context.startActivity(intent); @@ -211,7 +218,7 @@ public class ProfileListActivity extends AppCompatActivity { final MobileLedgerProfile profile = Data.profiles.get(position); final MobileLedgerProfile currentProfile = Data.profile.get(); Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(), - currentProfile.getUuid())); + (currentProfile == null) ? "" : currentProfile.getUuid())); holder.itemView.setTag(profile); holder.mTitle.setText(profile.getName()); holder.mSubTitle.setText(profile.getUrl()); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index b66cc053..67f28439 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -18,9 +18,9 @@ package net.ktnx.mobileledger.ui.profiles; import android.app.Activity; -import android.content.Context; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; @@ -109,11 +109,13 @@ public class ProfileDetailFragment extends Fragment { } } } - @Override - public void onAttach(Context context) { - super.onAttach(context); - fab = ((Activity) context).findViewById(R.id.fab); + public void onActivityCreated(@Nullable Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + Activity context = getActivity(); + if (context == null) return; + + fab = context.findViewById(R.id.fab); fab.setOnClickListener(v -> { if (mProfile != null) { mProfile.setName(profileName.getText()); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java index 02affd86..d5b748bb 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java @@ -32,6 +32,8 @@ public class TransactionListViewModel extends ViewModel { public static ObservableValue updateError = new ObservableValue<>(); public static void scheduleTransactionListReload() { + if (Data.profile.get() == null) return; + String filter = TransactionListFragment.accountFilter.get(); AsyncTask task = new UTT(); task.execute(filter); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 43a1132a..de2857eb 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -40,134 +40,144 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - - - + android:layout_height="match_parent"> + + + android:layout_height="wrap_content" + android:orientation="vertical" + android:theme="@style/AppTheme" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> - + - + - + - + - + - + android:elevation="24dp" + android:orientation="horizontal"> - - + + + + - - - + android:gravity="center_vertical" + android:orientation="horizontal" + android:visibility="gone"> + + + + + - - + - + - + - - + + + + + + + + + + +