diff --git a/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/AccountRepoTest.java b/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/AccountRepoTest.java index da5567f..74252b1 100644 --- a/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/AccountRepoTest.java +++ b/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/AccountRepoTest.java @@ -5,7 +5,6 @@ import com.blogspot.e_kanivets.moneytracker.entity.data.Account; import com.blogspot.e_kanivets.moneytracker.repo.DbHelper; -import com.blogspot.e_kanivets.moneytracker.repo.data.AccountRepo; import junit.framework.TestCase; @@ -41,12 +40,13 @@ public void testGetTable() throws Exception { } public void testContentValues() throws Exception { - Account account = new Account(-1, "title1", 100, "NON"); + Account account = new Account(-1, "title1", 100, "NON", 30); ContentValues expected = new ContentValues(); expected.put(DbHelper.TITLE_COLUMN, "title1"); expected.put(DbHelper.CUR_SUM_COLUMN, 100); expected.put(DbHelper.CURRENCY_COLUMN, "NON"); + expected.put(DbHelper.DECIMALS_COLUMN, 30); ContentValues actual = repo.contentValues(account); @@ -71,7 +71,7 @@ public void testGetListFromCursor() throws Exception { Mockito.when(mockCursor.getString(4)).thenReturn("NON"); List expected = new ArrayList<>(); - expected.add(new Account(1, "title", 100, "NON")); + expected.add(new Account(1, "title", 100, "NON", 0)); assertEquals(expected, repo.getListFromCursor(mockCursor)); diff --git a/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/RecordRepoTest.java b/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/RecordRepoTest.java index 567c0d1..ac23b6a 100644 --- a/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/RecordRepoTest.java +++ b/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/RecordRepoTest.java @@ -5,7 +5,6 @@ import com.blogspot.e_kanivets.moneytracker.entity.data.Record; import com.blogspot.e_kanivets.moneytracker.repo.DbHelper; -import com.blogspot.e_kanivets.moneytracker.repo.data.RecordRepo; import junit.framework.TestCase; @@ -41,16 +40,17 @@ public void testGetTable() throws Exception { } public void testContentValues() throws Exception { - Record record = new Record(1, 1, Record.TYPE_EXPENSE, "title", 1, 100, 1, "NON"); + Record record = new Record(1, 1, Record.TYPE_EXPENSE, "title", 1, 100, 1, "NON", 50); ContentValues expected = new ContentValues(); expected.put(DbHelper.TIME_COLUMN, 1L); expected.put(DbHelper.TYPE_COLUMN, Record.TYPE_EXPENSE); expected.put(DbHelper.TITLE_COLUMN, "title"); expected.put(DbHelper.CATEGORY_ID_COLUMN, 1L); - expected.put(DbHelper.PRICE_COLUMN, 100.0); + expected.put(DbHelper.PRICE_COLUMN, 100); expected.put(DbHelper.ACCOUNT_ID_COLUMN, 1L); expected.put(DbHelper.CURRENCY_COLUMN, "NON"); + expected.put(DbHelper.DECIMALS_COLUMN, 50); ContentValues actual = repo.contentValues(record); @@ -72,6 +72,7 @@ public void testGetListFromCursor() throws Exception { Mockito.when(mockCursor.getColumnIndex(DbHelper.PRICE_COLUMN)).thenReturn(6); Mockito.when(mockCursor.getColumnIndex(DbHelper.ACCOUNT_ID_COLUMN)).thenReturn(7); Mockito.when(mockCursor.getColumnIndex(DbHelper.CURRENCY_COLUMN)).thenReturn(8); + Mockito.when(mockCursor.getColumnIndex(DbHelper.DECIMALS_COLUMN)).thenReturn(9); Mockito.when(mockCursor.getLong(1)).thenReturn(1L); Mockito.when(mockCursor.getLong(2)).thenReturn(1L); @@ -81,9 +82,10 @@ public void testGetListFromCursor() throws Exception { Mockito.when(mockCursor.getInt(6)).thenReturn(100); Mockito.when(mockCursor.getLong(7)).thenReturn(1L); Mockito.when(mockCursor.getString(8)).thenReturn("NON"); + Mockito.when(mockCursor.getInt(9)).thenReturn(50); List expected = new ArrayList<>(); - expected.add(new Record(1, 1, Record.TYPE_EXPENSE, "title", 1, 100, 1, "NON")); + expected.add(new Record(1, 1, Record.TYPE_EXPENSE, "title", 1, 100, 1, "NON", 50)); assertEquals(expected, repo.getListFromCursor(mockCursor)); diff --git a/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/TransferRepoTest.java b/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/TransferRepoTest.java index b926cfc..9234100 100644 --- a/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/TransferRepoTest.java +++ b/app/src/androidTest/java/com/blogspot/e_kanivets/moneytracker/repo/data/TransferRepoTest.java @@ -5,7 +5,6 @@ import com.blogspot.e_kanivets.moneytracker.entity.data.Transfer; import com.blogspot.e_kanivets.moneytracker.repo.DbHelper; -import com.blogspot.e_kanivets.moneytracker.repo.data.TransferRepo; import junit.framework.TestCase; @@ -41,7 +40,7 @@ public void testGetTable() throws Exception { } public void testContentValues() throws Exception { - Transfer transfer = new Transfer(1, 1, 1, 2, 100, 200); + Transfer transfer = new Transfer(1, 1, 1, 2, 100, 200, 45, 50); ContentValues expected = new ContentValues(); expected.put(DbHelper.TIME_COLUMN, 1L); @@ -49,6 +48,8 @@ public void testContentValues() throws Exception { expected.put(DbHelper.TO_ACCOUNT_ID_COLUMN, 2L); expected.put(DbHelper.FROM_AMOUNT_COLUMN, 100); expected.put(DbHelper.TO_AMOUNT_COLUMN, 200); + expected.put(DbHelper.DECIMALS_FROM_COLUMN, 45); + expected.put(DbHelper.DECIMALS_TO_COLUMN, 50); ContentValues actual = repo.contentValues(transfer); @@ -68,6 +69,8 @@ public void testGetListFromCursor() throws Exception { Mockito.when(mockCursor.getColumnIndex(DbHelper.TO_ACCOUNT_ID_COLUMN)).thenReturn(4); Mockito.when(mockCursor.getColumnIndex(DbHelper.FROM_AMOUNT_COLUMN)).thenReturn(5); Mockito.when(mockCursor.getColumnIndex(DbHelper.TO_AMOUNT_COLUMN)).thenReturn(6); + Mockito.when(mockCursor.getColumnIndex(DbHelper.DECIMALS_FROM_COLUMN)).thenReturn(7); + Mockito.when(mockCursor.getColumnIndex(DbHelper.DECIMALS_TO_COLUMN)).thenReturn(8); Mockito.when(mockCursor.getLong(1)).thenReturn(1L); Mockito.when(mockCursor.getLong(2)).thenReturn(1L); @@ -75,9 +78,11 @@ public void testGetListFromCursor() throws Exception { Mockito.when(mockCursor.getLong(4)).thenReturn(2L); Mockito.when(mockCursor.getInt(5)).thenReturn(100); Mockito.when(mockCursor.getInt(6)).thenReturn(200); + Mockito.when(mockCursor.getInt(7)).thenReturn(45); + Mockito.when(mockCursor.getInt(8)).thenReturn(50); List expected = new ArrayList<>(); - expected.add(new Transfer(1, 1, 1, 2, 100, 200)); + expected.add(new Transfer(1, 1, 1, 2, 100, 200, 45, 50)); assertEquals(expected, repo.getListFromCursor(mockCursor)); diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/SettingsActivity.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/SettingsActivity.java index 22a4cd7..7316a13 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/SettingsActivity.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/SettingsActivity.java @@ -8,6 +8,8 @@ import com.blogspot.e_kanivets.moneytracker.MtApp; import com.blogspot.e_kanivets.moneytracker.R; import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity; +import com.blogspot.e_kanivets.moneytracker.controller.FormatController; +import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController; import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController; import com.blogspot.e_kanivets.moneytracker.controller.CurrencyController; import com.blogspot.e_kanivets.moneytracker.entity.data.Account; @@ -41,6 +43,8 @@ public static class SettingsFragment extends PreferenceFragment { AccountController accountController; @Inject CurrencyController currencyController; + @Inject + PreferenceController preferenceController; @Override public void onCreate(Bundle savedInstanceState) { @@ -53,43 +57,62 @@ public void onCreate(Bundle savedInstanceState) { setupDefaultAccountPref(); setupDefaultCurrencyPref(); + setupDisplayPrecision(); } private void setupDefaultAccountPref() { ListPreference defaultAccountPref = (ListPreference) findPreference(getString(R.string.pref_default_account)); - defaultAccountPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - getActivity().setResult(RESULT_OK); - return true; - } - }); + defaultAccountPref.setOnPreferenceChangeListener(preferenceChangeListener); List accountList = accountController.readAll(); - - if (accountList.size() > 0) - defaultAccountPref.setDefaultValue(Long.toString(accountList.get(0).getId())); defaultAccountPref.setEntries(getEntries(accountList)); defaultAccountPref.setEntryValues(getEntryValues(accountList)); + + Account defaultAccount = accountController.readDefaultAccount(); + if (defaultAccount == null) { + defaultAccountPref.setDefaultValue(""); + defaultAccountPref.setSummary(""); + } else { + defaultAccountPref.setDefaultValue(defaultAccount.getTitle()); + defaultAccountPref.setSummary(defaultAccount.getTitle()); + } } @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument") private void setupDefaultCurrencyPref() { ListPreference defaultCurrencyPref = (ListPreference) findPreference(getString(R.string.pref_default_currency)); - defaultCurrencyPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - getActivity().setResult(RESULT_OK); - return true; - } - }); + defaultCurrencyPref.setOnPreferenceChangeListener(preferenceChangeListener); List currencyList = currencyController.readAll(); + defaultCurrencyPref.setEntries(currencyList.toArray(new String[0])); + defaultCurrencyPref.setEntryValues(currencyList.toArray(new String[0])); + String defaultCurrency = currencyController.readDefaultCurrency(); defaultCurrencyPref.setDefaultValue(defaultCurrency); + defaultCurrencyPref.setSummary(defaultCurrency); + } - defaultCurrencyPref.setEntries(currencyList.toArray(new String[0])); - defaultCurrencyPref.setEntryValues(currencyList.toArray(new String[0])); + @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument") + private void setupDisplayPrecision() { + ListPreference displayPrecisionPref = (ListPreference) findPreference(getString(R.string.pref_display_precision)); + displayPrecisionPref.setOnPreferenceChangeListener(preferenceChangeListener); + + List precisionListValues = new ArrayList<>(); + precisionListValues.add(FormatController.PRECISION_MATH); + precisionListValues.add(FormatController.PRECISION_INT); + precisionListValues.add(FormatController.PRECISION_NONE); + displayPrecisionPref.setEntryValues(precisionListValues.toArray(new String[0])); + + List precisionList = new ArrayList<>(); + precisionList.add(getString(R.string.precision_math)); + precisionList.add(getString(R.string.precision_int)); + precisionList.add(getString(R.string.precision_none)); + displayPrecisionPref.setEntries(precisionList.toArray(new String[0])); + + if (FormatController.PRECISION_MATH.equals(preferenceController.readDisplayPrecision())) { + displayPrecisionPref.setDefaultValue(getString(R.string.precision_math)); + displayPrecisionPref.setSummary(getString(R.string.precision_math)); + } } @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument") @@ -113,5 +136,17 @@ private String[] getEntryValues(List accountList) { return result.toArray(new String[0]); } + + private Preference.OnPreferenceChangeListener preferenceChangeListener + = new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + // Previously we could set summary to default value, + // but now it's needed to display selected entry + preference.setSummary("%s"); + getActivity().setResult(RESULT_OK); + return true; + } + }; } } diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/AddAccountActivity.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/AddAccountActivity.java index aa09f84..8fbfaaa 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/AddAccountActivity.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/AddAccountActivity.java @@ -78,11 +78,10 @@ public boolean onOptionsItemSelected(MenuItem item) { private void addAccount() { String title = etTitle.getText().toString().trim(); - int initSum = Integer.parseInt(etInitSum.getText().toString().trim()); + double initSum = Double.parseDouble(etInitSum.getText().toString().trim()); String currency = (String) spinner.getSelectedItem(); Account account = new Account(title, initSum, currency); - accountController.create(account); } } \ No newline at end of file diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/TransferActivity.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/TransferActivity.java index a7669cc..9d627e8 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/TransferActivity.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/account/TransferActivity.java @@ -94,16 +94,16 @@ private void doTransfer() { Account fromAccount = accountList.get(spinnerFrom.getSelectedItemPosition()); Account toAccount = accountList.get(spinnerTo.getSelectedItemPosition()); - int fromAmount = -1; + double fromAmount = -1; try { - fromAmount = Integer.parseInt(etFromAmount.getText().toString()); + fromAmount = Double.parseDouble(etFromAmount.getText().toString()); } catch (NumberFormatException e) { e.printStackTrace(); } - int toAmount = -1; + double toAmount = -1; try { - toAmount = Integer.parseInt(etToAmount.getText().toString()); + toAmount = Double.parseDouble(etToAmount.getText().toString()); } catch (NumberFormatException e) { e.printStackTrace(); } diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/AddRecordActivity.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/AddRecordActivity.java index 1af95e3..eb14cd8 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/AddRecordActivity.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/AddRecordActivity.java @@ -21,6 +21,7 @@ import com.blogspot.e_kanivets.moneytracker.R; import com.blogspot.e_kanivets.moneytracker.activity.base.BaseBackActivity; import com.blogspot.e_kanivets.moneytracker.adapter.CategoryAutoCompleteAdapter; +import com.blogspot.e_kanivets.moneytracker.controller.FormatController; import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController; import com.blogspot.e_kanivets.moneytracker.controller.data.CategoryController; import com.blogspot.e_kanivets.moneytracker.controller.data.RecordController; @@ -62,6 +63,8 @@ public class AddRecordActivity extends BaseBackActivity { RecordController recordController; @Inject AccountController accountController; + @Inject + FormatController formatController; @Bind(R.id.et_title) EditText etTitle; @@ -96,11 +99,11 @@ record = getIntent().getParcelableExtra(KEY_RECORD); protected void initViews() { super.initViews(); - //Add texts to dialog if it's edit dialog + // Add texts to dialog if it's edit dialog if (mode == Mode.MODE_EDIT) { etTitle.setText(record.getTitle()); if (record.getCategory() != null) etCategory.setText(record.getCategory().getName()); - etPrice.setText(Integer.toString((int) record.getPrice())); + etPrice.setText(formatController.formatAmount(record.getFullPrice())); } presentSpinnerAccount(); @@ -221,14 +224,14 @@ private boolean prepareRecord() { //Check if price is valid //noinspection UnusedAssignment - int price = -1; + double price = -1; try { - price = Integer.parseInt(etPrice.getText().toString()); + price = Double.parseDouble(etPrice.getText().toString()); } catch (NumberFormatException e) { e.printStackTrace(); } - if (price >= 0 && price <= 1000000000 && spinnerAccount.getSelectedItemPosition() >= 0) { + if (price >= 0.0 && price <= 1000000000.0 && spinnerAccount.getSelectedItemPosition() >= 0) { Account account = null; if (spinnerAccount.isEnabled()) account = accountList.get(spinnerAccount.getSelectedItemPosition()); @@ -237,7 +240,7 @@ private boolean prepareRecord() { } else return false; } - private boolean doRecord(String title, String category, int price, @Nullable Account account) { + private boolean doRecord(String title, String category, double price, @Nullable Account account) { if (account == null) return false; if (mode == Mode.MODE_ADD) { @@ -267,4 +270,4 @@ private boolean doRecord(String title, String category, int price, @Nullable Acc } public enum Mode {MODE_ADD, MODE_EDIT} -} \ No newline at end of file +} diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/MainActivity.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/MainActivity.java index 9177242..9496484 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/MainActivity.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/record/MainActivity.java @@ -15,6 +15,7 @@ import com.blogspot.e_kanivets.moneytracker.activity.ReportActivity; import com.blogspot.e_kanivets.moneytracker.activity.base.BaseDrawerActivity; import com.blogspot.e_kanivets.moneytracker.adapter.RecordAdapter; +import com.blogspot.e_kanivets.moneytracker.controller.FormatController; import com.blogspot.e_kanivets.moneytracker.controller.PeriodController; import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController; import com.blogspot.e_kanivets.moneytracker.controller.data.ExchangeRateController; @@ -59,6 +60,8 @@ public class MainActivity extends BaseDrawerActivity { PreferenceController preferenceController; @Inject PeriodController periodController; + @Inject + FormatController formatController; private ShortSummaryPresenter summaryPresenter; @@ -228,7 +231,7 @@ private void fillDefaultAccount() { if (defaultAccount == null) return; tvDefaultAccountTitle.setText(defaultAccount.getTitle()); - tvDefaultAccountSum.setText(Integer.toString(defaultAccount.getCurSum())); + tvDefaultAccountSum.setText(formatController.formatAmount(defaultAccount.getFullSum())); tvCurrency.setText(defaultAccount.getCurrency()); } } diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/AccountAdapter.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/AccountAdapter.java index 586a334..3e5d827 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/AccountAdapter.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/AccountAdapter.java @@ -8,11 +8,14 @@ import android.widget.BaseAdapter; import android.widget.TextView; +import com.blogspot.e_kanivets.moneytracker.MtApp; import com.blogspot.e_kanivets.moneytracker.R; +import com.blogspot.e_kanivets.moneytracker.controller.FormatController; import com.blogspot.e_kanivets.moneytracker.entity.data.Account; import java.util.List; -import java.util.Locale; + +import javax.inject.Inject; import butterknife.Bind; import butterknife.ButterKnife; @@ -24,6 +27,9 @@ * @author Evgenii Kanivets */ public class AccountAdapter extends BaseAdapter { + @Inject + FormatController formatController; + private Context context; private List accounts; @@ -34,6 +40,8 @@ public class AccountAdapter extends BaseAdapter { @SuppressWarnings("deprecation") public AccountAdapter(Context context, List accounts) { + MtApp.get().getAppComponent().inject(AccountAdapter.this); + this.context = context; this.accounts = accounts; @@ -74,22 +82,18 @@ public View getView(final int position, View convertView, ViewGroup parent) { Account account = accounts.get(position); - convertView.setBackgroundColor(account.getCurSum() >= 0 ? whiteGreen : whiteRed); + convertView.setBackgroundColor(account.getFullSum() >= 0.0 ? whiteGreen : whiteRed); - viewHolder.tvCurSum.setTextColor(account.getCurSum() >= 0 ? green : red); - viewHolder.tvCurrency.setTextColor(account.getCurSum() >= 0 ? green : red); + viewHolder.tvCurSum.setTextColor(account.getFullSum() >= 0.0 ? green : red); + viewHolder.tvCurrency.setTextColor(account.getFullSum() >= 0.0 ? green : red); viewHolder.tvTitle.setText(account.getTitle()); - viewHolder.tvCurSum.setText(format(account.getCurSum())); + viewHolder.tvCurSum.setText(formatController.formatSignedAmount(account.getFullSum())); viewHolder.tvCurrency.setText(account.getCurrency()); return convertView; } - private String format(double amount) { - return (amount >= 0 ? "+ " : "- ") + String.format(Locale.getDefault(), "%.0f", Math.abs(amount)); - } - public static class ViewHolder { @Bind(R.id.tv_title) TextView tvTitle; diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/ExpandableListReportAdapter.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/ExpandableListReportAdapter.java index 3b23966..9c5b2b1 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/ExpandableListReportAdapter.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/ExpandableListReportAdapter.java @@ -7,13 +7,17 @@ import android.widget.SimpleExpandableListAdapter; import android.widget.TextView; +import com.blogspot.e_kanivets.moneytracker.MtApp; import com.blogspot.e_kanivets.moneytracker.R; +import com.blogspot.e_kanivets.moneytracker.controller.FormatController; import com.blogspot.e_kanivets.moneytracker.report.record.RecordReportConverter; import java.util.List; import java.util.Locale; import java.util.Map; +import javax.inject.Inject; + import butterknife.Bind; import butterknife.ButterKnife; @@ -23,6 +27,9 @@ * @author Evgenii Kanivets */ public class ExpandableListReportAdapter extends SimpleExpandableListAdapter { + @Inject + FormatController formatController; + private List> groupData; private List>> childData; @@ -44,6 +51,7 @@ public ExpandableListReportAdapter(Context context, List>> childData, int childLayout, String[] childFrom, int[] childTo) { super(context, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo); + MtApp.get().getAppComponent().inject(ExpandableListReportAdapter.this); this.groupData = groupData; this.childData = childData; @@ -84,11 +92,7 @@ private void customizeView(View view, Map values, boolean groupV viewHolder.tvTotal.setTextColor(price >= 0 ? green : red); viewHolder.tvCategory.setText(values.get(RecordReportConverter.TITLE_PARAM_NAME)); - viewHolder.tvTotal.setText(format(price)); - } - - private String format(double amount) { - return (amount >= 0 ? "+ " : "- ") + String.format(Locale.getDefault(), "%.0f", Math.abs(amount)); + viewHolder.tvTotal.setText(formatController.formatSignedAmount(price)); } public static class ViewHolder { diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/RecordAdapter.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/RecordAdapter.java index d6c393b..db19fb8 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/RecordAdapter.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/RecordAdapter.java @@ -8,13 +8,17 @@ import android.widget.BaseAdapter; import android.widget.TextView; +import com.blogspot.e_kanivets.moneytracker.MtApp; import com.blogspot.e_kanivets.moneytracker.R; +import com.blogspot.e_kanivets.moneytracker.controller.FormatController; import com.blogspot.e_kanivets.moneytracker.entity.data.Record; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import javax.inject.Inject; + import butterknife.Bind; import butterknife.ButterKnife; @@ -25,6 +29,9 @@ * @author Evgenii Kanivets */ public class RecordAdapter extends BaseAdapter { + @Inject + FormatController formatController; + private Context context; private List records; @@ -35,6 +42,8 @@ public class RecordAdapter extends BaseAdapter { @SuppressWarnings("deprecation") public RecordAdapter(Context context, List records) { + MtApp.get().getAppComponent().inject(RecordAdapter.this); + this.context = context; this.records = records; @@ -82,8 +91,8 @@ public View getView(final int position, View convertView, ViewGroup parent) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); viewHolder.tvDateAndTime.setText(dateFormat.format(new Date(record.getTime()))); - viewHolder.tvPrice.setText((record.isIncome() ? "+ " : "- ") - + Integer.toString((int) record.getPrice())); + viewHolder.tvPrice.setText(formatController.formatAmount( + (record.isIncome() ? 1 : -1) * record.getFullPrice())); viewHolder.tvTitle.setText(record.getTitle()); if (record.getCategory() != null) viewHolder.tvCategory.setText(record.getCategory().getName()); diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/FormatController.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/FormatController.java new file mode 100644 index 0000000..91da958 --- /dev/null +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/FormatController.java @@ -0,0 +1,50 @@ +package com.blogspot.e_kanivets.moneytracker.controller; + +import java.util.Locale; + +/** + * Controller class to encapsulate string format handling logic. + * Not deal with {@link com.blogspot.e_kanivets.moneytracker.repo.base.IRepo} instances as others. + * Created on 6/1/16. + * + * @author Evgenii Kanivets + */ +public class FormatController { + public static final String PRECISION_MATH = "precision_math"; + public static final String PRECISION_INT = "precision_int"; + public static final String PRECISION_NONE = "precision_none"; + + private PreferenceController preferenceController; + + public FormatController(PreferenceController preferenceController) { + this.preferenceController = preferenceController; + } + + public String formatAmount(double amount) { + switch (preferenceController.readDisplayPrecision()) { + case PRECISION_MATH: + return String.format(Locale.getDefault(), "%d", Math.round(amount)); + + case PRECISION_INT: + return String.format(Locale.getDefault(), "%d", (int) amount); + + case PRECISION_NONE: + return String.format(Locale.getDefault(), "%.2f", amount); + + default: + return String.format(Locale.getDefault(), "%d", Math.round(amount)); + } + } + + public String formatSignedAmount(double amount) { + return (amount >= 0.0 ? "+ " : "- ") + formatAmount(Math.abs(amount)); + } + + public String formatIncome(double amount, String currency) { + return (amount >= 0 ? "+ " : "- ") + formatAmount(Math.abs(amount)) + " " + currency; + } + + public String formatExpense(double amount, String currency) { + return (amount > 0 ? "+ " : "- ") + formatAmount(Math.abs(amount)) + " " + currency; + } +} diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/PreferenceController.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/PreferenceController.java index f144ea5..c342ee7 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/PreferenceController.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/PreferenceController.java @@ -94,6 +94,14 @@ public String readDefaultCurrency() { return preferences.getString(defaultCurrencyPref, null); } + @NonNull + public String readDisplayPrecision() { + String displayPrecisionPref = context.getString(R.string.pref_display_precision); + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + + return preferences.getString(displayPrecisionPref, FormatController.PRECISION_MATH); + } + public long readFirstTs() { return getDefaultPrefs().getLong(KEY_FIRST_TS, -1); } diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/data/AccountController.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/data/AccountController.java index d09ba71..1fe5206 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/data/AccountController.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/data/AccountController.java @@ -36,11 +36,11 @@ public boolean recordAdded(@Nullable Record record) { switch (record.getType()) { case Record.TYPE_EXPENSE: - account.take((int) record.getPrice()); + account.take(record.getFullPrice()); break; case Record.TYPE_INCOME: - account.put((int) record.getPrice()); + account.put(record.getFullPrice()); break; default: @@ -60,11 +60,11 @@ public boolean recordDeleted(@Nullable Record record) { switch (record.getType()) { case Record.TYPE_EXPENSE: - account.put((int) record.getPrice()); + account.put(record.getFullPrice()); break; case Record.TYPE_INCOME: - account.take((int) record.getPrice()); + account.take(record.getFullPrice()); break; default: @@ -91,8 +91,8 @@ public boolean transferDone(@Nullable Transfer transfer) { if (fromAccount == null || toAccount == null) return false; - fromAccount.take(transfer.getFromAmount()); - toAccount.put(transfer.getToAmount()); + fromAccount.take(transfer.getFullFromAmount()); + toAccount.put(transfer.getFullToAmount()); repo.update(fromAccount); repo.update(toAccount); diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/data/RecordController.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/data/RecordController.java index 6a36903..b3bbf22 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/data/RecordController.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/data/RecordController.java @@ -102,7 +102,8 @@ public List readWithCondition(String condition, String[] args) { account = accountController.read(record.getAccount().getId()); completedRecordList.add(new Record(record.getId(), record.getTime(), record.getType(), - record.getTitle(), category, record.getPrice(), account, record.getCurrency())); + record.getTitle(), category, record.getPrice(), account, record.getCurrency(), + record.getDecimals())); } return completedRecordList; @@ -161,6 +162,7 @@ private Record validateRecord(@NonNull Record record) { Category category = categoryController.readOrCreate(record.getCategory().getName()); return new Record(record.getId(), record.getTime(), record.getType(), record.getTitle(), - category, record.getPrice(), record.getAccount(), record.getCurrency()); + category, record.getPrice(), record.getAccount(), record.getCurrency(), + record.getDecimals()); } } \ No newline at end of file diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/AppComponent.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/AppComponent.java index 1e9ef3d..019e9ac 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/AppComponent.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/AppComponent.java @@ -11,11 +11,15 @@ import com.blogspot.e_kanivets.moneytracker.activity.exchange_rate.ExchangeRatesActivity; import com.blogspot.e_kanivets.moneytracker.activity.record.AddRecordActivity; import com.blogspot.e_kanivets.moneytracker.activity.record.MainActivity; +import com.blogspot.e_kanivets.moneytracker.adapter.AccountAdapter; +import com.blogspot.e_kanivets.moneytracker.adapter.ExpandableListReportAdapter; +import com.blogspot.e_kanivets.moneytracker.adapter.RecordAdapter; import com.blogspot.e_kanivets.moneytracker.di.module.repo.CachedRepoModule; import com.blogspot.e_kanivets.moneytracker.di.module.ControllerModule; import com.blogspot.e_kanivets.moneytracker.ui.AppRateDialog; import com.blogspot.e_kanivets.moneytracker.ui.PeriodSpinner; import com.blogspot.e_kanivets.moneytracker.ui.presenter.AccountsSummaryPresenter; +import com.blogspot.e_kanivets.moneytracker.ui.presenter.ShortSummaryPresenter; import javax.inject.Singleton; @@ -57,4 +61,12 @@ public interface AppComponent { void inject(AppRateDialog appRateDialog); void inject(PeriodSpinner periodSpinner); + + void inject(RecordAdapter recordAdapter); + + void inject(AccountAdapter accountAdapter); + + void inject(ExpandableListReportAdapter expandableListReportAdapter); + + void inject(ShortSummaryPresenter shortSummaryPresenter); } diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/module/ControllerModule.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/module/ControllerModule.java index 73babad..fbba9b1 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/module/ControllerModule.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/di/module/ControllerModule.java @@ -3,6 +3,7 @@ import android.content.Context; import android.support.annotation.NonNull; +import com.blogspot.e_kanivets.moneytracker.controller.FormatController; import com.blogspot.e_kanivets.moneytracker.controller.PeriodController; import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController; import com.blogspot.e_kanivets.moneytracker.controller.data.CategoryController; @@ -97,4 +98,11 @@ public PreferenceController providesPreferenceController() { public PeriodController providesPeriodController(PreferenceController preferenceController) { return new PeriodController(preferenceController); } + + @Provides + @NonNull + @Singleton + public FormatController providesFormatController(PreferenceController preferenceController) { + return new FormatController(preferenceController); + } } diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/base/BaseEntity.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/base/BaseEntity.java index 684f41c..c96a7f8 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/base/BaseEntity.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/base/BaseEntity.java @@ -18,4 +18,13 @@ protected boolean equals(String str1, String str2) { if (str1 == null) return str2 == null; else return str1.equals(str2); } -} \ No newline at end of file + + protected int getInteger(double value) { + return (int) value; + } + + protected int getDecimal(double value) { + // Strange calculation because of double type precision issue + return (int) Math.round(value * 100 - getInteger(value) * 100); + } +} diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Account.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Account.java index bfd4855..61bd958 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Account.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Account.java @@ -12,22 +12,25 @@ * @author Evgenii Kanivets */ public class Account extends BaseEntity implements Parcelable { - private String title; + private final String title; private int curSum; - private String currency; + private final String currency; + private int decimals; - public Account(long id, String title, int curSum, String currency) { + public Account(long id, String title, int curSum, String currency, int decimals) { this.id = id; this.title = title; this.curSum = curSum; this.currency = currency; + this.decimals = decimals; } - public Account(String title, int curSum, String currency) { + public Account(String title, double curSum, String currency) { this.id = -1; this.title = title; - this.curSum = curSum; this.currency = currency; + this.curSum = getInteger(curSum); + this.decimals = getDecimal(curSum); } protected Account(Parcel in) { @@ -35,6 +38,7 @@ protected Account(Parcel in) { title = in.readString(); curSum = in.readInt(); currency = in.readString(); + decimals = in.readInt(); } public static final Creator CREATOR = new Creator() { @@ -57,16 +61,30 @@ public int getCurSum() { return curSum; } + public int getDecimals() { + return decimals; + } + + public double getFullSum() { + return getCurSum() + getDecimals() / 100.0; + } + public String getCurrency() { return currency; } - public void put(int amount) { - curSum += amount; + public void put(double amount) { + double sum = curSum + decimals / 100.0; + sum += amount; + curSum = getInteger(sum); + decimals = getDecimal(sum); } - public void take(int amount) { - curSum -= amount; + public void take(double amount) { + double sum = curSum + decimals / 100.0; + sum -= amount; + curSum = getInteger(sum); + decimals = getDecimal(sum); } @SuppressWarnings("SimplifiableIfStatement") @@ -77,7 +95,8 @@ public boolean equals(Object o) { return this.id == account.getId() && equals(this.title, account.getTitle()) && this.curSum == account.getCurSum() - && equals(this.currency, account.getCurrency()); + && equals(this.currency, account.getCurrency()) + && this.decimals == account.decimals; } else return false; } @@ -89,7 +108,8 @@ public String toString() { sb.append("id = ").append(id).append(", "); sb.append("title = ").append(title).append(", "); sb.append("curSum = ").append(curSum).append(", "); - sb.append("currency = ").append(currency); + sb.append("currency = ").append(currency).append(", "); + sb.append("decimals = ").append(decimals); sb.append("}"); return sb.toString(); @@ -106,5 +126,6 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeString(title); dest.writeInt(curSum); dest.writeString(currency); + dest.writeInt(decimals); } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Record.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Record.java index e4d9f11..eb643b0 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Record.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Record.java @@ -19,22 +19,26 @@ public class Record extends BaseEntity implements Parcelable { private final int type; private final String title; private final Category category; - private final double price; + private final int price; private final Account account; private final String currency; + private final int decimals; - public Record(long id, long time, int type, String title, long categoryId, double price, long accountId, String currency) { + public Record(long id, long time, int type, String title, long categoryId, int price, + long accountId, String currency, int decimals) { this.id = id; this.time = time; this.type = type; this.title = title; this.category = new Category(categoryId, null); this.price = price; - this.account = new Account(accountId, null, -1, null); + this.account = new Account(accountId, null, -1, null, 0); this.currency = currency; + this.decimals = decimals; } - public Record(long id, long time, int type, String title, Category category, double price, Account account, String currency) { + public Record(long id, long time, int type, String title, Category category, int price, + Account account, String currency, int decimals) { this.id = id; this.time = time; this.type = type; @@ -43,17 +47,33 @@ public Record(long id, long time, int type, String title, Category category, dou this.price = price; this.account = account; this.currency = currency; + this.decimals = decimals; } - public Record(long time, int type, String title, Category category, double price, Account account, String currency) { + public Record(long id, long time, int type, String title, Category category, double price, + Account account, String currency) { + this.id = id; + this.time = time; + this.type = type; + this.title = title; + this.category = category; + this.account = account; + this.currency = currency; + this.price = getInteger(price); + this.decimals = getDecimal(price); + } + + public Record(long time, int type, String title, Category category, double price, Account account, + String currency) { this.id = -1; this.time = time; this.type = type; this.title = title; this.category = category; - this.price = price; this.account = account; this.currency = currency; + this.price = getInteger(price); + this.decimals = getDecimal(price); } protected Record(Parcel in) { @@ -62,9 +82,10 @@ protected Record(Parcel in) { type = in.readInt(); title = in.readString(); category = in.readParcelable(Category.class.getClassLoader()); - price = in.readDouble(); + price = in.readInt(); account = in.readParcelable(Account.class.getClassLoader()); currency = in.readString(); + decimals = in.readInt(); } public static final Creator CREATOR = new Creator() { @@ -97,10 +118,18 @@ public Category getCategory() { return category; } - public double getPrice() { + public int getPrice() { return price; } + public int getDecimals() { + return decimals; + } + + public double getFullPrice() { + return price + decimals / 100.0; + } + public long getTime() { return time; } @@ -146,7 +175,8 @@ public String toString() { sb.append("category = ").append(category).append(", "); sb.append("price = ").append(price).append(", "); sb.append("account = ").append(account).append(", "); - sb.append("currency = ").append(currency); + sb.append("currency = ").append(currency).append(", "); + sb.append("decimals = ").append(decimals); sb.append("}"); return sb.toString(); @@ -163,7 +193,8 @@ && equals(this.title, record.getTitle()) && this.category.equals(record.getCategory()) && this.price == record.getPrice() && this.account.equals(record.getAccount()) - && equals(this.currency, record.getCurrency()); + && equals(this.currency, record.getCurrency()) + && this.decimals == record.decimals; } else return false; } @@ -179,8 +210,9 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeInt(type); dest.writeString(title); dest.writeParcelable(category, 0); - dest.writeDouble(price); + dest.writeInt(price); dest.writeParcelable(account, 0); dest.writeString(currency); + dest.writeInt(decimals); } } \ No newline at end of file diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Transfer.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Transfer.java index 7b4e8f5..b674c1f 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Transfer.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/entity/data/Transfer.java @@ -12,13 +12,18 @@ * @author Evgenii Kanivets */ public class Transfer extends BaseEntity implements Parcelable { - private long time; - private long fromAccountId; - private long toAccountId; - private int fromAmount; - private int toAmount; - - public Transfer(long id, long time, long fromAccountId, long toAccountId, int fromAmount, int toAmount) { + private final long time; + private final long fromAccountId; + private final long toAccountId; + private final int fromAmount; + private final int toAmount; + private final int fromDecimals; + private final int toDecimals; + + public Transfer(long id, long time, long fromAccountId, long toAccountId, int fromAmount, + int toAmount, int fromDecimals, int toDecimals) { + this.fromDecimals = fromDecimals; + this.toDecimals = toDecimals; this.id = id; this.time = time; this.fromAccountId = fromAccountId; @@ -27,12 +32,14 @@ public Transfer(long id, long time, long fromAccountId, long toAccountId, int fr this.toAmount = toAmount; } - public Transfer(long time, long fromAccountId, long toAccountId, int fromAmount, int toAmount) { + public Transfer(long time, long fromAccountId, long toAccountId, double fromAmount, double toAmount) { this.time = time; this.fromAccountId = fromAccountId; this.toAccountId = toAccountId; - this.fromAmount = fromAmount; - this.toAmount = toAmount; + this.fromAmount = getInteger(fromAmount); + this.fromDecimals = getDecimal(fromAmount); + this.toAmount = getInteger(toAmount); + this.toDecimals = getDecimal(toAmount); } protected Transfer(Parcel in) { @@ -41,6 +48,8 @@ protected Transfer(Parcel in) { toAccountId = in.readLong(); fromAmount = in.readInt(); toAmount = in.readInt(); + fromDecimals = in.readInt(); + toDecimals = in.readInt(); } public static final Creator CREATOR = new Creator() { @@ -80,16 +89,34 @@ public int getToAmount() { return toAmount; } + public int getFromDecimals() { + return fromDecimals; + } + + public int getToDecimals() { + return toDecimals; + } + + public double getFullFromAmount() { + return fromAmount + fromDecimals / 100.0; + } + + public double getFullToAmount() { + return toAmount + toDecimals / 100.0; + } + @Override public boolean equals(Object o) { if (o instanceof Transfer) { Transfer transfer = (Transfer) o; - return this.id == transfer.getId() - && this.time == transfer.getTime() - && this.fromAccountId == transfer.getFromAccountId() - && this.toAccountId == transfer.getToAccountId() - && this.fromAmount == transfer.getFromAmount() - && this.toAmount == transfer.getToAmount(); + return this.id == transfer.id + && this.time == transfer.time + && this.fromAccountId == transfer.fromAccountId + && this.toAccountId == transfer.toAccountId + && this.fromAmount == transfer.fromAmount + && this.toAmount == transfer.toAmount + && this.fromDecimals == transfer.fromDecimals + && this.toDecimals == transfer.toDecimals; } else return false; } @@ -103,7 +130,9 @@ public String toString() { sb.append("fromAccountId = ").append(fromAccountId).append(", "); sb.append("toAccountId = ").append(toAccountId).append(", "); sb.append("fromAmount = ").append(fromAmount).append(", "); - sb.append("toAmount = ").append(toAmount); + sb.append("toAmount = ").append(toAmount).append(", "); + sb.append("fromDecimals = ").append(fromDecimals).append(", "); + sb.append("toDecimals = ").append(toDecimals); sb.append("}"); return sb.toString(); @@ -121,5 +150,7 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeLong(toAccountId); dest.writeInt(fromAmount); dest.writeInt(toAmount); + dest.writeInt(fromDecimals); + dest.writeInt(toDecimals); } } \ No newline at end of file diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/DbHelper.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/DbHelper.java index e4d9d40..09df2b5 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/DbHelper.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/DbHelper.java @@ -15,7 +15,7 @@ public class DbHelper extends SQLiteOpenHelper { /* DB_VERSION = 1 */ public static final String DB_NAME = "database"; - public static final int DB_VERSION = 3; + public static final int DB_VERSION = 4; public static final String TABLE_RECORDS = "records"; public static final String TABLE_CATEGORIES = "categories"; @@ -49,6 +49,11 @@ public class DbHelper extends SQLiteOpenHelper { public static final String TO_CURRENCY_COLUMN = "to_currency"; public static final String AMOUNT_COLUMN = "amount"; + /* DB_VERSION = 4 */ + public static final String DECIMALS_COLUMN = "decimals"; + public static final String DECIMALS_FROM_COLUMN = "decimals_from"; + public static final String DECIMALS_TO_COLUMN = "decimals_to"; + public DbHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @@ -57,7 +62,8 @@ public DbHelper(Context context) { public void onCreate(SQLiteDatabase db) { //createDbVersion1(db); //createDbVersion2(db); - createDbVersion3(db); + //createDbVersion3(db); + createDbVersion4(db); } @Override @@ -65,12 +71,23 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if (oldVersion < 2) { db.beginTransaction(); - createAccountsTable(db); + db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "(" + + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + + CREATED_AT_COLUMN + " INTEGER," + + TITLE_COLUMN + " TEXT," + + CUR_SUM_COLUMN + " INTEGER," + + CURRENCY_COLUMN + " TEXT );"); /* Add account_id column into the records table */ db.execSQL("ALTER TABLE " + TABLE_RECORDS + " ADD COLUMN " + ACCOUNT_ID_COLUMN + " INTEGER;"); - createTransfersTable(db); + db.execSQL("CREATE TABLE " + TABLE_TRANSFERS + "(" + + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + + TIME_COLUMN + " INTEGER," + + FROM_ACCOUNT_ID_COLUMN + " INTEGER," + + TO_ACCOUNT_ID_COLUMN + " INTEGER," + + FROM_AMOUNT_COLUMN + " INTEGER," + + TO_AMOUNT_COLUMN + " INTEGER);"); /* Insert default account for all records from DB_VERSION = 1 */ long id = insertDefaultAccount(db); @@ -81,7 +98,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.update(DbHelper.TABLE_RECORDS, contentValues, null, null); db.setTransactionSuccessful(); - db.endTransaction(); } @@ -94,7 +110,27 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("ALTER TABLE " + TABLE_RECORDS + " ADD COLUMN " + CURRENCY_COLUMN + " INTEGER;"); db.setTransactionSuccessful(); + db.endTransaction(); + } + + if (oldVersion < 4) { + db.beginTransaction(); + + /* Add decimals column into the records table */ + db.execSQL("ALTER TABLE " + TABLE_RECORDS + " ADD COLUMN " + + DECIMALS_COLUMN + " INTEGER;"); + + /* Add decimals column into the accounts table */ + db.execSQL("ALTER TABLE " + TABLE_ACCOUNTS + " ADD COLUMN " + + DECIMALS_COLUMN + " INTEGER;"); + /* Add decimal_from and decimals_to columns into the transfers table */ + db.execSQL("ALTER TABLE " + TABLE_TRANSFERS + " ADD COLUMN " + + DECIMALS_FROM_COLUMN + " INTEGER;"); + db.execSQL("ALTER TABLE " + TABLE_TRANSFERS + " ADD COLUMN " + + DECIMALS_TO_COLUMN + " INTEGER;"); + + db.setTransactionSuccessful(); db.endTransaction(); } } @@ -129,13 +165,25 @@ private void createDbVersion2(SQLiteDatabase db) { + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME_COLUMN + " TEXT" + ");"); - createAccountsTable(db); + db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "(" + + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + + CREATED_AT_COLUMN + " INTEGER," + + TITLE_COLUMN + " TEXT," + + CUR_SUM_COLUMN + " INTEGER," + + CURRENCY_COLUMN + " TEXT );"); - createTransfersTable(db); + db.execSQL("CREATE TABLE " + TABLE_TRANSFERS + "(" + + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + + TIME_COLUMN + " INTEGER," + + FROM_ACCOUNT_ID_COLUMN + " INTEGER," + + TO_ACCOUNT_ID_COLUMN + " INTEGER," + + FROM_AMOUNT_COLUMN + " INTEGER," + + TO_AMOUNT_COLUMN + " INTEGER);"); insertDefaultAccount(db); } + @SuppressWarnings("unused") private void createDbVersion3(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_RECORDS + "(" + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," @@ -151,32 +199,63 @@ private void createDbVersion3(SQLiteDatabase db) { + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME_COLUMN + " TEXT" + ");"); - createAccountsTable(db); + db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "(" + + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + + CREATED_AT_COLUMN + " INTEGER," + + TITLE_COLUMN + " TEXT," + + CUR_SUM_COLUMN + " INTEGER," + + CURRENCY_COLUMN + " TEXT );"); - createTransfersTable(db); + db.execSQL("CREATE TABLE " + TABLE_TRANSFERS + "(" + + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + + TIME_COLUMN + " INTEGER," + + FROM_ACCOUNT_ID_COLUMN + " INTEGER," + + TO_ACCOUNT_ID_COLUMN + " INTEGER," + + FROM_AMOUNT_COLUMN + " INTEGER," + + TO_AMOUNT_COLUMN + " INTEGER);"); createRatesTable(db); insertDefaultAccount(db); } - private void createAccountsTable(SQLiteDatabase db) { + private void createDbVersion4(SQLiteDatabase db) { + db.execSQL("CREATE TABLE " + TABLE_RECORDS + "(" + + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + + TIME_COLUMN + " INTEGER," + + TYPE_COLUMN + " INTEGER," + + TITLE_COLUMN + " TEXT," + + CATEGORY_ID_COLUMN + " INTEGER," + + PRICE_COLUMN + " INTEGER," + + ACCOUNT_ID_COLUMN + " INTEGER," + + CURRENCY_COLUMN + " TEXT," + + DECIMALS_COLUMN + " INTEGER);"); + + db.execSQL("CREATE TABLE " + TABLE_CATEGORIES + "(" + + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + + NAME_COLUMN + " TEXT" + ");"); + db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "(" + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + CREATED_AT_COLUMN + " INTEGER," + TITLE_COLUMN + " TEXT," + CUR_SUM_COLUMN + " INTEGER," - + CURRENCY_COLUMN + " TEXT );"); - } + + CURRENCY_COLUMN + " TEXT," + + DECIMALS_COLUMN + " INTEGER);"); - private void createTransfersTable(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_TRANSFERS + "(" + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT," + TIME_COLUMN + " INTEGER," + FROM_ACCOUNT_ID_COLUMN + " INTEGER," + TO_ACCOUNT_ID_COLUMN + " INTEGER," + FROM_AMOUNT_COLUMN + " INTEGER," - + TO_AMOUNT_COLUMN + " INTEGER);"); + + TO_AMOUNT_COLUMN + " INTEGER," + + DECIMALS_FROM_COLUMN + " INTEGER," + + DECIMALS_TO_COLUMN + " INTEGER);"); + + createRatesTable(db); + + insertDefaultAccount(db); } private void createRatesTable(SQLiteDatabase db) { @@ -198,4 +277,4 @@ private long insertDefaultAccount(SQLiteDatabase db) { return db.insert(TABLE_ACCOUNTS, null, contentValues); } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/AccountRepo.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/AccountRepo.java index f4e00b1..4c09827 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/AccountRepo.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/AccountRepo.java @@ -42,6 +42,7 @@ protected ContentValues contentValues(@Nullable Account account) { contentValues.put(DbHelper.TITLE_COLUMN, account.getTitle()); contentValues.put(DbHelper.CUR_SUM_COLUMN, account.getCurSum()); contentValues.put(DbHelper.CURRENCY_COLUMN, account.getCurrency()); + contentValues.put(DbHelper.DECIMALS_COLUMN, account.getDecimals()); return contentValues; } @@ -58,13 +59,15 @@ protected List getListFromCursor(@Nullable Cursor cursor) { int titleColIndex = cursor.getColumnIndex(DbHelper.TITLE_COLUMN); int curSumColIndex = cursor.getColumnIndex(DbHelper.CUR_SUM_COLUMN); int currencyColIndex = cursor.getColumnIndex(DbHelper.CURRENCY_COLUMN); + int decimalsColIndex = cursor.getColumnIndex(DbHelper.DECIMALS_COLUMN); do { // Read a account from DB Account account = new Account(cursor.getLong(idColIndex), cursor.getString(titleColIndex), cursor.getInt(curSumColIndex), - cursor.getString(currencyColIndex)); + cursor.getString(currencyColIndex), + cursor.getInt(decimalsColIndex)); accountList.add(account); } while (cursor.moveToNext()); diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/RecordRepo.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/RecordRepo.java index 8fec8a9..d7a5acf 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/RecordRepo.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/RecordRepo.java @@ -47,6 +47,7 @@ protected ContentValues contentValues(@Nullable Record record) { contentValues.put(DbHelper.PRICE_COLUMN, record.getPrice()); contentValues.put(DbHelper.ACCOUNT_ID_COLUMN, record.getAccount().getId()); contentValues.put(DbHelper.CURRENCY_COLUMN, record.getCurrency()); + contentValues.put(DbHelper.DECIMALS_COLUMN, record.getDecimals()); return contentValues; } @@ -66,6 +67,7 @@ protected List getListFromCursor(Cursor cursor) { int priceColIndex = cursor.getColumnIndex(DbHelper.PRICE_COLUMN); int accountIdColIndex = cursor.getColumnIndex(DbHelper.ACCOUNT_ID_COLUMN); int currencyColIndex = cursor.getColumnIndex(DbHelper.CURRENCY_COLUMN); + int decimalsColIndex = cursor.getColumnIndex(DbHelper.DECIMALS_COLUMN); do { Record record = new Record(cursor.getLong(idColIndex), @@ -75,7 +77,8 @@ protected List getListFromCursor(Cursor cursor) { cursor.getLong(categoryColIndex), cursor.getInt(priceColIndex), cursor.getLong(accountIdColIndex), - cursor.getString(currencyColIndex)); + cursor.getString(currencyColIndex), + cursor.getInt(decimalsColIndex)); recordList.add(record); } while (cursor.moveToNext()); diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/TransferRepo.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/TransferRepo.java index d323fc1..10864c9 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/TransferRepo.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/data/TransferRepo.java @@ -44,6 +44,8 @@ protected ContentValues contentValues(@Nullable Transfer transfer) { contentValues.put(DbHelper.TO_ACCOUNT_ID_COLUMN, transfer.getToAccountId()); contentValues.put(DbHelper.FROM_AMOUNT_COLUMN, transfer.getFromAmount()); contentValues.put(DbHelper.TO_AMOUNT_COLUMN, transfer.getToAmount()); + contentValues.put(DbHelper.DECIMALS_FROM_COLUMN, transfer.getFromDecimals()); + contentValues.put(DbHelper.DECIMALS_TO_COLUMN, transfer.getToDecimals()); return contentValues; } @@ -62,6 +64,8 @@ protected List getListFromCursor(@Nullable Cursor cursor) { int idColToAccountId = cursor.getColumnIndex(DbHelper.TO_ACCOUNT_ID_COLUMN); int idColFromAmount = cursor.getColumnIndex(DbHelper.FROM_AMOUNT_COLUMN); int idColToAmount = cursor.getColumnIndex(DbHelper.TO_AMOUNT_COLUMN); + int idColDecimalsFrom = cursor.getColumnIndex(DbHelper.DECIMALS_FROM_COLUMN); + int idColDecimalsTo = cursor.getColumnIndex(DbHelper.DECIMALS_TO_COLUMN); do { // Read a account from DB @@ -70,7 +74,9 @@ protected List getListFromCursor(@Nullable Cursor cursor) { cursor.getLong(idColFromAccountId), cursor.getLong(idColToAccountId), cursor.getInt(idColFromAmount), - cursor.getInt(idColToAmount)); + cursor.getInt(idColToAmount), + cursor.getInt(idColDecimalsFrom), + cursor.getInt(idColDecimalsTo)); accountList.add(account); } while (cursor.moveToNext()); diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/account/AccountsReport.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/account/AccountsReport.java index 50f3635..626cbbc 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/account/AccountsReport.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/account/AccountsReport.java @@ -4,7 +4,6 @@ import com.blogspot.e_kanivets.moneytracker.entity.data.Account; import com.blogspot.e_kanivets.moneytracker.entity.data.ExchangeRate; -import com.blogspot.e_kanivets.moneytracker.report.account.IAccountsReport; import com.blogspot.e_kanivets.moneytracker.report.base.IExchangeRateProvider; import java.util.List; @@ -49,7 +48,7 @@ private void makeReport(List accountList) { total = 0; for (Account account : accountList) { - int convertedSum = account.getCurSum(); + double convertedSum = account.getFullSum(); if (!currency.equals(account.getCurrency())) { ExchangeRate exchangeRate = rateProvider.getRate(account); diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/chart/MonthReport.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/chart/MonthReport.java index 23abe84..143f73e 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/chart/MonthReport.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/chart/MonthReport.java @@ -91,7 +91,7 @@ private List generateReport(List recordList) { if (monthMap.get(timestamp) == null) monthMap.put(timestamp, new MonthNode(timestamp)); MonthNode node = monthMap.get(timestamp); - double convertedPrice = record.getPrice(); + double convertedPrice = record.getFullPrice(); if (!currency.equals(record.getCurrency())) { ExchangeRate exchangeRate = rateProvider.getRate(record); if (exchangeRate == null) throw new NullPointerException("No exchange rate found"); diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/record/RecordReport.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/record/RecordReport.java index bac23af..c9cf415 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/record/RecordReport.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/report/record/RecordReport.java @@ -92,11 +92,11 @@ private void makeReport(List recordList) { for (Record record : convertedRecordList) { switch (record.getType()) { case Record.TYPE_INCOME: - totalIncome += record.getPrice(); + totalIncome += record.getFullPrice(); break; case Record.TYPE_EXPENSE: - totalExpense -= record.getPrice(); + totalExpense -= record.getFullPrice(); break; default: @@ -128,7 +128,7 @@ private List convertRecordList(List recordList) { List convertedRecordList = new ArrayList<>(); for (Record record : recordList) { - double convertedPrice = record.getPrice(); + double convertedPrice = record.getFullPrice(); if (!currency.equals(record.getCurrency())) { ExchangeRate exchangeRate = rateProvider.getRate(record); @@ -136,8 +136,13 @@ private List convertRecordList(List recordList) { convertedPrice *= exchangeRate.getAmount(); } + int intConvertedPrice = (int) convertedPrice; + // Strange calculation because of double type precision issue + int decConvertedPrice = (int) Math.round(convertedPrice * 100 - intConvertedPrice * 100); + Record convertedRecord = new Record(record.getId(), record.getTime(), record.getType(), - record.getTitle(), record.getCategory(), convertedPrice, record.getAccount(), currency); + record.getTitle(), record.getCategory(), intConvertedPrice, record.getAccount(), + currency, decConvertedPrice); convertedRecordList.add(convertedRecord); } @@ -204,10 +209,10 @@ public int compare(Record lhs, Record rhs) { private double getAmount(Record record) { switch (record.getType()) { case Record.TYPE_INCOME: - return record.getPrice(); + return record.getFullPrice(); case Record.TYPE_EXPENSE: - return -record.getPrice(); + return -record.getFullPrice(); default: return 0; diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/presenter/AccountsSummaryPresenter.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/presenter/AccountsSummaryPresenter.java index 31534ff..fc69167 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/presenter/AccountsSummaryPresenter.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/presenter/AccountsSummaryPresenter.java @@ -11,6 +11,7 @@ import com.blogspot.e_kanivets.moneytracker.controller.CurrencyController; import com.blogspot.e_kanivets.moneytracker.MtApp; import com.blogspot.e_kanivets.moneytracker.R; +import com.blogspot.e_kanivets.moneytracker.controller.FormatController; import com.blogspot.e_kanivets.moneytracker.controller.data.AccountController; import com.blogspot.e_kanivets.moneytracker.controller.data.ExchangeRateController; import com.blogspot.e_kanivets.moneytracker.report.ReportMaker; @@ -39,6 +40,8 @@ public class AccountsSummaryPresenter extends BaseSummaryPresenter { AccountController accountController; @Inject CurrencyController currencyController; + @Inject + FormatController formatController; private int red; private int green; @@ -107,16 +110,12 @@ public void update() { viewHolder.tvCurrency.setText(""); } else { viewHolder.tvTotal.setTextColor(report.getTotal() >= 0 ? green : red); - viewHolder.tvTotal.setText(format(report.getTotal())); + viewHolder.tvTotal.setText(formatController.formatSignedAmount(report.getTotal())); viewHolder.tvCurrency.setTextColor(report.getTotal() >= 0 ? green : red); viewHolder.tvCurrency.setText(report.getCurrency()); } } - private String format(double amount) { - return (amount >= 0 ? "+ " : "- ") + String.format(Locale.getDefault(), "%.0f", Math.abs(amount)); - } - public static class ViewHolder { @Bind(R.id.spinner_currency) AppCompatSpinner spinnerCurrency; diff --git a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/presenter/ShortSummaryPresenter.java b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/presenter/ShortSummaryPresenter.java index 2269c37..f6af61c 100644 --- a/app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/presenter/ShortSummaryPresenter.java +++ b/app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/presenter/ShortSummaryPresenter.java @@ -5,12 +5,15 @@ import android.view.View; import android.widget.TextView; +import com.blogspot.e_kanivets.moneytracker.MtApp; import com.blogspot.e_kanivets.moneytracker.R; +import com.blogspot.e_kanivets.moneytracker.controller.FormatController; import com.blogspot.e_kanivets.moneytracker.report.record.IRecordReport; import com.blogspot.e_kanivets.moneytracker.ui.presenter.base.BaseSummaryPresenter; import java.util.List; -import java.util.Locale; + +import javax.inject.Inject; import butterknife.Bind; import butterknife.ButterKnife; @@ -23,6 +26,9 @@ */ public class ShortSummaryPresenter extends BaseSummaryPresenter { + @Inject + FormatController formatController; + private int red; private int green; private View view; @@ -30,6 +36,7 @@ public class ShortSummaryPresenter extends BaseSummaryPresenter { @SuppressWarnings("deprecation") public ShortSummaryPresenter(Context context) { this.context = context; + MtApp.get().getAppComponent().inject(ShortSummaryPresenter.this); layoutInflater = LayoutInflater.from(context); red = context.getResources().getColor(R.color.red); @@ -60,26 +67,19 @@ public void update(IRecordReport report, String currency, List ratesNeed report.getPeriod().getFirstDay(), report.getPeriod().getLastDay())); viewHolder.tvTotalIncome.setTextColor(report.getTotalIncome() >= 0 ? green : red); - viewHolder.tvTotalIncome.setText(formatIncome(report.getTotalIncome(), report.getCurrency())); + viewHolder.tvTotalIncome.setText(formatController.formatIncome(report.getTotalIncome(), + report.getCurrency())); viewHolder.tvTotalExpense.setTextColor(report.getTotalExpense() > 0 ? green : red); - viewHolder.tvTotalExpense.setText(formatExpense(report.getTotalExpense(), report.getCurrency())); + viewHolder.tvTotalExpense.setText(formatController.formatExpense(report.getTotalExpense(), + report.getCurrency())); viewHolder.tvTotal.setTextColor(report.getTotal() >= 0 ? green : red); - viewHolder.tvTotal.setText(formatIncome(report.getTotal(), report.getCurrency())); + viewHolder.tvTotal.setText(formatController.formatIncome(report.getTotal(), + report.getCurrency())); } } - private String formatIncome(double amount, String currency) { - return (amount >= 0 ? "+ " : "- ") + String.format(Locale.getDefault(), "%.0f", Math.abs(amount)) - + " " + currency; - } - - private String formatExpense(double amount, String currency) { - return (amount > 0 ? "+ " : "- ") + String.format(Locale.getDefault(), "%.0f", Math.abs(amount)) - + " " + currency; - } - public static class ViewHolder { @Bind(R.id.tv_period) TextView tvPeriod; @@ -94,4 +94,4 @@ public ViewHolder(View view) { ButterKnife.bind(this, view); } } -} \ No newline at end of file +} diff --git a/app/src/main/res/layout/content_add_account.xml b/app/src/main/res/layout/content_add_account.xml index 39d0a50..9f463f7 100644 --- a/app/src/main/res/layout/content_add_account.xml +++ b/app/src/main/res/layout/content_add_account.xml @@ -33,7 +33,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/initial_sum" - android:inputType="numberSigned" + android:inputType="numberDecimal|numberSigned" android:maxLines="1" android:singleLine="true" /> diff --git a/app/src/main/res/layout/content_add_record.xml b/app/src/main/res/layout/content_add_record.xml index 8a642fa..d5fb9dd 100644 --- a/app/src/main/res/layout/content_add_record.xml +++ b/app/src/main/res/layout/content_add_record.xml @@ -29,9 +29,9 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/price" - android:inputType="numberSigned" android:maxLines="1" - android:singleLine="true" /> + android:singleLine="true" + android:inputType="numberDecimal" /> + android:singleLine="true" + android:inputType="numberDecimal" /> @@ -77,9 +77,9 @@ android:layout_height="wrap_content" android:ems="10" android:hint="@string/amount" - android:inputType="number" android:maxLines="1" - android:singleLine="true" /> + android:singleLine="true" + android:inputType="numberDecimal" /> diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 35b792c..727e0ce 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -63,4 +63,5 @@ Доходы Расходы + Точность отображения diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index 85db8a6..b062f81 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -63,4 +63,5 @@ Доходи Витрати + Точність відображення \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 65223a1..84d23a8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -69,5 +69,10 @@ Incomes Expenses + pref_display_precision + Display precision + 9.99$ = 10$ + 9.99$ = 9$ + 9.99$ = 9.99$ diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index b1d4ca8..1f68ed7 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -9,4 +9,9 @@ android:key="@string/pref_default_currency" android:summary="%s" android:title="@string/default_currency" /> + + \ No newline at end of file diff --git a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/CurrencyControllerTest.java b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/CurrencyControllerTest.java index 76b1c10..fc6ae90 100644 --- a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/CurrencyControllerTest.java +++ b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/CurrencyControllerTest.java @@ -26,7 +26,7 @@ public void testReadDefaultCurrency() throws Exception { Mockito.when(accountMock.readDefaultAccount()).thenReturn(null); assertEquals("NON", currencyController.readDefaultCurrency()); - Account account = new Account(1, "a1", 100, "ACM"); + Account account = new Account(1, "a1", 100, "ACM", 0); Mockito.when(prefsMock.readDefaultCurrency()).thenReturn(null); Mockito.when(accountMock.readDefaultAccount()).thenReturn(account); assertEquals(account.getCurrency(), currencyController.readDefaultCurrency()); diff --git a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/AccountControllerTest.java b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/AccountControllerTest.java index ae7e9d5..5556aa7 100644 --- a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/AccountControllerTest.java +++ b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/AccountControllerTest.java @@ -52,7 +52,7 @@ public void tearDown() throws Exception { @Test public void testRecordAdded() throws Exception { Category category = new Category(1, "c1"); - Account account = new Account(1, "a1", 100, "NON"); + Account account = new Account(1, "a1", 100, "NON", 0); repo.create(account); Record income = new Record(1, 1, Record.TYPE_INCOME, "income", category, 10, account, @@ -78,7 +78,7 @@ public void testRecordAdded() throws Exception { @Test public void testRecordDeleted() throws Exception { Category category = new Category(1, "c1"); - Account account = new Account(1, "a1", 100, "NON"); + Account account = new Account(1, "a1", 100, "NON", 0); repo.create(account); Record income = new Record(1, 1, Record.TYPE_INCOME, "income", category, 10, account, @@ -104,7 +104,7 @@ public void testRecordDeleted() throws Exception { @Test public void testRecordUpdated() throws Exception { Category category = new Category(1, "c1"); - Account account = new Account(1, "a1", 100, "NON"); + Account account = new Account(1, "a1", 100, "NON", 0); repo.create(account); Record incomeOld = new Record(1, 1, Record.TYPE_INCOME, "income", category, 10, account, @@ -139,8 +139,8 @@ public void testRecordUpdated() throws Exception { @Test public void testTransferDone() throws Exception { - Account account1 = new Account(1, "a1", 100, "NON"); - Account account2 = new Account(2, "a2", 0, "NON"); + Account account1 = new Account(1, "a1", 100, "NON", 0); + Account account2 = new Account(2, "a2", 0, "NON", 0); repo.create(account1); repo.create(account2); @@ -149,22 +149,22 @@ public void testTransferDone() throws Exception { assertEquals(100, account1.getCurSum()); assertEquals(0, account2.getCurSum()); - Transfer transfer = new Transfer(1, 1, account1.getId(), account2.getId(), 10, 10); + Transfer transfer = new Transfer(1, 1, account1.getId(), account2.getId(), 10, 10, 0, 0); accountController.transferDone(transfer); assertEquals(90, account1.getCurSum()); assertEquals(10, account2.getCurSum()); - transfer = new Transfer(2, 1, account1.getId(), account2.getId(), 10, 10); + transfer = new Transfer(2, 1, account1.getId(), account2.getId(), 10, 10, 0, 0); accountController.transferDone(transfer); assertEquals(80, account1.getCurSum()); assertEquals(20, account2.getCurSum()); - transfer = new Transfer(2, 1, account2.getId(), account1.getId(), 20, 20); + transfer = new Transfer(2, 1, account2.getId(), account1.getId(), 20, 20, 0, 0); accountController.transferDone(transfer); assertEquals(100, account1.getCurSum()); assertEquals(0, account2.getCurSum()); - transfer = new Transfer(2, 1, account1.getId(), account2.getId(), 0, 100); + transfer = new Transfer(2, 1, account1.getId(), account2.getId(), 0, 100, 0, 0); accountController.transferDone(transfer); assertEquals(100, account1.getCurSum()); assertEquals(100, account2.getCurSum()); @@ -174,11 +174,11 @@ public void testTransferDone() throws Exception { public void testReadDefaultAccount() throws Exception { assertNull(accountController.readDefaultAccount()); - Account account1 = new Account(1, "a1", 100, "NON"); + Account account1 = new Account(1, "a1", 100, "NON", 0); repo.create(account1); assertEquals(account1, accountController.readDefaultAccount()); - Account account2 = new Account(2, "a2", 0, "NON"); + Account account2 = new Account(2, "a2", 0, "NON", 0); repo.create(account2); assertEquals(account1, accountController.readDefaultAccount()); diff --git a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/RecordControllerTest.java b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/RecordControllerTest.java index 0fca728..ae7ee43 100644 --- a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/RecordControllerTest.java +++ b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/RecordControllerTest.java @@ -60,7 +60,7 @@ public void testCreate() throws Exception { Mockito.verify(accountMock, Mockito.times(0)).recordAdded(null); Category category = new Category(1, "c1"); - Account account = new Account(1, "a1", 100, "NON"); + Account account = new Account(1, "a1", 100, "NON", 0); Record record = new Record(1, 1, Record.TYPE_INCOME, "r1", category, 10, account, "NON"); Mockito.when(categoryMock.readOrCreate(category.getName())).thenReturn(category); @@ -78,7 +78,7 @@ public void testUpdate() throws Exception { Mockito.verify(accountMock, Mockito.times(0)).recordAdded(null); Category category = new Category(1, "c1"); - Account account = new Account(1, "a1", 100, "NON"); + Account account = new Account(1, "a1", 100, "NON", 0); Record record = new Record(1, 1, Record.TYPE_INCOME, "r1", category, 10, account, "NON"); Mockito.when(categoryMock.readOrCreate(category.getName())).thenReturn(category); @@ -95,7 +95,7 @@ public void testDelete() throws Exception { Mockito.verify(accountMock, Mockito.times(0)).recordDeleted(null); Category category = new Category(1, "c1"); - Account account = new Account(1, "a1", 100, "NON"); + Account account = new Account(1, "a1", 100, "NON", 0); Record record = new Record(1, 1, Record.TYPE_INCOME, "r1", category, 10, account, "NON"); assertFalse(recordController.delete(record)); @@ -112,8 +112,9 @@ public void testRead() throws Exception { assertNull(recordController.read(-1)); Category category = new Category(1, "c1"); - Account account = new Account(1, "a1", 100, "NON"); - Record recordNotFull = new Record(1, 1, Record.TYPE_INCOME, "r1", category.getId(), 10, account.getId(), "NON"); + Account account = new Account(1, "a1", 100, "NON", 0); + Record recordNotFull = new Record(1, 1, Record.TYPE_INCOME, "r1", category.getId(), 10, + account.getId(), "NON", 0); Record record = new Record(1, 1, Record.TYPE_INCOME, "r1", category, 10, account, "NON"); repo.create(recordNotFull); diff --git a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/TransferControllerTest.java b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/TransferControllerTest.java index 0e21750..bdaa485 100644 --- a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/TransferControllerTest.java +++ b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/controller/data/TransferControllerTest.java @@ -24,7 +24,7 @@ public class TransferControllerTest { @Test public void testCreate() throws Exception { AccountController mock = Mockito.mock(AccountController.class); - Transfer transfer = new Transfer(1, 1, 1, 2, 10, 20); + Transfer transfer = new Transfer(1, 1, 1, 2, 10, 20, 0, 0); Mockito.when(mock.transferDone(transfer)).thenReturn(true); Mockito.when(mock.transferDone(null)).thenReturn(false); diff --git a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/report/ExchangeRateProviderTest.java b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/report/ExchangeRateProviderTest.java index fa1e5ab..b44bcf0 100644 --- a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/report/ExchangeRateProviderTest.java +++ b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/report/ExchangeRateProviderTest.java @@ -68,12 +68,12 @@ public void testGetRate() throws Exception { provider = new ExchangeRateProvider("USD", rateController); assertEquals(new ExchangeRate(1, "UAH", "USD", 4), - provider.getRate(new Record(1, 0, 0, "", 1, 0, 0, "UAH"))); + provider.getRate(new Record(1, 0, 0, "", 1, 0, 0, "UAH", 0))); assertEquals(new ExchangeRate(0, "AFN", "USD", 3), - provider.getRate(new Record(1, 0, 0, "", 1, 0, 0, "AFN"))); + provider.getRate(new Record(1, 0, 0, "", 1, 0, 0, "AFN", 0))); - assertNull(provider.getRate(new Record(1, 0, 0, "", 1, 0, 0, "SMTH"))); + assertNull(provider.getRate(new Record(1, 0, 0, "", 1, 0, 0, "SMTH", 0))); } private static class TestRepo implements IRepo { diff --git a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/report/ReportTest.java b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/report/ReportTest.java index 9ab1d7f..75116de 100644 --- a/app/src/test/java/com/blogspot/e_kanivets/moneytracker/report/ReportTest.java +++ b/app/src/test/java/com/blogspot/e_kanivets/moneytracker/report/ReportTest.java @@ -175,8 +175,8 @@ public void testGetSummary() throws Exception { List recordList = new ArrayList<>(); Category category = new Category(1, "category"); - Account account1 = new Account(1, "account1", 100, "UAH"); - Account account2 = new Account(2, "account2", 100, "USD"); + Account account1 = new Account(1, "account1", 100, "UAH", 0); + Account account2 = new Account(2, "account2", 100, "USD", 0); Record record1 = new Record(1, 0, Record.TYPE_INCOME, "1", category, 10, account2, "USD"); recordList.add(record1); @@ -220,8 +220,8 @@ private List getRecordList() { List recordList = new ArrayList<>(); Category category = new Category(1, "category"); - Account account1 = new Account(1, "account1", 100, "UAH"); - Account account2 = new Account(2, "account2", 100, "USD"); + Account account1 = new Account(1, "account1", 100, "UAH", 0); + Account account2 = new Account(2, "account2", 100, "USD", 0); Record record1 = new Record(1, 0, Record.TYPE_INCOME, "1", category, 10, account2, "USD"); recordList.add(record1);