ProfileManagerFragment.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.app.Fragment;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.Menu;
  8. import android.view.MenuInflater;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import com.fortysevendeg.android.swipelistview.BaseSwipeListViewListener;
  13. import java.util.LinkedList;
  14. import java.util.List;
  15. import de.tudarmstadt.informatik.hostage.Hostage;
  16. import de.tudarmstadt.informatik.hostage.R;
  17. import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
  18. import de.tudarmstadt.informatik.hostage.model.Profile;
  19. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  20. import de.tudarmstadt.informatik.hostage.ui2.activity.ProfileEditActivity;
  21. import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter;
  22. import de.tudarmstadt.informatik.hostage.ui2.swipelist.SwipeListView;
  23. /**
  24. * Displays a list of all available profiles and allows invocation of the edit activity for an profile
  25. *
  26. * @author Alexander Brakowski
  27. * @created 14.01.14 15:05
  28. */
  29. public class ProfileManagerFragment extends TrackerFragment {
  30. /**
  31. * The adapter for the profile list
  32. */
  33. private ProfileManagerListAdapter mAdapter;
  34. /**
  35. * Holds the shared preferences for the app
  36. */
  37. private SharedPreferences mSharedPreferences;
  38. public ProfileManagerFragment(){}
  39. /**
  40. * Holds the listview for the profile list
  41. */
  42. private SwipeListView list;
  43. /**
  44. * {@inheritDoc}
  45. */
  46. @Override
  47. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  48. Bundle savedInstanceState) {
  49. super.onCreateView(inflater, container, savedInstanceState);
  50. getActivity().setTitle(getResources().getString(R.string.drawer_profile_manager));
  51. // show action bar menu items
  52. setHasOptionsMenu(true);
  53. // inflate the view
  54. View rootView = inflater.inflate(R.layout.fragment_profile_manager, container, false);
  55. list = (SwipeListView) rootView.findViewById(R.id.profile_manager_listview);
  56. final ProfileManager pmanager = ProfileManager.getInstance();
  57. pmanager.loadData();
  58. String sharedPreferencePath = MainActivity.getContext().getString(R.string.shared_preference_path);
  59. mSharedPreferences = MainActivity.getContext().getSharedPreferences(sharedPreferencePath, Hostage.MODE_PRIVATE);
  60. final List<Profile> strList = new LinkedList<Profile>(pmanager.getProfilesList());
  61. // show an help item in the listview to indicate, that the items in the list are swipeable
  62. if(strList.size() > 0 && !mSharedPreferences.getBoolean("dismissedProfileSwipeHelp", false)){
  63. Profile tProfile = new Profile();
  64. tProfile.mShowTooltip = true;
  65. strList.add(1, tProfile);
  66. }
  67. mAdapter = new ProfileManagerListAdapter(getActivity(), strList, list);
  68. pmanager.setProfileListAdapter(mAdapter);
  69. list.setAdapter(mAdapter);
  70. // add open and close actions to the items of the list view
  71. list.setSwipeListViewListener(new BaseSwipeListViewListener() {
  72. @Override
  73. public void onOpened(int position, boolean toRight){
  74. Profile profile = mAdapter.getItem(position);
  75. if(profile.mShowTooltip){
  76. mAdapter.remove(profile);
  77. strList.remove(profile);
  78. list.dismiss(position);
  79. mSharedPreferences.edit().putBoolean("dismissedProfileSwipeHelp", true).commit();
  80. }
  81. }
  82. @Override
  83. public void onClickFrontView(int position) {
  84. // active the pressed profile
  85. Profile profile = mAdapter.getItem(position);
  86. if(profile.mShowTooltip) return;
  87. pmanager.activateProfile(profile);
  88. mAdapter.notifyDataSetChanged();
  89. }
  90. });
  91. return rootView;
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. @Override
  97. public void onResume() {
  98. super.onResume();
  99. list.closeOpenedItems();
  100. mAdapter.notifyDataSetChanged();
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. @Override
  106. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  107. // Inflate the menu items for use in the action bar
  108. inflater.inflate(R.menu.profile_manager_actions, menu);
  109. }
  110. /**
  111. * {@inheritDoc}
  112. */
  113. @Override
  114. public boolean onOptionsItemSelected(MenuItem item) {
  115. switch(item.getItemId()){
  116. case R.id.profile_manager_action_add:
  117. Intent intent = new Intent(getActivity(), ProfileEditActivity.class);
  118. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  119. getActivity().startActivity(intent);
  120. return true;
  121. }
  122. return false;
  123. }
  124. }