Skip to content

Commit

Permalink
加入 Kotlin 协程
Browse files Browse the repository at this point in the history
优化部分代码的写法
  • Loading branch information
getActivity committed Jan 8, 2022
1 parent 74541ff commit 270e035
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 78 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

* 博客地址:[但愿人长久,搬砖不再有](https://www.jianshu.com/p/77dd326f21dc)

* 另外你如果对 Kotlin 不熟悉,恰好想学习的话,推荐你通过下面这三篇文章来学习

* [全民 Kotlin:Java我们不一样](https://www.jianshu.com/p/a01e6b957269)

* [全民 Kotlin:你没有玩过的全新玩法](https://www.jianshu.com/p/884ca0a49e5e)

* [全民 Kotlin:协程特别篇](https://www.jianshu.com/p/2e0746c7d4f3)

* 当我们日复一日年复一年的搬砖的时候,你是否曾想过提升一下开发效率,如果一个通用的架构摆在你的面前,你还会选择自己搭架构么,但是搭建出一个好的架构并非易事,有多少人愿意选择去做,还有多少人选择努力去做好,可能寥寥无几,但是你今天看到的,正是你所想要的,一个真正能解决你开发新项目时最大痛点的架构工程,你不需要再麻木 Copy 原有旧项目的代码,只需改动少量代码就能得到想要的效果,你会发现开发新项目其实是一件很快乐的事。

* AndroidProject 已维护三年多的时间,几乎耗尽我所有的业余时间,里面的代码改了再改,改了又改,不断 Review、不断创新、不断改进、不断测试、不断优化,每天都在重复这些枯燥的步骤,但是只有这样才能把这件事做好,因为我相信把同样一件事重复做,迟早有一天可以做好。
Expand Down
12 changes: 2 additions & 10 deletions app/src/main/java/com/hjq/demo/manager/ActivityManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,10 @@ class ActivityManager private constructor() : ActivityLifecycleCallbacks {
companion object {

@Suppress("StaticFieldLeak")
@Volatile
private var sInstance: ActivityManager? = null
private val activityManager: ActivityManager by lazy { ActivityManager() }

fun getInstance(): ActivityManager {
if (sInstance == null) {
synchronized(ActivityManager::class.java) {
if (sInstance == null) {
sInstance = ActivityManager()
}
}
}
return sInstance!!
return activityManager
}

/**
Expand Down
34 changes: 0 additions & 34 deletions app/src/main/java/com/hjq/demo/manager/ThreadPoolManager.kt

This file was deleted.

11 changes: 8 additions & 3 deletions app/src/main/java/com/hjq/demo/ui/activity/CrashActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ import android.view.View
import android.widget.TextView
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.lifecycleScope
import com.gyf.immersionbar.ImmersionBar
import com.hjq.demo.R
import com.hjq.demo.aop.SingleClick
import com.hjq.demo.app.AppActivity
import com.hjq.demo.manager.ThreadPoolManager
import com.hjq.demo.other.AppConfig
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.tencent.bugly.crashreport.CrashReport
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.PrintWriter
import java.io.StringWriter
import java.net.InetAddress
Expand Down Expand Up @@ -227,14 +229,17 @@ class CrashActivity : AppActivity() {
}
if (permissions.contains(Manifest.permission.INTERNET)) {
builder.append("\n当前网络访问:\t")
ThreadPoolManager.getInstance().execute {

lifecycleScope.launch(Dispatchers.IO) {
try {
InetAddress.getByName("www.baidu.com")
builder.append("正常")
} catch (ignored: UnknownHostException) {
builder.append("异常")
}
post { infoView?.text = builder }
lifecycleScope.launch(Dispatchers.Main) {
infoView?.text = builder
}
}
} else {
infoView?.text = builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.provider.MediaStore
import android.text.TextUtils
import android.view.*
import android.view.animation.*
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.hjq.base.BaseActivity
import com.hjq.base.BaseAdapter
Expand All @@ -18,7 +19,6 @@ import com.hjq.demo.aop.Log
import com.hjq.demo.aop.Permissions
import com.hjq.demo.aop.SingleClick
import com.hjq.demo.app.AppActivity
import com.hjq.demo.manager.ThreadPoolManager
import com.hjq.demo.other.GridSpaceDecoration
import com.hjq.demo.ui.activity.CameraActivity.OnCameraListener
import com.hjq.demo.ui.adapter.ImageSelectAdapter
Expand All @@ -28,6 +28,8 @@ import com.hjq.demo.widget.StatusLayout
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.hjq.widget.view.FloatActionButton
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import java.util.*

Expand Down Expand Up @@ -150,7 +152,7 @@ class ImageSelectActivity : AppActivity(), StatusAction, Runnable,
// 显示加载进度条
showLoading()
// 加载图片列表
ThreadPoolManager.getInstance().execute(this)
lifecycleScope.launch(Dispatchers.IO) { run() }
}

override fun getStatusLayout(): StatusLayout? {
Expand Down Expand Up @@ -238,7 +240,7 @@ class ImageSelectActivity : AppActivity(), StatusAction, Runnable,
// 这里需要延迟刷新,否则可能会找不到拍照的图片
postDelayed({
// 重新加载图片列表
ThreadPoolManager.getInstance().execute(this@ImageSelectActivity)
lifecycleScope.launch { run() }
}, 1000)
}

Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/com/hjq/demo/ui/activity/SettingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.hjq.demo.ui.activity

import android.view.Gravity
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.hjq.base.BaseDialog
import com.hjq.base.action.AnimAction
import com.hjq.demo.R
Expand All @@ -12,7 +13,6 @@ import com.hjq.demo.http.glide.GlideApp
import com.hjq.demo.http.model.HttpData
import com.hjq.demo.manager.ActivityManager
import com.hjq.demo.manager.CacheDataManager
import com.hjq.demo.manager.ThreadPoolManager
import com.hjq.demo.other.AppConfig
import com.hjq.demo.ui.dialog.MenuDialog
import com.hjq.demo.ui.dialog.SafeDialog
Expand All @@ -21,6 +21,9 @@ import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallback
import com.hjq.widget.layout.SettingBar
import com.hjq.widget.view.SwitchButton
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
* author : Android 轮子哥
Expand Down Expand Up @@ -132,11 +135,11 @@ class SettingActivity : AppActivity(), SwitchButton.OnCheckedChangeListener {

// 清除内存缓存(必须在主线程)
GlideApp.get(this@SettingActivity).clearMemory()
ThreadPoolManager.getInstance().execute {
CacheDataManager.clearAllCache(this)
lifecycleScope.launch(Dispatchers.IO) {
CacheDataManager.clearAllCache(this@SettingActivity)
// 清除本地缓存(必须在子线程)
GlideApp.get(this@SettingActivity).clearDiskCache()
post {
withContext(Dispatchers.Main) {
// 重新获取应用缓存大小
cleanCacheView?.setRightText(CacheDataManager.getTotalCacheSize(this@SettingActivity))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.os.Parcelable
import android.provider.MediaStore
import android.view.*
import android.view.animation.*
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.hjq.base.BaseActivity
import com.hjq.base.BaseAdapter
Expand All @@ -21,7 +22,6 @@ import com.hjq.demo.aop.Log
import com.hjq.demo.aop.Permissions
import com.hjq.demo.aop.SingleClick
import com.hjq.demo.app.AppActivity
import com.hjq.demo.manager.ThreadPoolManager
import com.hjq.demo.other.GridSpaceDecoration
import com.hjq.demo.ui.activity.CameraActivity.OnCameraListener
import com.hjq.demo.ui.adapter.VideoSelectAdapter
Expand All @@ -32,6 +32,8 @@ import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.hjq.widget.view.FloatActionButton
import com.tencent.bugly.crashreport.CrashReport
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import java.util.*

Expand Down Expand Up @@ -150,7 +152,7 @@ class VideoSelectActivity : AppActivity(), StatusAction, Runnable, BaseAdapter.O
// 显示加载进度条
showLoading()
// 加载视频列表
ThreadPoolManager.getInstance().execute(this)
lifecycleScope.launch(Dispatchers.IO) { run() }
}

override fun getStatusLayout(): StatusLayout? {
Expand Down Expand Up @@ -241,7 +243,7 @@ class VideoSelectActivity : AppActivity(), StatusAction, Runnable, BaseAdapter.O
// 这里需要延迟刷新,否则可能会找不到拍照的视频
postDelayed({
// 重新加载视频列表
ThreadPoolManager.getInstance().execute(this@VideoSelectActivity)
lifecycleScope.launch { run() }
}, 1000)
}

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/hjq/demo/ui/fragment/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package com.hjq.demo.ui.fragment

import android.content.res.ColorStateList
import android.widget.TextView
import androidx.appcompat.widget.*
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
Expand All @@ -13,7 +14,7 @@ import com.hjq.demo.R
import com.hjq.demo.app.AppFragment
import com.hjq.demo.app.TitleBarFragment
import com.hjq.demo.ui.activity.HomeActivity
import com.hjq.demo.ui.adapter.*
import com.hjq.demo.ui.adapter.TabAdapter
import com.hjq.demo.ui.adapter.TabAdapter.OnTabListener
import com.hjq.demo.widget.XCollapsingToolbarLayout
import com.hjq.demo.widget.XCollapsingToolbarLayout.OnScrimsListener
Expand Down
6 changes: 6 additions & 0 deletions common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
// Material 库:https://github.com/material-components/material-components-android
implementation 'com.google.android.material:material:1.4.0'

// Kotlin 协程:https://github.com/Kotlin/kotlinx.coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ interface HandlerAction {
* 延迟一段时间执行
*/
fun postDelayed(runnable: Runnable, delayMillis: Long): Boolean {
var delayMillis: Long = delayMillis
if (delayMillis < 0) {
delayMillis = 0
}
return postAtTime(runnable, SystemClock.uptimeMillis() + delayMillis)
return postAtTime(runnable, SystemClock.uptimeMillis() + if (delayMillis < 0) 0 else delayMillis)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.hjq.widget.view
import android.content.Context
import android.graphics.drawable.Drawable
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.MotionEvent
Expand Down Expand Up @@ -60,11 +61,7 @@ class ClearEditText @JvmOverloads constructor(
* [OnFocusChangeListener]
*/
override fun onFocusChange(view: View, hasFocus: Boolean) {
if (hasFocus && text != null) {
setDrawableVisible(text!!.isNotEmpty())
} else {
setDrawableVisible(false)
}
setDrawableVisible(hasFocus && !TextUtils.isEmpty(text))
focusChangeListener?.onFocusChange(view, hasFocus)
}

Expand All @@ -91,7 +88,7 @@ class ClearEditText @JvmOverloads constructor(
}
return true
}
return touchListener != null && touchListener!!.onTouch(view, event)
return touchListener?.onTouch(view, event) ?: false
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.hjq.widget.view

import android.content.*
import android.content.Context
import android.graphics.drawable.Drawable
import android.text.*
import android.text.Editable
import android.text.InputType
import android.text.TextUtils
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.util.AttributeSet
import android.view.*
import android.view.MotionEvent
import android.view.View
import android.view.View.OnFocusChangeListener
import android.view.View.OnTouchListener
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -76,11 +80,7 @@ class PasswordEditText @JvmOverloads constructor(
* [OnFocusChangeListener]
*/
override fun onFocusChange(view: View?, hasFocus: Boolean) {
if (hasFocus && text != null) {
setDrawableVisible(text!!.isNotEmpty())
} else {
setDrawableVisible(false)
}
setDrawableVisible(hasFocus && !TextUtils.isEmpty(text))
focusChangeListener?.onFocusChange(view, hasFocus)
}

Expand Down

0 comments on commit 270e035

Please sign in to comment.