詹子聪 před 5 roky
rodič
revize
cc7480a39c

+ 16 - 5
app/src/main/AndroidManifest.xml

@@ -30,27 +30,38 @@
         <!--添加优惠券-->
         <activity android:name=".ui.home.more.add.AddCouponActivity"
             android:configChanges="orientation|screenSize|keyboardHidden"
-            android:screenOrientation="portrait"/>
+            android:screenOrientation="portrait"
+            android:theme="@style/TextInputStyle"/>
 
         <!--忘记密码-->
         <activity android:name=".ui.home.more.forget.ForgetActivity"
             android:configChanges="orientation|screenSize|keyboardHidden"
-            android:screenOrientation="portrait"/>
+            android:screenOrientation="portrait"
+            android:theme="@style/TextInputStyle"/>
 
         <!--登录-->
         <activity android:name=".ui.home.more.login.LoginActivity"
             android:configChanges="orientation|screenSize|keyboardHidden"
-            android:screenOrientation="portrait"/>
+            android:screenOrientation="portrait"
+            android:theme="@style/TextInputStyle"/>
 
         <!--完善资料-->
         <activity android:name=".ui.home.more.register.fill.FillDataActivity"
             android:configChanges="orientation|screenSize|keyboardHidden"
-            android:screenOrientation="portrait"/>
+            android:screenOrientation="portrait"
+            android:theme="@style/TextInputStyle"/>
 
         <!--注册-->
         <activity android:name=".ui.home.more.register.RegisterActivity"
             android:configChanges="orientation|screenSize|keyboardHidden"
-            android:screenOrientation="portrait"/>
+            android:screenOrientation="portrait"
+            android:theme="@style/TextInputStyle"/>
+
+        <!--搜索-->
+        <activity android:name=".ui.home.search.SearchActivity"
+            android:configChanges="orientation|screenSize|keyboardHidden"
+            android:screenOrientation="portrait"
+            android:theme="@style/TextInputStyle"/>
     </application>
 
 </manifest>

+ 6 - 11
app/src/main/java/com/miekir/ym/base/ApiService.java

@@ -55,21 +55,16 @@ public interface ApiService {
     @GET("shibei/api/getCouponList")
     Observable<BaseResponse<List<CouponBean>>> getCouponList(@Query("pageNum") int pageNum, @Query("pageSize") int pageSize);
 
-    
+    // todo
+    /**根据关键字分页查询优惠券*/
+    @GET("shibei/api/getCouponListByKeyword")
+    Observable<BaseResponse<List<CouponBean>>> getCouponListByKeyword(@Query("keywords") String keyword, @Query("pageNum") int pageNum, @Query("pageSize") int pageSize);
+
+
 
 //    /*----------------------------------------升级信息----------------------------------------*/
 //    /**获取版本更新信息*/
 //    @GET("shibei/api/getUpgradeInfo")
 //    Observable<BaseResponse<UpgradeBean>> getUpgradeInfo();
-//
-//    /*----------------------------------------系统配置信息----------------------------------------*/
-//    /**获取系统配置信息*/
-//    @GET("shibei/api/getSystemConfig")
-//    Observable<BaseResponse<SystemBean>> getSystemConfig();
-
-    /**设置是否VIP才能用API*/
-    @POST("shibei/api/setApiConfig")
-    Observable<BaseResponse<String>> setApiConfig(@Query("isApiVipOnly") boolean isApiVipOnly);
-
 
 }

+ 13 - 0
app/src/main/java/com/miekir/ym/base/YangActivity.kt

@@ -2,7 +2,9 @@ package com.miekir.ym.base
 
 import android.os.Bundle
 import android.view.View
+import androidx.appcompat.widget.Toolbar
 import com.miekir.mvp.view.BaseMvpActivity
