Browse Source

[회원가입][New] 리스트 아이템 타입에 따른 데이터 전달 방식
- 데이터를 String 으로 저장하고 각 아이템에서 해당 Bean으로 변환하여 사용

hyodong.min 7 years ago
parent
commit
838820fb5f

+ 2 - 1
app/build.gradle

@@ -80,10 +80,11 @@ android {
 dependencies {
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     implementation 'com.android.support:appcompat-v7:27.1.1'
-    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
+    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
     implementation 'com.android.support:support-v4:27.1.1'
     implementation 'com.google.android.gms:play-services-plus:15.0.1'
     implementation 'com.android.support:recyclerview-v7:27.1.1'
+    implementation 'com.google.code.gson:gson:2.8.5'
 
     /*********************************
      * UNIT TEST

+ 1 - 2
app/src/main/java/kr/co/zumo/app/lifeplus/view/fragment/signup/BaseViewHolder.java

@@ -4,9 +4,8 @@ import android.support.v7.widget.RecyclerView;
 import android.view.View;
 
 /**
- * [클래스 개요]
+ * BaseViewHolder
  * <pre>
- * [클래스 상세 설명 또는 사용 시 주의점]
  * </pre>
  *
  * @author 하세미

+ 12 - 17
app/src/main/java/kr/co/zumo/app/lifeplus/view/fragment/signup/SignUpAdapter.java

@@ -7,11 +7,12 @@ import android.view.View;
 import android.view.ViewGroup;
 
 import kr.co.zumo.app.R;
+import kr.co.zumo.app.lifeplus.view.model.SignUpListItem;
+import kr.co.zumo.app.lifeplus.view.model.SignUpViewModel;
 
 /**
- * [클래스 개요]
+ * 회원 가입 리스트 뷰 화면의 관리하는 어댑터 클래스
  * <pre>
- * [클래스 상세 설명 또는 사용 시 주의점]
  * </pre>
  *
  * @author 하세미
@@ -21,10 +22,10 @@ import kr.co.zumo.app.R;
  */
 public class SignUpAdapter extends RecyclerView.Adapter {
 
-  private static final int SIGN_UP_HELLO = 0;
-  private static final int SIGN_UP_BENEFIT = 1;
+  private SignUpViewModel signUpViewModel;
 
-  public SignUpAdapter() {
+  public SignUpAdapter(SignUpViewModel signUpViewModel) {
+    this.signUpViewModel = signUpViewModel;
   }
 
   @NonNull
@@ -32,16 +33,16 @@ public class SignUpAdapter extends RecyclerView.Adapter {
   public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
     BaseViewHolder holder = null;
     switch (viewType) {
-      case SIGN_UP_HELLO:
+      case SignUpListItem.SIGN_UP_HELLO:
         View textView = LayoutInflater.from(parent.getContext()).inflate(R.layout.sign_up_text_view, parent, false);
         holder = new TextViewHolder(textView);
         holder.draw();
         break;
-      case SIGN_UP_BENEFIT:
+//      case SignUpListItem.SIGN_UP_BENEFIT:
 //        dataObj = viewModel.getCurrentData();
 //        holder = new BenefitHolder(parent, dataObj);
 //        View SignUpBenefitView = LayoutInflater.from(parent.getContext()).inflate(R.layout.signup_benefit_view, parent, false);
-        break;
+//        break;
       default:
         break;
 
@@ -60,14 +61,8 @@ public class SignUpAdapter extends RecyclerView.Adapter {
 
   @Override
   public int getItemViewType(int position) {
-    switch (position) {
-      case 0:
-        return SIGN_UP_HELLO;
-      case 1:
-        return SIGN_UP_BENEFIT;
-      default:
-        break;
-    }
-    return 0;
+    SignUpListItem item = signUpViewModel.getLastItem();
+    int itemType = item.getItemType();
+    return itemType;
   }
 }

+ 4 - 3
app/src/main/java/kr/co/zumo/app/lifeplus/view/fragment/signup/SignUpFragment.java

@@ -43,12 +43,13 @@ public class SignUpFragment extends FragmentBase {
   @Override
   protected ViewModel defineViewModel() {
     ScreenChangerHelper helper = ScreenChangerHelper.getInstance();
-    viewModel = new SignUpViewModel(new FragmentChanger(helper.getFragmentActivity(), helper.getContainerId(), new BasicFragmentFactory()));
+    SignUpViewModel signUpViewModel = new SignUpViewModel(new FragmentChanger(helper.getFragmentActivity(), helper.getContainerId(), new BasicFragmentFactory()));
     FragmentSignUpBinding binding = DataBindingUtil.getBinding(getView());
-    SignUpAdapter adapter = new SignUpAdapter();
-    binding.setModel(viewModel);
+    SignUpAdapter adapter = new SignUpAdapter(signUpViewModel);
+    binding.setModel(signUpViewModel);
     binding.signUpList.setAdapter(adapter);
 
+    viewModel = signUpViewModel;
     return viewModel;
   }
 

+ 7 - 3
app/src/main/java/kr/co/zumo/app/lifeplus/view/fragment/signup/TextViewHolder.java

@@ -9,12 +9,13 @@ import android.widget.TextView;
 
 import kr.co.zumo.app.R;
 import kr.co.zumo.app.lifeplus.model.SuperModel;
+import kr.co.zumo.app.lifeplus.view.model.SignUpListItem;
 import kr.co.zumo.app.lifeplus.view.model.SignUpViewModel;
+import kr.co.zumo.app.lifeplus.view.model.bean.SignUpTextBean;
 
 /**
- * [클래스 개요]
+ * TextViewHolder - 회원 가입 화면에 텍스트 표시를 하는 클래스
  * <pre>
- * [클래스 상세 설명 또는 사용 시 주의점]
  * </pre>
  *
  * @author 하세미
@@ -33,7 +34,10 @@ public class TextViewHolder extends BaseViewHolder {
   void draw() {
     SignUpViewModel model = (SignUpViewModel) SuperModel.getInstance().getViewModel();
 
-    ((TextView) itemView.findViewById(R.id.sign_up_text_view)).setText(model.getStringId());
+    SignUpListItem item = model.getLastItem();
+    SignUpTextBean bean = item.getBean(SignUpTextBean.class);
+    String text = bean.getText();
 
+    ((TextView) itemView.findViewById(R.id.sign_up_text_view)).setText(text);
   }
 }

+ 0 - 26
app/src/main/java/kr/co/zumo/app/lifeplus/view/model/SignUpItem.java

@@ -1,26 +0,0 @@
-/*
- * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
- */
-package kr.co.zumo.app.lifeplus.view.model;
-
-import android.support.annotation.StringRes;
-
-import kr.co.zumo.app.R;
-
-/**
- * [클래스 개요]
- * <pre>
- * [클래스 상세 설명 또는 사용 시 주의점]
- * </pre>
- *
- * @author 민효동
- * @version 1.0
- * @history 민효동   [2018. 9. 11.]   [최초 작성]
- * @since 2018. 9. 11.
- */
-public class SignUpItem {
-  @StringRes
-  public int getStringId() {
-    return R.string.sign_up_hello_text;
-  }
-}

+ 64 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/view/model/SignUpListItem.java

@@ -0,0 +1,64 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.view.model;
+
+import com.google.gson.Gson;
+
+/**
+ * 회원 가입의 대화형 화면에 사용되는 각 아이템의 데이터 객체
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 9. 11.]   [최초 작성]
+ * @since 2018. 9. 11.
+ */
+public class SignUpListItem {
+
+  public static final int SIGN_UP_HELLO = 0;
+  public static final int SIGN_UP_BENEFIT = 1;
+
+  protected String jsonString;
+  protected int itemType;
+
+  /**
+   *
+   *
+   * @param itemType 텍스트, 버튼 등 아이템의 타입 지정
+   * @param jsonString 타입에 사용되는 json 타입의 데이터
+   */
+  public SignUpListItem(int itemType, String jsonString) {
+    this.jsonString = jsonString;
+    this.itemType = itemType;
+  }
+
+  /**
+   * 아이템의 데이터로 사용되는 string
+   *
+   * @return json 스트링
+   */
+  public String getJsonString() {
+    return jsonString;
+  }
+
+  /**
+   * 아이템 타입
+   *
+   * @return 아이템 타입, SIGN_UP_HELLO ...
+   */
+  public int getItemType() {
+    return itemType;
+  }
+
+  /**
+   * jsonString 를 Bean Object 로 변환하여 반환
+   *
+   * @param beanClass 변환할 Bean 의 클래스
+   * @return 변환된 클래스
+   */
+  public <T> T getBean(Class<T> beanClass) {
+    return new Gson().fromJson(jsonString, beanClass);
+  }
+}

+ 31 - 7
app/src/main/java/kr/co/zumo/app/lifeplus/view/model/SignUpViewModel.java

@@ -3,11 +3,12 @@
  */
 package kr.co.zumo.app.lifeplus.view.model;
 
+import android.databinding.ObservableArrayList;
 import android.databinding.ObservableList;
 import android.support.annotation.Nullable;
-import android.support.annotation.StringRes;
 
 import kr.co.zumo.app.R;
+import kr.co.zumo.app.lifeplus.application.App;
 import kr.co.zumo.app.lifeplus.supervisor.ScreenChanger;
 import kr.co.zumo.app.lifeplus.supervisor.ScreenID;
 
@@ -23,10 +24,15 @@ import kr.co.zumo.app.lifeplus.supervisor.ScreenID;
  */
 public class SignUpViewModel extends ViewModel {
 
-  ObservableList<SignUpItem> signUpList;
+  ObservableList<SignUpListItem> signUpList;
 
   public SignUpViewModel(ScreenChanger screenChanger) {
     super(screenChanger);
+
+    signUpList = new ObservableArrayList<>();
+
+    // fixme for test
+    signUpList.add(new SignUpListItem(SignUpListItem.SIGN_UP_HELLO, App.getInstance().getString(R.string.sign_up_hello_text)));
   }
 
   @Override
@@ -56,13 +62,31 @@ public class SignUpViewModel extends ViewModel {
     exitTo(ScreenID.LOGIN);
   }
 
-  public ObservableList<SignUpItem> getSignUpList() {
+  /**
+   * 화면 구성을 이루는 데이터 객체 배열 반환
+   * - recyclerView 의 데이터로 사용
+   *
+   * @return ObservableList<SignUpListItem>
+   */
+  public ObservableList<SignUpListItem> getSignUpList() {
     return signUpList;
   }
 
-  // fixme for test
-  @StringRes
-  public int getStringId() {
-    return R.string.sign_up_hello_text;
+  /**
+   * 데이터를 추가하여 view 표시
+   *
+   * @param item SignUpListItem
+   */
+  public void addItem(SignUpListItem item) {
+    signUpList.add(item);
+  }
+
+  /**
+   * 표시할 마지막 데이터를 가져옴
+   *
+   * @return signUpList 의 마지막 데이터
+   */
+  public SignUpListItem getLastItem() {
+    return signUpList.get(signUpList.size() - 1);
   }
 }

+ 27 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/view/model/bean/JsonBean.java

@@ -0,0 +1,27 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.view.model.bean;
+
+import com.google.gson.Gson;
+
+/**
+ * Bean model base class
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 9. 11.]   [최초 작성]
+ * @since 2018. 9. 11.
+ */
+public class JsonBean {
+  /**
+   * 현재 객체를 json string 으로 변환하여 반환
+   *
+   * @return json string
+   */
+  public String toJson() {
+    return new Gson().toJson(this);
+  }
+}

+ 30 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/view/model/bean/SignUpTextBean.java

@@ -0,0 +1,30 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.view.model.bean;
+
+import com.google.gson.annotations.SerializedName;
+
+/**
+ * SignUpTextBean
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 9. 11.]   [최초 작성]
+ * @since 2018. 9. 11.
+ */
+public class SignUpTextBean extends JsonBean {
+
+  @SerializedName("text")
+  private String text;
+
+  public String getText() {
+    return text;
+  }
+
+  public void setText(String text) {
+    this.text = text;
+  }
+}