Browse Source

Profile manager without presisting data finished

Alexander Brakowski 11 năm trước cách đây
mục cha
commit
e39d1d9594

+ 59 - 58
res/layout/profile_manager_list_item.xml

@@ -4,7 +4,7 @@
               android:layout_width="wrap_content"
               android:layout_height="wrap_content">
 
-    <RelativeLayout
+	<RelativeLayout
         android:id="@+id/swipelist_backview"
         android:orientation="horizontal"
         android:layout_width="match_parent"
@@ -33,7 +33,8 @@
                 android:layout_height="match_parent"
                 android:background="@color/light_grey"
                 android:layout_marginTop="10dp"
-                android:layout_marginBottom="10dp"/>
+                android:layout_marginBottom="10dp"
+		        android:id="@+id/profile_manager_item_seperator"/>
 
             <ImageButton
                 android:layout_width="0dp"
@@ -51,66 +52,66 @@
         </LinearLayout>
 
     </RelativeLayout>
-    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-                    android:id="@+id/swipelist_frontview"
-                    android:layout_width="match_parent"
-                    android:layout_height="wrap_content"
-                    android:padding="5dp"
-                    android:baselineAligned="false"
-                    android:background="@drawable/panel_bg_selector">
+	<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+	                android:id="@+id/swipelist_frontview"
+	                android:layout_width="match_parent"
+	                android:layout_height="wrap_content"
+	                android:padding="5dp"
+	                android:baselineAligned="false"
+	                android:background="@drawable/panel_bg_selector">
 
-        <TextView
-            android:id="@+id/profile_manager_item_label"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:textAppearance="?android:attr/textAppearanceListItemSmall"
-            android:gravity="center_vertical"
-            android:paddingLeft="16dp"
-            android:paddingRight="16dp"
-            android:textColor="?android:attr/colorForeground"
-            android:minHeight="?android:attr/listPreferredItemHeightSmall"
-            android:text="Sample title"
-            android:layout_alignParentTop="true"
-            android:layout_toRightOf="@+id/profile_manager_item_image" />
+	    <TextView
+	        android:id="@+id/profile_manager_item_label"
+	        android:layout_width="match_parent"
+	        android:layout_height="wrap_content"
+	        android:textAppearance="?android:attr/textAppearanceListItemSmall"
+	        android:gravity="center_vertical"
+	        android:paddingLeft="16dp"
+	        android:paddingRight="16dp"
+	        android:textColor="?android:attr/colorForeground"
+	        android:minHeight="?android:attr/listPreferredItemHeightSmall"
+	        android:text="Sample title"
+	        android:layout_alignParentTop="true"
+	        android:layout_toRightOf="@+id/profile_manager_item_image" />
 
-        <TextView
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:text="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
-            android:id="@+id/profile_manager_item_text"
-            android:paddingLeft="16dp"
-            android:paddingRight="16dp"
-            android:paddingBottom="16dp"
-            android:textColor="#808080"
-            android:visibility="visible"
-            android:singleLine="false"
-            android:layout_alignParentLeft="true"
-            android:layout_alignParentStart="true"
-            android:layout_below="@+id/profile_manager_item_label"
-            android:paddingTop="10dp"
-            android:layout_toLeftOf="@+id/profile_manager_item_activated"/>
+	    <TextView
+	        android:layout_width="match_parent"
+	        android:layout_height="wrap_content"
+	        android:text="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
+	        android:id="@+id/profile_manager_item_text"
+	        android:paddingLeft="16dp"
+	        android:paddingRight="16dp"
+	        android:paddingBottom="16dp"
+	        android:textColor="#808080"
+	        android:visibility="visible"
+	        android:singleLine="false"
+	        android:layout_alignParentLeft="true"
+	        android:layout_alignParentStart="true"
+	        android:layout_below="@+id/profile_manager_item_label"
+	        android:paddingTop="10dp"
+	        android:layout_toLeftOf="@+id/profile_manager_item_activated"/>
 
-        <ImageView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:id="@+id/profile_manager_item_activated"
-            android:src="@drawable/ic_action_accept"
-            android:layout_centerVertical="true"
-            android:layout_alignParentRight="true"
-            android:layout_alignParentEnd="true"
-            android:layout_marginRight="20dp"
-            android:visibility="visible"
-            android:layout_marginLeft="20dp"/>
+	    <ImageView
+	        android:layout_width="wrap_content"
+	        android:layout_height="wrap_content"
+	        android:id="@+id/profile_manager_item_activated"
+	        android:src="@drawable/ic_action_accept"
+	        android:layout_centerVertical="true"
+	        android:layout_alignParentRight="true"
+	        android:layout_alignParentEnd="true"
+	        android:layout_marginRight="20dp"
+	        android:visibility="visible"
+	        android:layout_marginLeft="20dp"/>
 