+import com.miekir.ym.R
 
 /**
  * Copyright (C), 2019-2020, Miekir
@@ -16,4 +18,15 @@ abstract class YangActivity : BaseMvpActivity() {
         window.decorView.systemUiVisibility =
             View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
     }
+
+    protected open fun setTitle(title: String?) {
+        //iv_done = findViewById(R.id.iv_done);
+        val toolbar: Toolbar = findViewById(R.id.toolbar) ?: return
+        toolbar.title = title
+        //toolbar.setTitleTextColor(getResources().getColor(R.color.black_theme));
+        setSupportActionBar(toolbar)
+        //关键下面两句话,设置了回退按钮,及点击事件的效果
+        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
+        toolbar.setNavigationOnClickListener { finish() }
+    }
 }

+ 9 - 6
app/src/main/java/com/miekir/ym/ui/home/MainActivity.kt

@@ -16,6 +16,7 @@ import com.miekir.ym.ui.home.coupon.CouponBean
 import com.miekir.ym.ui.home.coupon.CouponFragment
 import com.miekir.ym.ui.home.more.MineActivity
 import com.miekir.ym.ui.home.more.login.LoginActivity
+import com.miekir.ym.ui.home.search.SearchActivity
 import kotlinx.android.synthetic.main.activity_main.*
 import java.util.*
 
@@ -25,12 +26,14 @@ class MainActivity : YangActivity(), View.OnClickListener {
     }
 
     override fun initViews(savedInstanceState: Bundle?) {
-        val titles = arrayOf("数码", "美食", "工具", "关于")
+        val titles = arrayOf("全部", "美食", "数码", "服饰", "生活", "其他")
         val fragments: ArrayList<Fragment> = ArrayList()
         fragments.add(CouponFragment(CouponBean.TYPE_ALL))
-        fragments.add(CouponFragment(CouponBean.TYPE_ALL))
-        fragments.add(CouponFragment(CouponBean.TYPE_ALL))
-        fragments.add(CouponFragment(CouponBean.TYPE_ALL))
+        fragments.add(CouponFragment(CouponBean.TYPE_FOOD))
+        fragments.add(CouponFragment(CouponBean.TYPE_DIGITAL))
+        fragments.add(CouponFragment(CouponBean.TYPE_CLOTHES))
+        fragments.add(CouponFragment(CouponBean.TYPE_LIVE))
+        fragments.add(CouponFragment(CouponBean.TYPE_OTHER))
 
         val adapter = TabFragmentAdapter(supportFragmentManager, fragments)
         vp_main.adapter = adapter
@@ -78,8 +81,8 @@ class MainActivity : YangActivity(), View.OnClickListener {
             }
 
             R.id.fl_search_top, R.id.fl_search -> {
-                // todo 点击搜索
-//                startActivity(Intent(this, SearchActivity::class.java))
+                // 点击搜索
+                startActivity(Intent(this, SearchActivity::class.java))
             }
 
             else -> {

+ 16 - 3
app/src/main/java/com/miekir/ym/ui/home/coupon/CouponBean.java

@@ -17,13 +17,25 @@ public class CouponBean implements Serializable {
      */
     public static final int TYPE_ALL = 0;
     /**
-     * 生活
+     * 美食
      */
-    public static final int TYPE_LIVE = 1;
+    public static final int TYPE_FOOD = 1;
     /**
-     * 数
+     * 数
      */
     public static final int TYPE_DIGITAL = 2;
+    /**
+     * 衣服
+     */
+    public static final int TYPE_CLOTHES = 3;
+    /**
+     * 生活
+     */
+    public static final int TYPE_LIVE = 4;
+    /**
+     * 其他
+     */
+    public static final int TYPE_OTHER = 5;
 
     /**
      * 模板Id
@@ -61,6 +73,7 @@ public class CouponBean implements Serializable {
      * 当前是否被收藏
      */
     public boolean isFavorite;
