Browse Source

添加kotlin拓展函数,优化了一点点代码

hegj 5 years ago
parent
commit
88cf7ced6f

+ 0 - 1
app/src/main/AndroidManifest.xml

@@ -141,7 +141,6 @@
     <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 
 </manifest>

+ 5 - 0
app/src/main/java/me/hegj/wandroid/app/GlobalHttpHandlerImpl.kt

@@ -19,6 +19,7 @@ import android.content.Context
 import com.jess.arms.http.GlobalHttpHandler
 import me.hegj.wandroid.app.utils.CacheUtil
 import me.hegj.wandroid.app.utils.HttpUtils.encodeCookie
+import okhttp3.FormBody
 import okhttp3.Interceptor
 import okhttp3.Request
 import okhttp3.Response
@@ -67,11 +68,15 @@ class GlobalHttpHandlerImpl(val context: Context) : GlobalHttpHandler {
     override fun onHttpRequestBefore(chain: Interceptor.Chain, request: Request): Request {
         /* 如果需要在请求服务器之前做一些操作, 则重新构建一个做过操作的 Request 并 return, 如增加 Header、Params 等请求信息, 不做操作则直接返回参数 request*/
         if (CacheUtil.isLogin()) {
+//            chain.request().url().newBuilder().addEncodedPathSegment()
             val cookies = CacheUtil.getCookie()
             //如果已经登录过了,那么请求的时候可以带上cookie 参数
             cookies?.run {
                 return chain.request().newBuilder()
                         .addHeader("Cookie", this)
+                       /* .post(FormBody.Builder().apply {
+                            addEncoded("token","xxx")
+                        }.build())*/
                         .build()
             }
         }

+ 6 - 8
app/src/main/java/me/hegj/wandroid/app/utils/HttpUtils.kt

@@ -38,19 +38,17 @@ object HttpUtils {
     }
 
     private fun convertStatusCode(httpException: HttpException): String {
-        val msg: String
-        if (httpException.code() == 500) {
-            msg = "服务器发生错误"
+        return if (httpException.code() == 500) {
+            "服务器发生错误"
         } else if (httpException.code() == 404) {
-            msg = "请求地址不存在"
+            "请求地址不存在"
         } else if (httpException.code() == 403) {
-            msg = "请求被服务器拒绝"
+            "请求被服务器拒绝"
         } else if (httpException.code() == 307) {
-            msg = "请求被重定向到其他页面"
+            "请求被重定向到其他页面"
         } else {
-            msg = httpException.message()
+            httpException.message()
         }
-        return msg
     }
 
     /**

+ 44 - 27
app/src/main/java/me/hegj/wandroid/app/utils/KotlinUtil.kt

@@ -4,6 +4,9 @@ import android.app.Activity
 import android.content.Context
 import android.content.Intent
 import android.os.Bundle
+import android.text.Editable
+import android.text.TextWatcher
+import android.widget.EditText
 import android.widget.FrameLayout
 import android.widget.LinearLayout
 import android.widget.TextView
@@ -16,10 +19,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
 import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
 import com.kingja.loadsir.core.LoadService
 import me.hegj.wandroid.app.weight.DefineLoadMoreView
-import me.hegj.wandroid.mvp.ui.activity.main.me.MeFragment
-import me.hegj.wandroid.mvp.ui.activity.setting.SettingActivity
 import me.hegj.wandroid.mvp.ui.activity.start.LoginActivity
-import me.yokeyword.fragmentation.SupportFragment
 
 /**
  * 根据控件的类型设置主题,注意,控件具有优先级, 基本类型的控件建议放到最后,像 Textview,FragmentLayout,不然会出现问题,
@@ -40,7 +40,7 @@ fun setUiTheme(context: Context, anylist: List<Any>) {
                     it.closeLoadAnimation()
                 }
             }
-            is BottomNavigationViewEx ->{
+            is BottomNavigationViewEx -> {
                 it.itemIconTintList = SettingUtil.getColorStateList(context)
                 it.itemTextColor = SettingUtil.getColorStateList(context)
             }
@@ -52,34 +52,51 @@ fun setUiTheme(context: Context, anylist: List<Any>) {
         }
     }
 }
-    fun Fragment.startActivityKx(cls :Class<*>,isNeedLogin:Boolean = false,bundle: Bundle = Bundle()){
-        if(isNeedLogin){
-            if (!CacheUtil.isLogin()) {
-                startActivity(Intent(this.activity,LoginActivity::class.java))
-            }else{
-                startActivity(Intent(this.activity,cls).apply {
-                    putExtras(bundle)
-                })
-            }
-        }else{
-            startActivity(Intent(this.activity,cls).apply {
+
+fun Fragment.startActivityKx(cls: Class<*>, isNeedLogin: Boolean = false, bundle: Bundle = Bundle()) {
+    if (isNeedLogin) {
+        if (!CacheUtil.isLogin()) {
+            startActivity(Intent(this.activity, LoginActivity::class.java))
+        } else {
+            startActivity(Intent(this.activity, cls).apply {
                 putExtras(bundle)
             })
         }
+    } else {
+        startActivity(Intent(this.activity, cls).apply {
+            putExtras(bundle)
+        })
     }
-    fun Activity.startActivityKx(cls :Class<*>, isNeedLogin:Boolean = false,bundle: Bundle = Bundle()){
-        if(isNeedLogin){
-            if (!CacheUtil.isLogin()) {
-                startActivity(Intent(this,LoginActivity::class.java))
-            }else{
-                startActivity(Intent(this,cls).apply {
-                    putExtras(bundle)
-                })
-            }
-        }else{
-            startActivity(Intent(this,cls).apply {
+}
+
+fun Activity.startActivityKx(cls: Class<*>, isNeedLogin: Boolean = false, bundle: Bundle = Bundle()) {
+    if (isNeedLogin) {
+        if (!CacheUtil.isLogin()) {
+            startActivity(Intent(this, LoginActivity::class.java))
+        } else {
+            startActivity(Intent(this, cls).apply {
                 putExtras(bundle)
             })
         }
-
+    } else {
+        startActivity(Intent(this, cls).apply {
+            putExtras(bundle)
+        })
     }
+}
+
+fun EditText.afterTextChange(afterTextChanged: (String) -> Unit) {
+    this.addTextChangedListener(object : TextWatcher {
+        override fun afterTextChanged(s: Editable?) {
+            afterTextChanged.invoke(s.toString())
+        }
+
+        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
+
+        }
+
+        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
+
+        }
+    })
+}

+ 0 - 1
app/src/main/java/me/hegj/wandroid/app/utils/ShowUtils.kt

@@ -73,7 +73,6 @@ object ShowUtils {
             val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
             inputMethodManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
         }
-
     }
 
 

+ 2 - 2
app/src/main/java/me/hegj/wandroid/mvp/model/api/Api.kt

@@ -17,6 +17,7 @@ package me.hegj.wandroid.mvp.model.api
 
 import io.reactivex.Observable
 import me.hegj.wandroid.mvp.model.entity.*
+import okhttp3.ResponseBody
 import retrofit2.http.*
 
 /**
@@ -44,7 +45,6 @@ interface Api {
     @POST("/user/register")
     fun register(@Field("username") username: String, @Field("password") pwd: String, @Field("repassword") rpwd: String): Observable<ApiResponse<Any>>
 
-
     /**
      * 获取banner数据
      */
@@ -228,7 +228,7 @@ interface Api {
     fun doneTodo(@Path("id") id: Int, @Field("status") status: Int): Observable<ApiResponse<Any>>
 
     /**
-     * 获取Todo列表数据 根据完成时间排序
+     * 广场列表数据
      */
     @GET("/user_article/list/{page}/json")
     fun getSquareData(@Path("page") page: Int): Observable<ApiResponse<ApiPagerResponse<MutableList<AriticleResponse>>>>

+ 1 - 2
app/src/main/java/me/hegj/wandroid/mvp/ui/activity/main/home/HomeFragment.kt

@@ -3,6 +3,7 @@ package me.hegj.wandroid.mvp.ui.activity.main.home
 import android.annotation.SuppressLint
 import android.content.Intent
 import android.os.Bundle
+import android.util.Log
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
@@ -149,7 +150,6 @@ class HomeFragment : BaseFragment<HomePresenter>(), HomeContract.View {
         floatbtn.run {
             backgroundTintList = SettingUtil.getOneColorStateList(_mActivity)
             setOnClickListener {
-
                 val layoutManager = swiperecyclerview.layoutManager as LinearLayoutManager
                 //如果当前recyclerview 最后一个视图位置的索引大于等于40,则迅速返回顶部,否则带有滚动动画效果返回到顶部
                 if (layoutManager.findLastVisibleItemPosition() >= 40) {
@@ -348,7 +348,6 @@ class HomeFragment : BaseFragment<HomePresenter>(), HomeContract.View {
     fun settingEvent(event: SettingChangeEvent) {
         setUiTheme(_mActivity, listOf(toolbar,floatbtn,swipeRefreshLayout,loadsir,footView,adapter))
     }
-
 }
 
 

+ 2 - 0
app/src/main/java/me/hegj/wandroid/mvp/ui/activity/main/home/search/SearchActivity.kt

@@ -40,7 +40,9 @@ import me.hegj.wandroid.mvp.ui.adapter.SearchistoryAdapter
 class SearchActivity : BaseActivity<SearchPresenter>(), SearchContract.View {
 
     var mtagData = mutableListOf<SearchResponse>()//搜索热词数据
+
     var historyData = mutableListOf<String>()//搜索历史数据
+
     lateinit var adapter: SearchistoryAdapter//搜索历史适配器
 
     override fun setupActivityComponent(appComponent: AppComponent) {

+ 0 - 1
app/src/main/java/me/hegj/wandroid/mvp/ui/activity/main/me/MeFragment.kt

@@ -209,6 +209,5 @@ class MeFragment : BaseFragment<MePresenter>(), MeContract.View {
             ShowUtils.showToast(_mActivity,"未安装手机QQ或安装的版本不支持")
             false
         }
-
     }
 }

+ 2 - 1
app/src/main/java/me/hegj/wandroid/mvp/ui/activity/main/project/ProjectFragment.kt

@@ -4,6 +4,7 @@ import android.content.Context
 import android.graphics.Color
 import android.os.Bundle
 import android.text.Html
+import android.util.Log
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
@@ -27,6 +28,7 @@ import me.hegj.wandroid.di.component.main.project.DaggerProjectComponent
 import me.hegj.wandroid.di.module.main.project.ProjectModule
 import me.hegj.wandroid.mvp.contract.main.project.ProjectContract
 import me.hegj.wandroid.mvp.model.entity.ClassifyResponse
+import me.hegj.wandroid.mvp.model.entity.UserInfoResponse
 import me.hegj.wandroid.mvp.presenter.main.project.ProjectPresenter
 import me.hegj.wandroid.mvp.ui.BaseFragment
 import me.hegj.wandroid.mvp.ui.adapter.ViewPagerAdapter
@@ -162,5 +164,4 @@ class ProjectFragment : BaseFragment<ProjectPresenter>(), ProjectContract.View {
     fun settingEvent(event: SettingChangeEvent) {
         setUiTheme(_mActivity, listOf(viewpager_linear,loadsir))
     }
-
 }

+ 14 - 26
app/src/main/java/me/hegj/wandroid/mvp/ui/activity/start/LoginActivity.kt

@@ -14,12 +14,14 @@ import me.hegj.wandroid.R
 import me.hegj.wandroid.app.event.LoginFreshEvent
 import me.hegj.wandroid.app.utils.CacheUtil
 import me.hegj.wandroid.app.utils.SettingUtil
+import me.hegj.wandroid.app.utils.afterTextChange
 import me.hegj.wandroid.di.component.start.DaggerLoginComponent
 import me.hegj.wandroid.di.module.start.LoginModule
 import me.hegj.wandroid.mvp.contract.start.LoginContract
 import me.hegj.wandroid.mvp.model.entity.UserInfoResponse
 import me.hegj.wandroid.mvp.presenter.start.LoginPresenter
 import me.hegj.wandroid.mvp.ui.BaseActivity
+import org.greenrobot.eventbus.EventBus
 
 /**
  * 登录
@@ -51,34 +53,20 @@ class LoginActivity : BaseActivity<LoginPresenter>(), LoginContract.View {
         }
         SettingUtil.setShapColor(login_sub, SettingUtil.getColor(this))
         login_goregister?.setTextColor(SettingUtil.getColor(this))
-        login_username.addTextChangedListener(object : TextWatcher {
-
-            override fun afterTextChanged(s: Editable?) {}
-
-            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
-
-            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
-                if (s.isNotEmpty()) {
-                    login_clear.visibility = View.VISIBLE
-                } else {
-                    login_clear.visibility = View.GONE
-                }
+        login_username.afterTextChange {
+            if (it.isNotEmpty()) {
+                login_clear.visibility = View.VISIBLE
+            } else {
+                login_clear.visibility = View.GONE
             }
-        })
-        login_pwd.addTextChangedListener(object : TextWatcher {
-
-            override fun afterTextChanged(s: Editable?) {}
-
-            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
-
-            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
-                if (s.isNotEmpty()) {
-                    login_key.visibility = View.VISIBLE
-                } else {
-                    login_key.visibility = View.GONE
-                }
+        }
+        login_pwd.afterTextChange {
+            if (it.isNotEmpty()) {
+                login_key.visibility = View.VISIBLE
+            } else {
+                login_key.visibility = View.GONE
             }
-        })
+        }
         login_key.setOnCheckedChangeListener { _, isChecked ->
             if (isChecked) {
                 login_pwd.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

+ 19 - 38
app/src/main/java/me/hegj/wandroid/mvp/ui/activity/start/RegisterActivity.kt

@@ -15,6 +15,7 @@ import me.hegj.wandroid.R
 import me.hegj.wandroid.app.event.LoginFreshEvent
 import me.hegj.wandroid.app.utils.CacheUtil
 import me.hegj.wandroid.app.utils.SettingUtil
+import me.hegj.wandroid.app.utils.afterTextChange
 import me.hegj.wandroid.di.component.start.DaggerLoginComponent
 import me.hegj.wandroid.di.module.start.LoginModule
 import me.hegj.wandroid.mvp.contract.start.LoginContract
@@ -52,48 +53,28 @@ class RegisterActivity : BaseActivity<LoginPresenter>(), LoginContract.View {
         }
         SettingUtil.setShapColor(register_sub, SettingUtil.getColor(this))
         login_goregister?.setTextColor(SettingUtil.getColor(this))
-        register_username.addTextChangedListener(object : TextWatcher {
 
-            override fun afterTextChanged(s: Editable?) {}
-
-            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
-
-            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
-                if (s.isNotEmpty()) {
-                    register_clear.visibility = View.VISIBLE
-                } else {
-                    register_clear.visibility = View.GONE
-                }
+        register_username.afterTextChange {
+            if (it.isNotEmpty()) {
+                register_clear.visibility = View.VISIBLE
+            } else {
+                register_clear.visibility = View.GONE
             }
-        })
-        register_pwd.addTextChangedListener(object : TextWatcher {
-
-            override fun afterTextChanged(s: Editable?) {}
-
-            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
-
-            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
-                if (s.isNotEmpty()) {
-                    register_key.visibility = View.VISIBLE
-                } else {
-                    register_key.visibility = View.GONE
-                }
+        }
+        register_pwd.afterTextChange {
+            if (it.isNotEmpty()) {
+                register_key.visibility = View.VISIBLE
+            } else {
+                register_key.visibility = View.GONE
             }
-        })
-        register_pwd1.addTextChangedListener(object : TextWatcher {
-
-            override fun afterTextChanged(s: Editable?) {}
-
-            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
-
-            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
-                if (s.isNotEmpty()) {
-                    register_key1.visibility = View.VISIBLE
-                } else {
-                    register_key1.visibility = View.GONE
-                }
+        }
+        register_pwd1.afterTextChange {
+            if (it.isNotEmpty()) {
+                register_key1.visibility = View.VISIBLE
+            } else {
+                register_key1.visibility = View.GONE
             }
-        })
+        }
         register_key.setOnCheckedChangeListener { _, isChecked ->
             if (isChecked) {
                 register_pwd.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

+ 17 - 14
app/src/main/java/me/hegj/wandroid/mvp/ui/activity/web/WebviewActivity.kt

@@ -11,7 +11,6 @@ import android.view.Window
 import android.widget.LinearLayout
 import androidx.core.content.ContextCompat
 import com.jess.arms.di.component.AppComponent
-import com.jess.arms.integration.AppManager
 import com.just.agentweb.AgentWeb
 import kotlinx.android.synthetic.main.activity_webview.*
 import kotlinx.android.synthetic.main.include_toolbar.*
@@ -36,12 +35,15 @@ import org.greenrobot.eventbus.Subscribe
 
 class WebviewActivity : BaseActivity<WebviewPresenter>(), WebviewContract.View {
 
-    var collect = false//是否收藏
-    var id = 0//id
-    lateinit var showTitle:String//标题
-    lateinit var url:String
-
-    var collectTYpe = 0 //需要收藏的类型 具体参数说明请看 CollectType 枚举类
+    //是否收藏
+    var collect = false
+    //id
+    var id = 0
+    lateinit var showTitle: String
+    //标题
+    lateinit var url: String
+    //需要收藏的类型 具体参数说明请看 CollectType 枚举类
+    var collectTYpe = 0
 
     private lateinit var mAgentWeb: AgentWeb
 
@@ -156,18 +158,18 @@ class WebviewActivity : BaseActivity<WebviewPresenter>(), WebviewContract.View {
                 if (CacheUtil.isLogin()) {
                     //是否已经收藏了
                     if (collect) {
-                        if(collectTYpe==CollectType.Url.type){
+                        if (collectTYpe == CollectType.Url.type) {
                             //取消收藏网址
                             mPresenter?.uncollectUrl(id)
-                        }else{
+                        } else {
                             //取消收藏文章
                             mPresenter?.uncollect(id)
                         }
                     } else {
-                        if(collectTYpe==CollectType.Url.type){
+                        if (collectTYpe == CollectType.Url.type) {
                             //收藏网址
-                            mPresenter?.collectUrl(showTitle,url)
-                        }else{
+                            mPresenter?.collectUrl(showTitle, url)
+                        } else {
                             //收藏文章
                             mPresenter?.collect(id)
                         }
@@ -195,7 +197,7 @@ class WebviewActivity : BaseActivity<WebviewPresenter>(), WebviewContract.View {
         window.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL)
         invalidateOptionsMenu()
         //通知app刷新相对应ID的数据的收藏的值
-        CollectEvent(collected,id).post()
+        CollectEvent(collected, id).post()
     }
 
     /**
@@ -208,8 +210,9 @@ class WebviewActivity : BaseActivity<WebviewPresenter>(), WebviewContract.View {
         invalidateOptionsMenu()
         id = data.id
         //通知app刷新相对应ID的数据的收藏的值
-        CollectEvent(collected,id).post()
+        CollectEvent(collected, id).post()
     }
+
     @Subscribe
     fun freshLogin(event: LoginFreshEvent) {
         //如果是登录了, 当前界面的id与账户收藏集合id匹配的值需要设置已经收藏 并刷新menu

+ 3 - 1
app/src/main/res/drawable/title_lable_shap1.xml

@@ -4,6 +4,8 @@
     <stroke
         android:width="0.5dp"
         android:color="@android:color/holo_green_dark" />
-    <solid android:color="@color/white"/>
+    <solid android:color="@color/white" />
+
     <corners android:radius="1dp" />
+
 </shape>

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

@@ -1,7 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
-    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@color/white"

+ 1 - 0
app/src/main/res/layout/item_ariticle.xml

@@ -22,6 +22,7 @@
             android:layout_marginStart="8dp"
             android:layout_marginLeft="8dp"
             android:layout_marginTop="12dp"
+            android:background="@drawable/item_imgselector"
             android:text="有何高见"
             android:background="@drawable/item_imgselector"
             android:textColor="@color/colorBlack666"