123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package de.tudarmstadt.informatik.hostage.ui.fragment;
- import android.app.Fragment;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import com.fortysevendeg.android.swipelistview.BaseSwipeListViewListener;
- import java.util.LinkedList;
- import java.util.List;
- import de.tudarmstadt.informatik.hostage.Hostage;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
- import de.tudarmstadt.informatik.hostage.model.Profile;
- import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
- import de.tudarmstadt.informatik.hostage.ui.activity.ProfileEditActivity;
- import de.tudarmstadt.informatik.hostage.ui.adapter.ProfileManagerListAdapter;
- import de.tudarmstadt.informatik.hostage.ui.swipelist.SwipeListView;
- /**
- * Displays a list of all available profiles and allows invocation of the edit activity for an profile
- *
- * @author Alexander Brakowski
- * @created 14.01.14 15:05
- */
- public class ProfileManagerFragment extends TrackerFragment {
- /**
- * The adapter for the profile list
- */
- private ProfileManagerListAdapter mAdapter;
- /**
- * Holds the shared preferences for the app
- */
- private SharedPreferences mSharedPreferences;
- public ProfileManagerFragment(){}
- /**
- * Holds the listview for the profile list
- */
- private SwipeListView list;
- /**
- * {@inheritDoc}
- */
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- super.onCreateView(inflater, container, savedInstanceState);
- getActivity().setTitle(getResources().getString(R.string.drawer_profile_manager));
- // show action bar menu items
- setHasOptionsMenu(true);
- // inflate the view
- View rootView = inflater.inflate(R.layout.fragment_profile_manager, container, false);
- list = (SwipeListView) rootView.findViewById(R.id.profile_manager_listview);
- final ProfileManager pmanager = ProfileManager.getInstance();
- pmanager.loadData();
- String sharedPreferencePath = MainActivity.getContext().getString(R.string.shared_preference_path);
- mSharedPreferences = MainActivity.getContext().getSharedPreferences(sharedPreferencePath, Hostage.MODE_PRIVATE);
- final List<Profile> strList = new LinkedList<Profile>(pmanager.getProfilesList());
- // show an help item in the listview to indicate, that the items in the list are swipeable
- if(strList.size() > 0 && !mSharedPreferences.getBoolean("dismissedProfileSwipeHelp", false)){
- Profile tProfile = new Profile();
- tProfile.mShowTooltip = true;
- strList.add(1, tProfile);
- }
- mAdapter = new ProfileManagerListAdapter(getActivity(), strList, list);
- pmanager.setProfileListAdapter(mAdapter);
- list.setAdapter(mAdapter);
- // add open and close actions to the items of the list view
- list.setSwipeListViewListener(new BaseSwipeListViewListener() {
- @Override
- public void onOpened(int position, boolean toRight){
- Profile profile = mAdapter.getItem(position);
- if(profile.mShowTooltip){
- mAdapter.remove(profile);
- strList.remove(profile);
- list.dismiss(position);
- mSharedPreferences.edit().putBoolean("dismissedProfileSwipeHelp", true).commit();
- }
- }
- @Override
- public void onClickFrontView(int position) {
- // active the pressed profile
- Profile profile = mAdapter.getItem(position);
- if(profile.mShowTooltip) return;
- pmanager.activateProfile(profile);
- mAdapter.notifyDataSetChanged();
- }
- });
- return rootView;
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onResume() {
- super.onResume();
- list.closeOpenedItems();
- mAdapter.notifyDataSetChanged();
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
- // Inflate the menu items for use in the action bar
- inflater.inflate(R.menu.profile_manager_actions, menu);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch(item.getItemId()){
- case R.id.profile_manager_action_add:
- Intent intent = new Intent(getActivity(), ProfileEditActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- getActivity().startActivity(intent);
- return true;
- }
- return false;
- }
- }
|