From: Damyan Ivanov Date: Thu, 6 Dec 2018 18:02:12 +0000 (+0000) Subject: hide submit fab when there is no enough account data X-Git-Tag: v0.3~331 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=707e3f7c480501bb80f60e17d4aeb8a9f5b96d27;p=mobile-ledger.git hide submit fab when there is no enough account data --- diff --git a/app/src/main/java/net/ktnx/mobileledger/NewTransactionActivity.java b/app/src/main/java/net/ktnx/mobileledger/NewTransactionActivity.java index 6709b7d9..32cc8ab8 100644 --- a/app/src/main/java/net/ktnx/mobileledger/NewTransactionActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/NewTransactionActivity.java @@ -1,7 +1,5 @@ package net.ktnx.mobileledger; -import android.animation.AnimatorInflater; -import android.animation.AnimatorSet; import android.annotation.TargetApi; import android.database.Cursor; import android.database.MatrixCursor; @@ -16,7 +14,9 @@ import android.support.design.widget.Snackbar; import android.support.v4.app.DialogFragment; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.text.Editable; import android.text.InputType; +import android.text.TextWatcher; import android.util.Log; import android.util.TypedValue; import android.view.Menu; @@ -54,10 +54,50 @@ import java.util.Objects; public class NewTransactionActivity extends AppCompatActivity implements TaskCallback { private TableLayout table; private FloatingActionButton fab; + boolean fab_should_be_visible; private ProgressBar progress; private TextView text_date; private TextView text_descr; private static SaveTransactionTask saver; + FloatingActionButton.OnVisibilityChangedListener fab_visibility_changed_listener = new FloatingActionButton.OnVisibilityChangedListener() { + @Override + public void onShown(FloatingActionButton fab) { + Log.d("visuals", "FAB shown"); + super.onShown(fab); + if (!fab_should_be_visible) fab.hide(); + } + + @Override + public void onHidden(FloatingActionButton fab) { + Log.d("visuals", "FAB hidden"); + fab.setImageResource(R.drawable.ic_save_white_24dp); + fab.setEnabled(true); +// super.onHidden(fab); + if (fab_should_be_visible) fab.show(); + } + }; + + private void hide_fab() { + hide_fab(false); + } + + private void hide_fab(boolean force) { + if (!fab_should_be_visible && !force) return; + + fab_should_be_visible = false; + fab.hide(fab_visibility_changed_listener); + } + + private void show_fab() { + show_fab(false); + } + + private void show_fab(boolean force) { + if (fab_should_be_visible && !force) return; + + fab_should_be_visible = true; + fab.show(fab_visibility_changed_listener); + } @Override protected void onCreate(Bundle savedInstanceState) { @@ -85,8 +125,13 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); table = findViewById(R.id.new_transaction_accounts_table); for (int i = 0; i < table.getChildCount(); i++) { - hook_swipe_listener((TableRow)table.getChildAt(i)); - hook_autocompletion_adapter((TableRow)table.getChildAt(i)); + TableRow row = (TableRow) table.getChildAt(i); + TextView acc_name_view = (TextView) row.getChildAt(0); + TextView amount_view = (TextView) row.getChildAt(1); + hook_swipe_listener(row); + hook_autocompletion_adapter(row); + hook_text_change_listener(acc_name_view); + hook_text_change_listener(amount_view); // Log.d("swipe", "hooked to row "+i); } } @@ -137,6 +182,27 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal }); } + private void hook_text_change_listener(final TextView view) { + view.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + + @Override + public void afterTextChanged(Editable s) { +// Log.d("input", "text changed"); + check_transaction_submittable(); + } + }); + + } + @TargetApi(Build.VERSION_CODES.N) private void hook_autocompletion_adapter(final TableRow row) { String[] from = {"name"}; @@ -221,31 +287,55 @@ public class NewTransactionActivity extends AppCompatActivity implements TaskCal hook_swipe_listener(row); hook_autocompletion_adapter(row); + hook_text_change_listener(acc); + hook_text_change_listener(amt); + } + + private void check_transaction_submittable() { + TableLayout table = findViewById(R.id.new_transaction_accounts_table); + int accounts = 0; + int accounts_with_values = 0; + for( int i = 0; i < table.getChildCount(); i++ ) { + TableRow row = (TableRow) table.getChildAt(i); + + TextView acc_name_v = (TextView) row.getChildAt(0); + + String acc_name = String.valueOf(acc_name_v.getText()); + acc_name = acc_name.trim(); + if (!acc_name.isEmpty()) { + accounts++; + + TextView amount_v = (TextView) row.getChildAt(1); + String amt = String.valueOf(amount_v.getText()); + + if (!amt.isEmpty()) accounts_with_values++; + } + + if ((accounts >= 2) && (accounts_with_values >= (accounts - 1))) { + show_fab(); + return; + } + } + + hide_fab(); } @Override public void done() { fab.setImageResource(R.drawable.ic_check_white_24dp); - fab.setEnabled(true); + progress.setVisibility(View.INVISIBLE); + Log.d("visuals", "hiding progress"); - AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.new_trans_animation); - set.setTarget(fab); - set.start(); - final Handler at_fade_out = new Handler(); - at_fade_out.postDelayed(new Runnable() { + fab_should_be_visible = false; + final Handler fade_out = new Handler(); + fade_out.postDelayed(new Runnable() { @Override public void run() { + Log.d("visuals", "hiding FAB"); - final Handler at_fade_in = new Handler(); - at_fade_in.postDelayed(new Runnable() { - @Override - public void run() { - fab.setImageResource(R.drawable.ic_save_white_24dp); - } - }, 1000); + hide_fab(true); } - }, 500); - progress.setVisibility(View.INVISIBLE); + }, 1000); reset_form(); } diff --git a/app/src/main/res/animator/new_trans_animation.xml b/app/src/main/res/animator/new_trans_animation.xml index 408e7e68..0cfc1e26 100644 --- a/app/src/main/res/animator/new_trans_animation.xml +++ b/app/src/main/res/animator/new_trans_animation.xml @@ -1,11 +1,10 @@ + android:ordering="together"> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_new_transaction.xml b/app/src/main/res/layout/activity_new_transaction.xml index db7df84b..0db12f2f 100644 --- a/app/src/main/res/layout/activity_new_transaction.xml +++ b/app/src/main/res/layout/activity_new_transaction.xml @@ -32,6 +32,7 @@ android:layout_width="80dp" android:layout_height="80dp" android:layout_margin="4dp" + android:indeterminate="true" android:visibility="invisible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> @@ -43,6 +44,7 @@ android:layout_gravity="center" android:padding="@dimen/fab_margin" android:tint="@android:color/white" + android:visibility="visible" app:backgroundTint="@color/colorPrimary" app:layout_constraintBottom_toBottomOf="@id/save_transaction_progress" app:layout_constraintEnd_toEndOf="@id/save_transaction_progress"