Quellcode durchsuchen

[검색][New] 태그 입력 수 제한 - 5개

hyodong.min vor 7 Jahren
Ursprung
Commit
551341c60f

+ 3 - 3
app/src/main/java/kr/co/zumo/app/lifeplus/bean/api/SearchRequestBean.java

@@ -18,10 +18,10 @@ import com.google.gson.annotations.SerializedName;
 public class SearchRequestBean extends KeywordRequestBean {
 
   /*
-    "keywordTxt": "#음악",
+    "keywordTxt": "#음악#서울#남산",
     "userNo": "201811270001008196",
 
-    "cateNo": "",
+    "ctgrNo": "",
     "fltrTxt":"#1#2#2#2", -- 일반필터조건 ,필터코드(#filterNo)
     "fltrAreaTxt":"#서울", -- 필터 지역 조건 ,지역명(#tagName)
     "fltrHotPlaceTxt":"#누각", -- 필터 핫플레이스 조건 ,핫플레이명(#tagName)
@@ -32,7 +32,7 @@ public class SearchRequestBean extends KeywordRequestBean {
   public static final String ORDER_BY_LIKE = "2";
   public static final String ORDER_BY_BOOKMARK = "3";
 
-  @SerializedName("cateNo")
+  @SerializedName("ctgrNo")
   private String categoryNumber;
   @SerializedName("fltrTxt")    // "fltrTxt":"#1#2#2#2", -- 일반필터조건 ,필터코드(#filterNo)
   private String filter;

+ 1 - 1
app/src/main/java/kr/co/zumo/app/lifeplus/view/CharFinder.java

@@ -41,7 +41,7 @@ public class CharFinder {
       // 길이 차이가 2이상일 경우 정상 입력이 아님
       setPrevStr(currentStr);
       str[0] = Character.MIN_VALUE;
-      index[0] = -1;
+      index[0] = prevStr.length() == currentStr.length() ? -1 : -2;
       return false;
     }
 

+ 1 - 1
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/search/SearchFragment.java

@@ -64,7 +64,7 @@ public class SearchFragment extends FragmentBase<SearchPresenter> implements ISe
 
     @Override
     public void afterTextChanged(Editable s) {
-      presenter.afterTextChanged(s);
+      presenter.onSearchAfterTextChanged(s);
     }
   };
 

+ 35 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/search/SearchModel.java

@@ -8,6 +8,7 @@ import android.util.Log;
 import com.google.gson.Gson;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import io.reactivex.disposables.Disposable;
@@ -45,6 +46,7 @@ public class SearchModel extends Model {
   protected List<TagBean> latestTagBeans;
   protected List<TagKeywordBean> autoCompletionTagBeans;
   private String tag;
+  private int tagCount = 0;
 
   private boolean isShowingAutoCompletion = false;
 
@@ -175,12 +177,44 @@ public class SearchModel extends Model {
   public void setTag(String s) {
     tag = toTag(s);
     Log.w("APP# SearchModel | setTag", "|" + "tag ==> " + tag);
+    tagCount = StringUtil.count(tag, StringUtil.HASH);
+
+    // fixme for log
+    String[] tags = tag.split(StringUtil.HASH);
+    Log.w("APP# SearchModel | setTag", "|" + "tag count ==> " + tagCount + ", tags: " + Arrays.toString(tags));
   }
 
   public String getTag() {
     return tag;
   }
 
+  public int getTagCount() {
+    return tagCount;
+  }
+
+  public int getTagMaxCount() {
+    return 5;
+  }
+
+  public String getRestrictedTag() {
+    if (StringUtil.isEmpty(tag)) {
+      return "";
+    }
+    String[] tags = tag.replace(StringUtil.HASH, "").split(" ");
+    int len = tags.length;
+    if (len > getTagMaxCount()) {
+      len = getTagMaxCount();
+    }
+    StringBuilder stringBuilder = new StringBuilder();
+    for (int i = 0; i < len; ++i) {
+      stringBuilder.append(StringUtil.HASH).append(tags[i]);
+      if (i < len - 1) {
+        stringBuilder.append(" ");
+      }
+    }
+    return stringBuilder.toString();
+  }
+
   private void stopAutoCompletion() {
     if (null != disposableAutoCompletion) {
       disposableAutoCompletion.dispose();
@@ -249,4 +283,5 @@ public class SearchModel extends Model {
   public void setShowingAutoCompletion(boolean showing) {
     isShowingAutoCompletion = showing;
   }
+
 }

+ 42 - 22
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/search/SearchPresenter.java

@@ -154,13 +154,8 @@ public class SearchPresenter extends Presenter<SearchModel, ISearchView> {
         }
         if (StringUtil.isFull(tag)) {
           // 키워드를 텍스트필드에 넣을 때 이벤트 발생을 막기위해 리스너 제거/재등록
-          view.removeTextWatcher();
           view.setBoldGuideHash(false);
-          // finder 선택 태그로 설정
-          String stripTag = stripTag(tag);
-          finder = new CharFinder(stripTag);
-          view.setKeyword(stripTag);
-          view.addTextWatcher();
+          replaceTag(tag);
 
           search(tag);
         }
@@ -224,7 +219,6 @@ public class SearchPresenter extends Presenter<SearchModel, ISearchView> {
   private void search(String tag) {
     if (StringUtil.isFull(tag)) {
       model.saveLatestTag(tag);
-      model.setTag(tag);
 
       model.setDeliveryPackaging(model.getTag());
       go(ScreenID.SEARCH_RESULT);
@@ -253,9 +247,27 @@ public class SearchPresenter extends Presenter<SearchModel, ISearchView> {
     }
   }
 
+  private void replaceTag(String tag) {
+    view.removeTextWatcher();
+    model.setTag(tag);
+    String stripTag = stripTag(tag);
+    finder.setPrevStr(stripTag);
+    view.setKeyword(stripTag);
+    view.addTextWatcher();
+  }
+
+
+  public void onSearchAfterTextChanged(Editable s) {
+    Log.w("APP# SearchPresenter | onSearchAfterTextChanged", "|" + "s: " + s.toString());
+    Log.w("APP# SearchPresenter | onSearchAfterTextChanged", "|" + " tag count: " + model.getTagCount());
+
+    if (model.getTagCount() > model.getTagMaxCount()) {
+      // 태그는 5개까지만 허용
+      String tag = model.getRestrictedTag();
+      replaceTag(tag);
+      return;
+    }
 
-  public void afterTextChanged(Editable s) {
-    Log.w("APP# SearchPresenter | afterTextChanged", "|" + "s: " + s.toString());
     char[] str = new char[1];
     int[] index = new int[1];
     int prevLen = finder.getLength();
@@ -264,14 +276,14 @@ public class SearchPresenter extends Presenter<SearchModel, ISearchView> {
 
     int afterLen = finder.getLength();
 
-    Log.i("APP# SearchPresenter | afterTextChanged", "|" + "prevlen: " + prevLen + ", afterlen: " + afterLen);
-    Log.i("APP# SearchPresenter | afterTextChanged", "|" + "str[0]: " + str[0] + ", index[0]: " + index[0] + ", bs: " + isBackspace);
+    Log.i("APP# SearchPresenter | onSearchAfterTextChanged", "|" + "prevlen: " + prevLen + ", afterlen: " + afterLen);
+    Log.i("APP# SearchPresenter | onSearchAfterTextChanged", "|" + "str[0]: " + str[0] + ", index[0]: " + index[0] + ", bs: " + isBackspace);
 
     if (index[0] > -1) {
       if (isBackspace) {
         // backspace
         // # 이 삭제되면
-        if (hash.equals(String.valueOf(str[0]))) {
+        if (str[0] == hash.charAt(0)) {
           // 이전 char 가 ' ' 이라면 삭제 해줌.
           int idx = index[0] - 1;
           if (idx > -1) {
@@ -286,15 +298,17 @@ public class SearchPresenter extends Presenter<SearchModel, ISearchView> {
           // 스페이스 입력
           if (prevLen == 0) {
             // 첫 칸에는 스페이스 제거
-            s.delete(0, 1);
+//            s.delete(0, 1);
+            view.setBoldGuideHash(true);
+            replaceTag("");
           }
           else {
-            replaceHash(s);
+            convertToHashTag(s);
           }
         }
         else if (str[0] == hash.charAt(0)) {
           // # 직접 입력
-          replaceHash(s);
+          convertToHashTag(s);
         }
       }
     }
@@ -302,15 +316,21 @@ public class SearchPresenter extends Presenter<SearchModel, ISearchView> {
 
   String hash = StringUtil.HASH;
 
-  private Editable replaceHash(Editable s) {
+  private Editable convertToHashTag(Editable s) {
     Editable ab = removeHash(s);
 
-    if (ab.toString().contains(hash) == false && ab.toString().length() > 0) {
-      ab = new SpannableStringBuilder(ab.toString().replace("  ", " ").replace(" ", " " + hash));
-      /**
-       * s.replace 를 실행하면 afterTextChanged() 가 다시 호출되므로 주의!!
-       */
-      s.replace(0, s.length(), ab);
+    if (ab.toString().contains(hash) == false) {
+      if (ab.toString().length() > 0) {
+        ab = new SpannableStringBuilder(ab.toString().replace("  ", " ").replace(" ", " " + hash));
+        /**
+         * s.replace 를 실행하면 onSearchAfterTextChanged() 가 다시 호출되므로 주의!!
+         */
+        s.replace(0, s.length(), ab);
+      }
+      else {
+        // 해시 제거하면 빈칸임.
+        s.clear();
+      }
     }
     return s;
   }