-        <ImageView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:id="@+id/profile_manager_item_image"
-            android:src="@drawable/ic_launcher"
-            android:layout_above="@+id/profile_manager_item_text"
-            android:layout_alignParentLeft="true"
-            android:layout_alignParentStart="true" />
+	    <ImageView
+	        android:layout_width="wrap_content"
+	        android:layout_height="wrap_content"
+	        android:id="@+id/profile_manager_item_image"
+	        android:src="@drawable/ic_launcher"
+	        android:layout_above="@+id/profile_manager_item_text"
+	        android:layout_alignParentLeft="true"
+	        android:layout_alignParentStart="true" />
 
-    </RelativeLayout>
+	</RelativeLayout>
 
 </FrameLayout>

+ 61 - 5
src/de/tudarmstadt/informatik/hostage/dao/ProfileManager.java

@@ -8,6 +8,7 @@ import java.util.Map;
 
 import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.model.Profile;
+import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter;
 import de.tudarmstadt.informatik.hostage.ui2.model.ProfileListItem;
 
 /**
@@ -18,6 +19,10 @@ public class ProfileManager {
 	private static ProfileManager INSTANCE = null;
 
 	private Map<Integer, Profile> mProfiles;
+	private ProfileManagerListAdapter mProfileListAdapter = null;
+
+	private int mProfileId = 4;
+	private Profile mCurrentActivatedProfile = null;
 
 	public static ProfileManager getInstance(){
 		if(INSTANCE == null){
@@ -33,31 +38,41 @@ public class ProfileManager {
 				0,
 				"Windows Vista",
 				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
-				R.drawable.ic_profile_vista));
+				R.drawable.ic_profile_vista,
+				false
+		));
 
 		this.mProfiles.put(1, new Profile(
 				1,
 				"Windows 7",
 				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
-				R.drawable.ic_profile_w7));
+				R.drawable.ic_profile_w7,
+				false
+		));
 
 		this.mProfiles.put(2, new Profile(
 				2,
 				"Unix Distro",
 				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
-				R.drawable.ic_profile_unix));
+				R.drawable.ic_profile_unix,
+				false
+		));
 
 		this.mProfiles.put(3, new Profile(
 				3,
 				"Random",
 				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
-				R.drawable.ic_service_green));
+				R.drawable.ic_service_green,
+				false
+		));
 
 		this.mProfiles.put(4, new Profile(
 				4,
 				"Mix",
 				"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
-				R.drawable.ic_service_green));
+				R.drawable.ic_service_green,
+				false
+		));
 	}
 
 	public List<Profile> getProfilesList(){
@@ -79,4 +94,45 @@ public class ProfileManager {
 
 		return null;
 	}
+
+	public void addProfile(Profile profile){
+		if(profile.id == -1){
+			profile.id = mProfileId++;
+		}
+
+		this.mProfiles.put(profile.id, profile);
+		if(this.mProfileListAdapter != null){
+			this.mProfileListAdapter.add(profile);
+		}
+	}
+
+	public void deleteProfile(Profile profile){
+		if(this.mProfiles.containsKey(profile.id)){
+			this.mProfiles.remove(profile.id);
+
+			if(this.mProfileListAdapter != null){
+				this.mProfileListAdapter.remove(profile);
+			}
+		}
+	}
+
+	public void activeProfile(Profile profile){
+		if(profile.equals(this.mCurrentActivatedProfile)) return;
+
+		if(this.mCurrentActivatedProfile != null){
+			this.mCurrentActivatedProfile.activated = false;
+		}
+
+		profile.activated = true;
+		this.mCurrentActivatedProfile = profile;
+	}
+
+	public void setProfileListAdapter(ProfileManagerListAdapter profileListAdapter){
+		this.mProfileListAdapter = profileListAdapter;
+	}
+
+
+	public ProfileManagerListAdapter getProfileListAdapter(){
+		return this.mProfileListAdapter;
+	}
 }

+ 13 - 3
src/de/tudarmstadt/informatik/hostage/model/Profile.java

@@ -17,17 +17,19 @@ public class Profile {
 	public Bitmap icon;
 
 	public boolean isBackVisible = false;
+	public boolean editable = false;
 
-	public Profile(int id, String text, String label, Bitmap icon){
+	public Profile(int id, String text, String label, Bitmap icon, boolean editable){
 		this.id = id;
 		this.text = text;
 		this.label = label;
 		this.activated = false;
 		this.icon = icon;
+		this.editable = editable;
 	}
 
-	public Profile(int id, String text, String label, int icon){
-		this(id, text, label, BitmapFactory.decodeResource(MainActivity.context.getResources(), icon));
+	public Profile(int id, String text, String label, int icon, boolean editable){
+		this(id, text, label, BitmapFactory.decodeResource(MainActivity.context.getResources(), icon), editable);
 	}
 
 	public void setIcon(Bitmap bitmap){
@@ -37,4 +39,12 @@ public class Profile {
 	public void setIcon(int icon){
 		this.icon = BitmapFactory.decodeResource(MainActivity.context.getResources(), icon);
 	}
+
+	public boolean isEditable(){
+		return this.editable;
+	}
+
+	public Profile cloneProfile(){
+		return new Profile(id, text, label, icon, editable);
+	}
 }

+ 54 - 3
src/de/tudarmstadt/informatik/hostage/ui2/adapter/ProfileManagerListAdapter.java

@@ -1,6 +1,8 @@
 package de.tudarmstadt.informatik.hostage.ui2.adapter;
 
+import android.app.AlertDialog;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.content.Intent;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -16,7 +18,10 @@ import com.fortysevendeg.android.swipelistview.SwipeListView;
 import java.util.List;
 
 import de.tudarmstadt.informatik.hostage.R;
+import de.tudarmstadt.informatik.hostage.dao.ProfileManager;
 import de.tudarmstadt.informatik.hostage.model.Profile;
+import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
+import de.tudarmstadt.informatik.hostage.ui2.fragment.ProfileManagerFragment;
 import de.tudarmstadt.informatik.hostage.ui2.model.ProfileListItem;
 import de.tudarmstadt.informatik.hostage.ui2.activity.ProfileEditActivity;
 
@@ -32,6 +37,7 @@ public class ProfileManagerListAdapter extends ArrayAdapter<Profile> {
 		public ImageView itemIcon;
 		public ImageButton buttonEdit;
 		public ImageButton buttonDelete;
+		public View seperator;
 	}
 
     private final Context context;
@@ -64,6 +70,7 @@ public class ProfileManagerListAdapter extends ArrayAdapter<Profile> {
 		    holder.itemIcon = (ImageView) rowView.findViewById(R.id.profile_manager_item_image);
 		    holder.buttonEdit = (ImageButton) rowView.findViewById(R.id.profile_manager_item_button_edit);
 		    holder.buttonDelete = (ImageButton) rowView.findViewById(R.id.profile_manager_item_button_delete);
+			holder.seperator = rowView.findViewById(R.id.profile_manager_item_seperator);
 
 		    rowView.setTag(holder);
 	    } else {
@@ -86,15 +93,59 @@ public class ProfileManagerListAdapter extends ArrayAdapter<Profile> {
 			}
 		});
 
+		holder.buttonDelete.setOnClickListener(new View.OnClickListener() {
+			@Override
+			public void onClick(View v) {
+				new AlertDialog.Builder(context)
+						.setTitle("Delete profile")
+						.setMessage("Do you really want to delete this profile?")
+						.setNegativeButton("No", new DialogInterface.OnClickListener() {
+							@Override
+							public void onClick(DialogInterface dialog, int which) {
+
+							}
+						})
+						.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
+							public void onClick(DialogInterface dialog, int which) {
+								ProfileManager profileManager = ProfileManager.getInstance();
+
+								profileManager.deleteProfile(item);
+								profileManager.getProfileListAdapter().notifyDataSetChanged();
+
+							}
+						})
+						.setIcon(android.R.drawable.ic_dialog_alert)
+						.show();
+			}
+		});
+
+		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.textView.getLayoutParams();
+
         if(!item.activated){
-            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams )holder.textView.getLayoutParams();
-            lp.setMargins(0,0,0,0);
+            lp.setMargins(0, 0, 0, 0);
 
 	        holder.textView.setLayoutParams(lp);
 
 	        holder.imageSelected.setVisibility(View.GONE);
-        }
+        } else {
+			/*float d = context.getResources().getDisplayMetrics().density;
+			int dm = (int)(20 * d);
+
+			lp.setMargins(0,0,dm,0);
+
+			holder.textView.setLayoutParams(lp);*/
+
+			holder.imageSelected.setVisibility(View.VISIBLE);
+		}
+
 
