PageRenderTime 131ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Kanedias/WalletMaster
Java | 213 lines | 172 code | 34 blank | 7 comment | 10 complexity | 6b6f26b9d4a18352d4fd0bc3943c60a9 MD5 | raw file
  1. package com.adonai.wallet;
  2. import android.app.AlertDialog;
  3. import android.content.DialogInterface;
  4. import android.graphics.Color;
  5. import android.graphics.LinearGradient;
  6. import android.graphics.Shader;
  7. import android.graphics.drawable.ShapeDrawable;
  8. import android.graphics.drawable.shapes.RoundRectShape;
  9. import android.os.Bundle;
  10. import android.support.annotation.NonNull;
  11. import android.support.annotation.Nullable;
  12. import android.support.v4.app.LoaderManager;
  13. import android.support.v4.content.Loader;
  14. import android.view.LayoutInflater;
  15. import android.view.Menu;
  16. import android.view.MenuInflater;
  17. import android.view.MenuItem;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.widget.AdapterView;
  21. import android.widget.ListView;
  22. import android.widget.TextView;
  23. import com.adonai.wallet.database.AbstractAsyncLoader;
  24. import com.adonai.wallet.database.DbProvider;
  25. import com.adonai.wallet.database.EntityDao;
  26. import com.adonai.wallet.entities.Account;
  27. import com.adonai.wallet.adapters.UUIDCursorAdapter;
  28. import com.j256.ormlite.android.AndroidDatabaseResults;
  29. import java.sql.SQLException;
  30. import java.util.Arrays;
  31. import java.util.UUID;
  32. import static com.adonai.wallet.Utils.convertDpToPixel;
  33. /**
  34. * Fragment responsible for showing accounts list
  35. * and their context actions
  36. *
  37. * @author Adonai
  38. */
  39. public class AccountsFragment extends WalletBaseListFragment {
  40. private TextView budgetSum;
  41. private final EntityDeleteListener mAccountDeleter = new EntityDeleteListener(R.string.really_delete_account);
  42. private RetrieveContentsCallback mContentRetrieveCallback = new RetrieveContentsCallback();
  43. @Override
  44. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  45. setHasOptionsMenu(true);
  46. final View rootView = inflater.inflate(R.layout.accounts_flow, container, false);
  47. assert rootView != null;
  48. mEntityList = (ListView) rootView.findViewById(R.id.account_list);
  49. getLoaderManager().initLoader(Utils.ACCOUNTS_LOADER, Bundle.EMPTY, mContentRetrieveCallback);
  50. budgetSum = (TextView) rootView.findViewById(R.id.account_sum);
  51. return rootView;
  52. }
  53. @Override
  54. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  55. inflater.inflate(R.menu.accounts_flow, menu);
  56. super.onCreateOptionsMenu(menu, inflater);
  57. }
  58. @Override
  59. public boolean onOptionsItemSelected(MenuItem item) {
  60. switch (item.getItemId()) {
  61. case R.id.action_add_account:
  62. final AccountDialogFragment accountCreate = new AccountDialogFragment();
  63. accountCreate.show(getFragmentManager(), "accCreate");
  64. return true;
  65. default:
  66. return super.onOptionsItemSelected(item);
  67. }
  68. }
  69. private class AccountsAdapter extends UUIDCursorAdapter<Account> {
  70. public AccountsAdapter() {
  71. super(getActivity(), Account.class);
  72. }
  73. @Override
  74. @SuppressWarnings("deprecation") // for compat with older APIs
  75. public View getView(int position, View convertView, ViewGroup parent) {
  76. final View view;
  77. final LayoutInflater inflater = LayoutInflater.from(mContext);
  78. if (convertView == null)
  79. view = inflater.inflate(R.layout.account_list_item, parent, false);
  80. else
  81. view = convertView;
  82. try {
  83. ((AndroidDatabaseResults) mCursor.getRawResults()).moveAbsolute(position);
  84. Account acc = mCursor.current();
  85. final int accColor = acc.getColor();
  86. final float[] rounds = new float[8];
  87. Arrays.fill(rounds, convertDpToPixel(10f, mContext));
  88. final ShapeDrawable mDrawable = new ShapeDrawable(new RoundRectShape(rounds, null, null));
  89. mDrawable.getPaint().setShader(new LinearGradient(0, 0, mContext.getResources().getDisplayMetrics().widthPixels, 0,
  90. Color.argb(50, Color.red(accColor), Color.green(accColor), Color.blue(accColor)),
  91. Color.argb(0, Color.red(accColor), Color.green(accColor), Color.blue(accColor)), Shader.TileMode.CLAMP));
  92. view.findViewById(R.id.main_content_layout).setBackgroundDrawable(mDrawable);
  93. final TextView name = (TextView) view.findViewById(R.id.account_name_label);
  94. name.setText(acc.getName());
  95. final TextView description = (TextView) view.findViewById(R.id.account_description_label);
  96. description.setText(acc.getDescription());
  97. final TextView currency = (TextView) view.findViewById(R.id.account_currency_label);
  98. currency.setText(acc.getCurrency().getCode());
  99. final TextView amount = (TextView) view.findViewById(R.id.account_amount_label);
  100. amount.setText(acc.getAmount().toPlainString());
  101. } catch (SQLException e) {
  102. throw new RuntimeException(e);
  103. }
  104. return view;
  105. }
  106. }
  107. private class AccountLongClickListener implements AdapterView.OnItemLongClickListener {
  108. @Override
  109. public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
  110. final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
  111. alertDialog.setItems(R.array.entity_choice_common, new AccountChoice(position)).setTitle(R.string.select_action).create().show();
  112. return true;
  113. }
  114. }
  115. private class AccountClickListener implements AdapterView.OnItemClickListener {
  116. @Override
  117. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  118. final AccountsAdapter adapter = (AccountsAdapter) parent.getAdapter();
  119. final UUID accountID = adapter.getItemUUID(position);
  120. final Account managed = DbProvider.getHelper().getAccountDao().queryForId(accountID);
  121. if(managed != null) {
  122. OperationDialogFragment.forAccount(managed.getId().toString()).show(getFragmentManager(), "createOperation");
  123. }
  124. }
  125. }
  126. private class AccountChoice extends EntityChoice {
  127. public AccountChoice(int mItemPosition) {
  128. super(mItemPosition);
  129. }
  130. @Override
  131. public void onClick(DialogInterface dialog, int which) {
  132. final AccountsAdapter adapter = (AccountsAdapter) mEntityList.getAdapter();
  133. final UUID accID = adapter.getItemUUID(mItemPosition);
  134. final Account acc = DbProvider.getHelper().getAccountDao().queryForId(accID);
  135. switch (which) {
  136. case 0: // modify
  137. AccountDialogFragment.forAccount(acc.getId().toString()).show(getFragmentManager(), "accModify");
  138. break;
  139. case 1: // delete
  140. mAccountDeleter.handleRemoveAttempt(acc);
  141. break;
  142. }
  143. }
  144. }
  145. private class RetrieveContentsCallback implements LoaderManager.LoaderCallbacks<AccountsAdapter> {
  146. @Override
  147. public Loader<AccountsAdapter> onCreateLoader(int id, @NonNull final Bundle args) {
  148. AbstractAsyncLoader<AccountsAdapter> toRegister = new AbstractAsyncLoader<AccountsAdapter>(getActivity()) {
  149. @Nullable
  150. @Override
  151. public AccountsAdapter loadInBackground() {
  152. if(!isStarted()) // task was cancelled
  153. return null;
  154. // check the DB for accounts
  155. return new AccountsAdapter();
  156. }
  157. @Override
  158. protected void onForceLoad() {
  159. if(mData != null) { // close old adapter before loading new one
  160. mData.closeCursor();
  161. }
  162. super.onForceLoad();
  163. }
  164. };
  165. EntityDao<Account> accDao = DbProvider.getHelper().getDao(Account.class);
  166. accDao.registerObserver(toRegister);
  167. return toRegister;
  168. }
  169. @Override
  170. public void onLoadFinished(Loader<AccountsAdapter> loader, AccountsAdapter data) {
  171. mEntityList.setAdapter(data);
  172. mEntityList.setOnItemLongClickListener(new AccountLongClickListener());
  173. mEntityList.setOnItemClickListener(new AccountClickListener());
  174. }
  175. @Override
  176. public void onLoaderReset(Loader<AccountsAdapter> loader) {
  177. }
  178. }
  179. }