Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 1be7fed

Browse files
author
Evgenii Kanivets
authored
Merge pull request #158 from evgenii-kanivets/feature-134_account_goal
Account goal.
2 parents 3bb5ab2 + ea19a2c commit 1be7fed

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/edit/fragment/EditAccountFragment.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@ import android.view.View
77
import com.blogspot.e_kanivets.moneytracker.R
88
import com.blogspot.e_kanivets.moneytracker.R.layout
99
import com.blogspot.e_kanivets.moneytracker.activity.base.BaseFragment
10+
import com.blogspot.e_kanivets.moneytracker.controller.FormatController
1011
import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController
1112
import com.blogspot.e_kanivets.moneytracker.entity.data.Account
13+
import com.blogspot.e_kanivets.moneytracker.util.AnswersProxy
14+
import com.blogspot.e_kanivets.moneytracker.util.validator.EditAccountValidator
15+
import com.blogspot.e_kanivets.moneytracker.util.validator.IValidator
1216
import kotlinx.android.synthetic.main.fragment_edit_account.*
1317
import javax.inject.Inject
1418

1519
class EditAccountFragment : BaseFragment() {
1620

1721
@Inject
1822
internal lateinit var accountController: AccountController
23+
@Inject
24+
internal lateinit var formatController: FormatController
25+
26+
private lateinit var accountValidator: IValidator<Account>
1927

2028
private lateinit var account: Account
2129

@@ -28,25 +36,28 @@ class EditAccountFragment : BaseFragment() {
2836

2937
override fun initViews(view: View) {
3038
etTitle.setText(account.title)
31-
etGoal.setText(account.goal.toString())
39+
etGoal.setText(formatController.formatPrecisionNone(account.goal))
3240
viewColor.setBackgroundColor(account.color)
3341

3442
val fabDone = view.rootView.findViewById<FloatingActionButton>(R.id.fabDone)
3543
fabDone.setOnClickListener { done() }
44+
45+
accountValidator = EditAccountValidator(context!!, view)
3646
}
3747

3848
private fun done() {
39-
val title = etTitle.text.toString().trim { it <= ' ' }
49+
AnswersProxy.get().logButton("Edit Account")
50+
if (accountValidator.validate()) {
51+
val title = etTitle.text.toString().trim { it <= ' ' }
52+
val goal = etGoal.text.toString().toDouble()
4053

41-
if (title.isEmpty()) {
42-
tilTitle.error = getString(R.string.field_cant_be_empty)
43-
} else {
4454
val newAccount = Account(
4555
account.id, title, account.curSum.toDouble(),
46-
account.currency, account.goal, account.isArchived, account.color
56+
account.currency, goal, account.isArchived, account.color
4757
)
4858
val updated = accountController.update(newAccount) != null
4959
if (updated) {
60+
AnswersProxy.get().logEvent("Edit Account")
5061
activity?.setResult(Activity.RESULT_OK)
5162
activity?.finish()
5263
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.blogspot.e_kanivets.moneytracker.util.validator;
2+
3+
import android.content.Context;
4+
import android.support.annotation.NonNull;
5+
import android.support.design.widget.TextInputLayout;
6+
import android.view.View;
7+
import android.widget.EditText;
8+
import butterknife.BindView;
9+
import butterknife.ButterKnife;
10+
import com.blogspot.e_kanivets.moneytracker.R;
11+
import com.blogspot.e_kanivets.moneytracker.entity.data.Account;
12+
13+
/**
14+
* Util class for EditAccount validation.
15+
* Created on 16.09.2018.
16+
*
17+
* @author Evgenii Kanivets
18+
*/
19+
20+
public class EditAccountValidator implements IValidator<Account> {
21+
22+
@NonNull private final Context context;
23+
24+
@BindView(R.id.tilTitle) TextInputLayout tilTitle;
25+
@BindView(R.id.etTitle) EditText etTitle;
26+
@BindView(R.id.tilGoal) TextInputLayout tilGoal;
27+
@BindView(R.id.etGoal) EditText etGoal;
28+
29+
public EditAccountValidator(@NonNull Context context, @NonNull View view) {
30+
this.context = context;
31+
ButterKnife.bind(this, view);
32+
initTextWatchers();
33+
}
34+
35+
@Override public boolean validate() {
36+
String title = etTitle.getText().toString().trim();
37+
double goal = Double.MAX_VALUE;
38+
39+
try {
40+
goal = Double.parseDouble(etGoal.getText().toString().trim());
41+
} catch (NumberFormatException e) {
42+
e.printStackTrace();
43+
}
44+
45+
boolean valid = true;
46+
47+
if (title.isEmpty()) {
48+
tilTitle.setError(context.getString(R.string.field_cant_be_empty));
49+
valid = false;
50+
}
51+
52+
if (goal == Double.MAX_VALUE) {
53+
tilGoal.setError(context.getString(R.string.field_cant_be_empty));
54+
goal = 0;
55+
valid = false;
56+
}
57+
58+
if (Math.abs(goal) > MAX_ABS_VALUE) {
59+
tilGoal.setError(context.getString(R.string.too_rich_or_poor));
60+
valid = false;
61+
}
62+
63+
return valid;
64+
}
65+
66+
private void initTextWatchers() {
67+
etTitle.addTextChangedListener(new ClearErrorTextWatcher(tilTitle));
68+
etGoal.addTextChangedListener(new ClearErrorTextWatcher(tilGoal));
69+
}
70+
}

app/src/main/res/layout/fragment_edit_account.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
</android.support.design.widget.TextInputLayout>
2828

2929
<android.support.design.widget.TextInputLayout
30-
android:id="@+id/til_goal"
30+
android:id="@+id/tilGoal"
3131
android:layout_width="match_parent"
3232
android:layout_height="wrap_content"
33-
android:visibility="invisible"
3433
>
3534

3635
<EditText

0 commit comments

Comments
 (0)