+		if(!item.isEditable()){
+			holder.buttonDelete.setVisibility(View.GONE);
+			holder.seperator.setVisibility(View.GONE);
+		} else {
+			holder.buttonDelete.setVisibility(View.VISIBLE);
+			holder.seperator.setVisibility(View.VISIBLE);
+		}
 
         return rowView;
     }

+ 43 - 2
src/de/tudarmstadt/informatik/hostage/ui2/fragment/ProfileEditFragment.java

@@ -5,9 +5,10 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
+import android.preference.EditTextPreference;
+import android.preference.Preference;
 import android.preference.PreferenceFragment;
 import android.preference.PreferenceManager;
-import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.LinearLayout;
@@ -35,6 +36,42 @@ public class ProfileEditFragment extends PreferenceFragment implements
 		View actionBarButtons = mInflater.inflate(R.layout.actionbar_donebar, new LinearLayout(getActivity()), false);
 		getActivity().getActionBar().setCustomView(actionBarButtons);
 
+		View doneButton = actionBarButtons.findViewById(R.id.action_done);
+		View cancelButton = actionBarButtons.findViewById(R.id.action_cancel);
+
+		doneButton.setOnClickListener(new View.OnClickListener() {
+			@Override
+			public void onClick(View v) {
+				SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
+				ProfileManager pmanager = ProfileManager.getInstance();
+
+				Profile profile = getProfile();
+				boolean createNew = false;
+
+				if(!profile.isEditable()){
+					profile = profile.cloneProfile();
+					profile.editable = true;
+					createNew = true;
+				}
+
+				profile.text = prefs.getString("pref_profile_general_name", profile.text);
+
+				if(createNew){
+					profile.id = -1;
+					pmanager.addProfile(profile);
+				}
+
+				getActivity().finish();
+			}
+		});
+
+		cancelButton.setOnClickListener(new View.OnClickListener() {
+			@Override
+			public void onClick(View v) {
+				getActivity().finish();
+			}
+		});
+
 		Profile profile = getProfile();
 		SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()).edit();
 
