Skip to content

Commit

Permalink
添加kotlin拓展函数,优化了一点点代码
Browse files Browse the repository at this point in the history
  • Loading branch information
hegj committed Mar 9, 2020
1 parent 536f315 commit 88cf7ce
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 123 deletions.
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
Expand Down
14 changes: 6 additions & 8 deletions app/src/main/java/me/hegj/wandroid/app/utils/HttpUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
71 changes: 44 additions & 27 deletions app/src/main/java/me/hegj/wandroid/app/utils/KotlinUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,不然会出现问题,
Expand All @@ -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)
}
Expand All @@ -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) {

}
})
}
1 change: 0 additions & 1 deletion app/src/main/java/me/hegj/wandroid/app/utils/ShowUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ object ShowUtils {
val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}

}


Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/me/hegj/wandroid/mvp/model/api/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

/**
Expand Down Expand Up @@ -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数据
*/
Expand Down Expand Up @@ -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>>>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -348,7 +348,6 @@ class HomeFragment : BaseFragment<HomePresenter>(), HomeContract.View {
fun settingEvent(event: SettingChangeEvent) {
setUiTheme(_mActivity, listOf(toolbar,floatbtn,swipeRefreshLayout,loadsir,footView,adapter))
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,5 @@ class MeFragment : BaseFragment<MePresenter>(), MeContract.View {
ShowUtils.showToast(_mActivity,"未安装手机QQ或安装的版本不支持")
false
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -162,5 +164,4 @@ class ProjectFragment : BaseFragment<ProjectPresenter>(), ProjectContract.View {
fun settingEvent(event: SettingChangeEvent) {
setUiTheme(_mActivity, listOf(viewpager_linear,loadsir))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
* 登录
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 88cf7ce

Please sign in to comment.