PageRenderTime 140ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/wallet/src/main/java/com/adonai/wallet/AccountDialogFragment.java

https://gitlab.com/Kanedias/WalletMaster
Java | 268 lines | 219 code | 41 blank | 8 comment | 23 complexity | 95638e618f80b8bc0d954d6f4ac87d77 MD5 | raw file
  1. package com.adonai.wallet;
  2. import android.app.AlertDialog;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.graphics.Color;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.BaseAdapter;
  13. import android.widget.EditText;
  14. import android.widget.Spinner;
  15. import android.widget.SpinnerAdapter;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18. import com.adonai.wallet.database.DbProvider;
  19. import com.adonai.wallet.entities.Account;
  20. import com.adonai.wallet.entities.Currency;
  21. import com.j256.ormlite.dao.CloseableIterator;
  22. import com.j256.ormlite.stmt.QueryBuilder;
  23. import java.math.BigDecimal;
  24. import java.sql.SQLException;
  25. import java.util.UUID;
  26. /**
  27. * Dialog fragment showing window for account modifying/adding
  28. *
  29. * @author adonai
  30. */
  31. public class AccountDialogFragment extends WalletBaseDialogFragment implements DialogInterface.OnClickListener {
  32. private final static String ACCOUNT_REFERENCE = "account.reference";
  33. private EditText mAccountName;
  34. private EditText mAccountDescription;
  35. private Spinner mCurrencySelector;
  36. private Spinner mColorSelector;
  37. private EditText mInitialAmount;
  38. private CurrencyAdapter mCurrAdapter;
  39. public static AccountDialogFragment forAccount(String accountId) {
  40. final AccountDialogFragment fragment = new AccountDialogFragment();
  41. final Bundle args = new Bundle();
  42. args.putString(ACCOUNT_REFERENCE, accountId);
  43. fragment.setArguments(args);
  44. return fragment;
  45. }
  46. @Override
  47. public Dialog onCreateDialog(Bundle savedInstanceState) {
  48. final View dialog = getActivity().getLayoutInflater().inflate(R.layout.account_create_modify_dialog, null);
  49. assert dialog != null;
  50. mAccountName = (EditText) dialog.findViewById(R.id.name_edit);
  51. mAccountDescription = (EditText) dialog.findViewById(R.id.description_edit);
  52. mCurrencySelector = (Spinner) dialog.findViewById(R.id.currency_spinner);
  53. mColorSelector = (Spinner) dialog.findViewById(R.id.color_spinner);
  54. mInitialAmount = (EditText) dialog.findViewById(R.id.initial_amount_edit);
  55. mCurrAdapter = new CurrencyAdapter();
  56. mCurrencySelector.setAdapter(mCurrAdapter);
  57. final ColorSpinnerAdapter mColorAdapter = new ColorSpinnerAdapter(getResources().getStringArray(R.array.colors));
  58. mColorSelector.setAdapter(mColorAdapter);
  59. final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  60. // if we are modifying existing account
  61. if(getArguments() != null && getArguments().containsKey(ACCOUNT_REFERENCE)) {
  62. Account modAcc = DbProvider.getHelper().getAccountDao().queryForId(UUID.fromString(getArguments().getString(ACCOUNT_REFERENCE)));
  63. builder.setPositiveButton(R.string.confirm, this);
  64. builder.setTitle(R.string.edit_account).setView(dialog);
  65. mAccountName.setText(modAcc.getName());
  66. mAccountDescription.setText(modAcc.getDescription());
  67. mCurrencySelector.setSelection(mCurrAdapter.getPosition(modAcc.getCurrency().getCode()));
  68. mColorSelector.setSelection(mColorAdapter.getPosition(String.format("#%06X", (0xFFFFFF & modAcc.getColor()))));
  69. mInitialAmount.setText(modAcc.getAmount().toPlainString());
  70. } else {
  71. builder.setPositiveButton(R.string.create, this);
  72. builder.setTitle(R.string.create_new_account).setView(dialog);
  73. mCurrencySelector.setSelection(mCurrAdapter.getPosition("RUB"));
  74. }
  75. return builder.create();
  76. }
  77. private void fillAccountFieldsFromGUI(Account acc) {
  78. acc.setName(mAccountName.getText().toString());
  79. acc.setDescription(mAccountDescription.getText().toString());
  80. acc.setCurrency((Currency) mCurrencySelector.getSelectedItem());
  81. acc.setAmount(Utils.getValue(mInitialAmount.getText().toString(), BigDecimal.ZERO));
  82. acc.setColor(Color.parseColor((String) mColorSelector.getSelectedItem()));
  83. }
  84. @Override
  85. public void onClick(DialogInterface dialog, int which) {
  86. if(mAccountName.getText() == null || mAccountName.getText().toString().equals("")) {
  87. Toast.makeText(getActivity(), R.string.account_name_invalid, Toast.LENGTH_SHORT).show();
  88. return;
  89. }
  90. Account tmp;
  91. if(getArguments() != null && getArguments().containsKey(ACCOUNT_REFERENCE)) { // modifying existing account
  92. tmp = DbProvider.getHelper().getAccountDao().queryForId(UUID.fromString(getArguments().getString(ACCOUNT_REFERENCE)));
  93. } else // creating new
  94. tmp = new Account();
  95. fillAccountFieldsFromGUI(tmp);
  96. DbProvider.getHelper().getAccountDao().createOrUpdate(tmp);
  97. }
  98. public class ColorSpinnerAdapter extends ArrayAdapter<String> implements SpinnerAdapter {
  99. public ColorSpinnerAdapter(String[] objects) {
  100. super(getActivity(), R.layout.color_list_item, objects);
  101. }
  102. @Override
  103. public View getDropDownView(int position, View convertView, ViewGroup parent) {
  104. View rowView = convertView;
  105. if (rowView == null) {
  106. LayoutInflater inflater = getActivity().getLayoutInflater();
  107. rowView = inflater.inflate(R.layout.color_list_item, null);
  108. rowView.findViewById(R.id.color_view).setBackgroundColor(Color.parseColor(getItem(position)));
  109. } else
  110. rowView.findViewById(R.id.color_view).setBackgroundColor(Color.parseColor(getItem(position)));
  111. return rowView;
  112. }
  113. @Override
  114. public View getView(int position, View convertView, ViewGroup parent) {
  115. View rowView = convertView;
  116. if (rowView == null) {
  117. //Color
  118. // Get a new instance of the row layout view
  119. LayoutInflater inflater = getActivity().getLayoutInflater();
  120. rowView = inflater.inflate(R.layout.color_list_item, parent, false);
  121. rowView.findViewById(R.id.color_view).setBackgroundColor(Color.parseColor(getItem(position)));
  122. } else
  123. rowView.findViewById(R.id.color_view).setBackgroundColor(Color.parseColor(getItem(position)));
  124. return rowView;
  125. }
  126. }
  127. public class CurrencyAdapter extends BaseAdapter {
  128. private final Context mContext;
  129. private final QueryBuilder<Currency, String> mQuery;
  130. private final CloseableIterator<Currency> mCursor;
  131. public CurrencyAdapter() {
  132. try {
  133. mContext = getActivity();
  134. mQuery = DbProvider.getHelper().getCurrencyDao().queryBuilder();
  135. mCursor = mQuery.iterator();
  136. } catch (SQLException e) { // should not happen
  137. throw new RuntimeException(e);
  138. }
  139. }
  140. @Override
  141. public View getView(int position, View convertView, ViewGroup parent) {
  142. final View view;
  143. final LayoutInflater inflater = LayoutInflater.from(mContext);
  144. if (convertView == null)
  145. view = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
  146. else
  147. view = convertView;
  148. try {
  149. mCursor.first();
  150. Currency cur = mCursor.moveRelative(position);
  151. final TextView code = (TextView) view.findViewById(android.R.id.text1);
  152. code.setText(cur.getCode());
  153. } catch (SQLException e) {
  154. throw new RuntimeException(e);
  155. }
  156. return view;
  157. }
  158. @Override
  159. public View getDropDownView(int position, View convertView, ViewGroup parent) {
  160. final View view;
  161. final LayoutInflater inflater = LayoutInflater.from(mContext);
  162. if (convertView == null)
  163. view = inflater.inflate(R.layout.currency_list_item, parent, false);
  164. else
  165. view = convertView;
  166. try {
  167. mCursor.first();
  168. Currency cur = mCursor.moveRelative(position);
  169. final TextView code = (TextView) view.findViewById(R.id.curr_caption_text);
  170. code.setText(cur.getCode());
  171. final TextView desc = (TextView) view.findViewById(R.id.curr_description_text);
  172. desc.setText(cur.getDescription());
  173. final TextView usedIn = (TextView) view.findViewById(R.id.curr_usedin_text);
  174. usedIn.setText(cur.getUsedIn());
  175. } catch (SQLException e) {
  176. throw new RuntimeException(e);
  177. }
  178. return view;
  179. }
  180. @Override
  181. public int getCount() {
  182. try {
  183. return (int) mQuery.countOf();
  184. } catch (SQLException e) {
  185. throw new RuntimeException(e);
  186. }
  187. }
  188. @Override
  189. public Currency getItem(int position) {
  190. try {
  191. mCursor.first();
  192. return mCursor.moveRelative(position);
  193. } catch (SQLException e) {
  194. throw new RuntimeException(e);
  195. }
  196. }
  197. @Override
  198. public long getItemId(int position) {
  199. return position;
  200. }
  201. public int getPosition(String code) {
  202. try {
  203. int pos = 0;
  204. Currency first = mCursor.first();
  205. if(first.getCode().equals(code))
  206. return pos;
  207. while (mCursor.hasNext()) {
  208. ++pos;
  209. Currency entity = mCursor.next();
  210. if(entity.getCode().equals(code))
  211. return pos;
  212. }
  213. } catch (SQLException e) {
  214. return -1;
  215. }
  216. return -1;
  217. }
  218. public void closeCursor() {
  219. mCursor.closeQuietly();
  220. }
  221. }
  222. @Override
  223. public void onDestroyView() {
  224. super.onDestroyView();
  225. mCurrAdapter.closeCursor();
  226. }
  227. }