+    public int couponTypeId = TYPE_ALL;
 
     public void updateData(CouponBean couponBean) {
         this.couponType = couponBean.couponType;

+ 2 - 2
app/src/main/java/com/miekir/ym/ui/home/coupon/CouponFragment.java

@@ -31,7 +31,7 @@ public class CouponFragment extends BaseMvpFragment implements ICouponView<Coupo
     private static final int PAGE_START = 0;
     private static final int PAGE_SIZE = 20;
 
-    private int mTemplateType = CouponBean.TYPE_ALL;
+    private int mCouponType = CouponBean.TYPE_ALL;
 
     @InjectPresenter
     CouponPresenter mPresenter;
@@ -47,7 +47,7 @@ public class CouponFragment extends BaseMvpFragment implements ICouponView<Coupo
     private boolean mIsRefresh = true;
 
     public CouponFragment(int templateType) {
-        mTemplateType = templateType;
+        mCouponType = templateType;
     }
 
     @Override

+ 16 - 0
app/src/main/java/com/miekir/ym/ui/home/search/ISearchView.java

@@ -0,0 +1,16 @@
+package com.miekir.ym.ui.home.search;
+
+import com.miekir.mvp.view.IView;
+import com.miekir.ym.ui.home.coupon.CouponBean;
+
+import java.util.List;
+
+/**
+ *
+ *
+ * @author Miekir
+ * @date 2020/7/8 19:44
+ */
+public interface ISearchView extends IView {
+    void onSearchKeywordResult(boolean success, String message, List<CouponBean> goodsList);
+}

+ 246 - 0
app/src/main/java/com/miekir/ym/ui/home/search/SearchActivity.java

@@ -0,0 +1,246 @@
+package com.miekir.ym.ui.home.search;
+
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.text.TextUtils;
+import android.view.View;
+import android.widget.Button;
+import android.widget.EditText;
+
+import androidx.appcompat.app.AlertDialog;
+import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
+
+import com.miekir.common.utils.ToastTool;
+import com.miekir.common.utils.ViewTool;
+import com.miekir.mvp.presenter.InjectPresenter;
+import com.miekir.ym.R;
+import com.miekir.ym.base.YangActivity;
+import com.miekir.ym.listener.OnRcvScrollListener;
+import com.miekir.ym.ui.home.coupon.CouponAdapter;
+import com.miekir.ym.ui.home.coupon.CouponBean;
+import com.miekir.ym.ui.home.coupon.CouponPresenter;
+import com.miekir.ym.ui.home.coupon.ICouponView;
+import com.miekir.ym.ui.home.more.add.AddCouponActivity;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import rx_activity_result2.RxActivityResult;
+
+/**
+ * Copyright (C), 2019-2020, Miekir
+ *
+ * @author Miekir
+ * @date 2020/8/12 19:40
+ * Description: 输入文字搜索
+ */
+public class SearchActivity extends YangActivity implements View.OnClickListener,
+        ISearchView, View.OnFocusChangeListener, ICouponView<CouponBean> {
+    private static final int PAGE_START = 0;
+    private static final int PAGE_SIZE = 20;
+    private int mCurrentPage = PAGE_START;
+
+    private EditText et_search;
+    private Button btn_search;
+
+    private RecyclerView rv_search_result;
+    private List<CouponBean> mCouponList = new ArrayList<>();
+    private CouponAdapter mAdapter;
+    private boolean mIsRefresh = true;
+
+    private String mKeyword;
+
+    @InjectPresenter
+    SearchPresenter searchPresenter;
+
+    @InjectPresenter
+    CouponPresenter couponPresenter;
+
+    @Override
+    public int getLayoutId() {
+        return R.layout.activity_search;
+    }
+
+    @Override
+    public void initViews(Bundle savedInstanceState) {
+        setTitle("搜索");
+        et_search = findViewById(R.id.et_search);
+        et_search.setOnFocusChangeListener(this);
+        btn_search = findViewById(R.id.btn_search);
+
+        rv_search_result = findViewById(R.id.rv_search_result);
+        // 必须要设置LayoutManager,否则RecyclerView不知道要使用什么布局,从而在界面上不显示
+        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
+        rv_search_result.setLayoutManager(layoutManager);
+        int dividerWidth = (int) getResources().getDimension(R.dimen.margin_s);
+        mAdapter = new CouponAdapter(this, mCouponList);
+        mAdapter.setCouponLongClickListener(this::showAdminDialog);
+        rv_search_result.setAdapter(mAdapter);
+        mAdapter.setEmptyView(R.layout.view_empty, rv_search_result);
+        // 加载更多
+        rv_search_result.addOnScrollListener(new OnRcvScrollListener(){
+            @Override
+            public void onBottom() {
+                super.onBottom();
+                // 如果到底部了,而且不是正在加载状态,就变为正在加载状态,并及时去加载数据
+                showLoading();
+                mIsRefresh = false;
+                searchPresenter.searchByKeyword(mKeyword, mCurrentPage, PAGE_SIZE);
+            }
+        });
+
+        ViewTool.requestInputFocus(this, et_search);
+        ViewTool.setOnClickListener(this, this, new int[]{R.id.ll_search, R.id.btn_search, R.id.fl_clear});
+    }
+
+    @Override
+    public void onClick(View v) {
+
+        switch (v.getId()) {
+            case R.id.btn_search:
+                et_search.clearFocus();
+                mKeyword = et_search.getText().toString();
+                if (TextUtils.isEmpty(mKeyword)) {
+                    ToastTool.showShort("请输入关键字或链接");
+                    return;
+                }
+                showLoading();
+                mCurrentPage = PAGE_START;
+                mCouponList.clear();
+                mAdapter.notifyDataSetChanged();
+                mIsRefresh = true;
+                searchPresenter.searchByKeyword(mKeyword, mCurrentPage, PAGE_SIZE);
+                break;
+            case R.id.ll_search:
+                et_search.clearFocus();
+                break;
+            case R.id.fl_clear:
+                et_search.setText("");
+                ViewTool.requestInputFocus(this, et_search);
+                break;
+            default:
+                break;
+        }
+    }
+
+    /**
+     * 修改商品或者删除商品
+     */
+    private void showAdminDialog(int position) {
+        CouponBean couponBean = mCouponList.get(position);
+        AlertDialog alertDialog = new AlertDialog.Builder(this)
+                .setMessage("选择操作")
+                .setPositiveButton("修改", (dialog, which) -> {
+                    dialog.dismiss();
+                    Intent modifyIntent = new Intent(this, AddCouponActivity.class);
+                    modifyIntent.putExtra(AddCouponActivity.KEY_MODIFY_COUPON, couponBean);
+                    RxActivityResult.on(this).startIntent(modifyIntent)
+                            .filter(result -> result.resultCode() == RESULT_OK)
+                            .doOnNext(result -> {
+                                // 修改商品成功之后刷新item
+                                CouponBean modifiedBean = (CouponBean) result.data().getSerializableExtra(AddCouponActivity.KEY_MODIFY_COUPON);
+                                if (modifiedBean == null) {
+                                    return;
+                                }
+                                couponBean.updateData(modifiedBean);
+                                mAdapter.notifyItemChanged(position);
+                            })
+                            .subscribe();
+                })
+                .setNeutralButton("取消", (dialog, which) -> {
+                    dialog.dismiss();
+                })
+                .setNegativeButton("删除", (DialogInterface dialog, int which) -> {
+                    dialog.dismiss();
+                    showDeleteDialog(position);
+                }).create();
+        alertDialog.setCanceledOnTouchOutside(false);
+        alertDialog.setCancelable(false);
+        alertDialog.show();
+    }
+
+    /**
+     * 是否删除商品
+     */
+    private void showDeleteDialog(int position) {
+        CouponBean couponBean = mCouponList.get(position);
+        AlertDialog alertDialog = new AlertDialog.Builder(this)
+                .setMessage("确定删除优惠:" + couponBean.couponName)
+                .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
+                .setPositiveButton("确定", (DialogInterface dialog, int which) -> {
+                    dialog.dismiss();
+                    // 删除商品
+                    couponPresenter.deleteCouponById(position, couponBean.id);
+                }).create();
+        alertDialog.show();
+    }
+
+    @Override
+    public void onSearchKeywordResult(boolean success, String message, List<CouponBean> goodsList) {
+        hideLoading();
+
+        if (!success) {
+            ToastTool.showShort(message);
+            return;
+        }
+
+        if (mCurrentPage == PAGE_START) {
+            mCouponList.clear();
+            if (goodsList != null) {
+                mCouponList.addAll(goodsList);
+            }
+
+            // 本地写死的数据
+            mAdapter.notifyDataSetChanged();
+            // 如果是下拉刷新获取的第0页数据,item不要自动滚动一段距离
+            // 或者用这个mLayoutManager.scrollToPositionWithOffset(0, 0);
+            if (mIsRefresh) {
+                rv_search_result.smoothScrollToPosition(0);
+            }
+        } else {
+            if (goodsList != null) {
+                mCouponList.addAll(goodsList);
+            }
+
+            mAdapter.notifyDataSetChanged();
+        }
+
+        // 请求到数据了,页数自增
+        if (goodsList != null && goodsList.size() > 0) {
+            mCurrentPage++;
+        } else {
+            ToastTool.showShort("没有更多数据了");
+        }
+    }
+
+    @Override
+    public void onFocusChange(View v, boolean hasFocus) {
+        if (v.getId() == R.id.et_search) {
+            if (hasFocus) {
+                rv_search_result.setVisibility(View.GONE);
+                btn_search.setVisibility(View.VISIBLE);
+            } else {
+                rv_search_result.setVisibility(View.VISIBLE);
+                btn_search.setVisibility(View.GONE);
+            }
+        }
+    }
+
+    @Override
+    public void onCouponDataCome(boolean success, String message, List<CouponBean> couponBeanList) {
+
+    }
+
+    @Override
+    public void onDeleteCoupon(boolean success, String message, int position) {
+        // 删除结果
+        if (success) {
+            mAdapter.notifyItemRemoved(position);
+            ToastTool.showShort("删除成功");
+        } else {
+            ToastTool.showShort(message);
+        }
+    }
+}

+ 52 - 0
app/src/main/java/com/miekir/ym/ui/home/search/SearchPresenter.java

@@ -0,0 +1,52 @@
+package com.miekir.ym.ui.home.search;
+
+import android.text.TextUtils;
+
+import com.miekir.mvp.presenter.BasePresenter;
+import com.miekir.network.RetrofitHelper;
+import com.miekir.network.widget.observe.NetMvpObserver;
+import com.miekir.ym.base.ApiService;
+import com.miekir.ym.ui.home.coupon.CouponBean;
+
+import java.util.List;
+
+import io.reactivex.android.schedulers.AndroidSchedulers;
+import io.reactivex.schedulers.Schedulers;
+
+/**
+ * Copyright (C), 2019-2020, Miekir
+ *
+ * @author Miekir
+ * @date 2020/8/16 9:34
+ * Description: 搜索
+ */
+public class SearchPresenter extends BasePresenter<ISearchView> {
+
+    public void searchByKeyword(String keyword, int pageNum, int pageSize) {
+        NetMvpObserver<List<CouponBean>> observer = new NetMvpObserver<List<CouponBean>>() {
+            @Override
+            public void onSuccess(int code, List<CouponBean> result) {
+                post(view -> view.onSearchKeywordResult(true, "", result));
+            }
+
+            @Override
+            public void onFailure(int code, Throwable e, String errMsg) {
+                post(view -> view.onSearchKeywordResult(false, TextUtils.isEmpty(errMsg) ? "获取失败:"+e.getMessage() : errMsg, null));
+            }
+        };
+
+        mProgressDisposableList.add(observer);
+
+        RetrofitHelper.getInstance()
+                .getRequestApi(ApiService.class)
+                .getCouponListByKeyword(keyword, pageNum, pageSize)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe(observer);
+    }
+
+    @Override
+    public void onInit() {
+
+    }
+}

+ 80 - 0
app/src/main/res/layout/activity_search.xml

@@ -0,0 +1,80 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--搜索栏-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical"
+    android:id="@+id/ll_search"
+    android:focusable="true"
+    android:focusableInTouchMode="true"
+    android:fitsSystemWindows="true"
+    android:background="@color/colorPrimary">
+
+    <include layout="@layout/view_toolbar"/>
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal"
+        android:gravity="center_vertical"
+        android:background="@color/white">
+        <EditText
+            android:id="@+id/et_search"
+            android:layout_width="0dp"
+            android:layout_weight="1"
+            android:layout_height="wrap_content"
+            android:minHeight="@dimen/height_tab_bar"
+            android:background="@color/white"
+            android:singleLine="true"
+            android:paddingStart="@dimen/margin_default"
+            android:paddingEnd="@dimen/margin_default"
+            android:paddingTop="@dimen/margin_ss"
+            android:paddingBottom="@dimen/margin_ss"
+            android:textSize="@dimen/text_normal_p"
+            android:hint="请输入关键字或链接"
+            android:textColor="@color/black_text_comfortable"
+            android:textColorHint="@color/gray_text_hint"
+            android:maxLength="512" />
+        <FrameLayout
+            android:id="@+id/fl_clear"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:paddingStart="@dimen/margin_default"
+            android:paddingEnd="@dimen/margin_default">
+            <ImageView
+                android:layout_width="16dp"
+                android:layout_height="16dp"
+                android:scaleType="fitXY"
+                android:src="@mipmap/search_clear" />
+        </FrameLayout>
+    </LinearLayout>
+
+    <include layout="@layout/view_divider"/>
+
+    <FrameLayout
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+        <android.widget.Button
+            android:id="@+id/btn_search"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:minHeight="0dp"
+            android:layout_marginTop="@dimen/margin_default"
+            android:layout_marginStart="@dimen/margin_default"
+            android:layout_marginEnd="@dimen/margin_default"
+            android:paddingTop="@dimen/padding_full_width"
+            android:paddingBottom="@dimen/padding_full_width"
+            android:text="立即搜索"
+            android:textColor="@color/white"
+            android:textStyle="bold"
+            android:background="@drawable/selector_btn"
+            style="?android:attr/borderlessButtonStyle"/>
+
+        <androidx.recyclerview.widget.RecyclerView
+            android:id="@+id/rv_search_result"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:visibility="gone"/>
+    </FrameLayout>
+
+</LinearLayout>

binární
app/src/main/res/mipmap-xxhdpi/search_clear.png