@@ -86,6 +123,10 @@ public class ProfileEditFragment extends PreferenceFragment implements
 	 */
 	@Override
 	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
-		Log.i("settings", "preference changed: " + key);
+		Preference p = findPreference(key);
+
+		if(p instanceof EditTextPreference){
+			p.setSummary(sharedPreferences.getString(key, ""));
+		}
 	}
 }

+ 24 - 7
src/de/tudarmstadt/informatik/hostage/ui2/fragment/ProfileManagerFragment.java

@@ -5,26 +5,25 @@ import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.ListView;
 
+import com.fortysevendeg.android.swipelistview.BaseSwipeListViewListener;
 import com.fortysevendeg.android.swipelistview.SwipeListView;
 
-import java.util.ArrayList;
 import java.util.List;
 
 import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.dao.ProfileManager;
 import de.tudarmstadt.informatik.hostage.model.Profile;
 import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter;
-import de.tudarmstadt.informatik.hostage.ui2.model.ProfileListItem;
 
 /**
  * @author Alexander Brakowski
  * @created 14.01.14 15:05
  */
 public class ProfileManagerFragment extends Fragment {
+	private ProfileManagerListAdapter mAdapter;
 
-    public ProfileManagerFragment(){}
+	public ProfileManagerFragment(){}
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -35,13 +34,31 @@ public class ProfileManagerFragment extends Fragment {
         View rootView = inflater.inflate(R.layout.fragment_profile_manager, container, false);
 	    SwipeListView list = (SwipeListView) rootView.findViewById(R.id.profile_manager_listview);
 
-		ProfileManager pmanager = ProfileManager.getInstance();
+		final ProfileManager pmanager = ProfileManager.getInstance();
 
         List<Profile> strList = pmanager.getProfilesList();
 
-        ProfileManagerListAdapter adapter = new ProfileManagerListAdapter(getActivity().getBaseContext(), strList);
-        list.setAdapter(adapter);
+		mAdapter = new ProfileManagerListAdapter(getActivity(), strList);
+		pmanager.setProfileListAdapter(mAdapter);
+
+        list.setAdapter(mAdapter);
+
+		list.setSwipeListViewListener(new BaseSwipeListViewListener() {
+			@Override
+			public void onClickFrontView(int position) {
+				Profile profile = mAdapter.getItem(position);
+				pmanager.activeProfile(profile);
+
+				mAdapter.notifyDataSetChanged();
+			}
+		});
 
         return rootView;
     }
+
+	@Override
+	public void onResume() {
+		super.onResume();
+		mAdapter.notifyDataSetChanged();
+	}
 }