diff --git a/README.md b/README.md
index 3f8f835..ffcc3fb 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@
![](https://img.shields.io/badge/language-Java%20&%20kotlin-orange.svg)
![](https://img.shields.io/hexpm/l/plug.svg)
-最近更新 2021.05.25
+最近更新 2021.10.16
### How to use
@@ -54,10 +54,12 @@ LogUtil.setIsLog(true);
* Context getAppContext()
获取全局上下文
-* void showLoading(Activity activity,String msg)
Loading加载框
+* void showLoading(Activity activity,String msg)
Loading加载框,kotlin版本参数更多
* void hideLoading()
hideLoading
+* boolean getLoadingDialog()
获取dialog
+
* boolean loadingIsShowing()
loading是否显示
* void finishBySleep(final long millis)
根据时间休眠然后关闭当前页面,比如:3秒自动返回
@@ -232,6 +234,9 @@ like this
## 近期更新
+#### 3.2.2(2021-10-16)
+- optimized code
+
#### 3.2.1(2021-05-25)
- 优化调整
- 添加Toast扩展
diff --git a/app/src/main/java/com/yechaoa/app/MainActivity.kt b/app/src/main/java/com/yechaoa/app/MainActivity.kt
index 46773b7..4668c67 100644
--- a/app/src/main/java/com/yechaoa/app/MainActivity.kt
+++ b/app/src/main/java/com/yechaoa/app/MainActivity.kt
@@ -46,7 +46,9 @@ class MainActivity : AppCompatActivity() {
}
mBinding.btnShowLoading.setOnClickListener {
- YUtils.showLoading(this, "test")
+ YUtils.showLoading(this, "test", cancelListener = {
+ LogUtil.i("dialog canceled")
+ })
LogUtil.i("" + YUtils.loadingIsShowing())
}
diff --git a/build.gradle b/build.gradle
index 0be4338..20453c9 100644
--- a/build.gradle
+++ b/build.gradle
@@ -5,7 +5,7 @@ buildscript {
repositories {
google()
- jcenter()
+ mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.2'
@@ -19,7 +19,7 @@ buildscript {
allprojects {
repositories {
google()
- jcenter()
+ mavenCentral()
}
}
diff --git a/yutilskt/build.gradle b/yutilskt/build.gradle
index 3127a9b..187101f 100644
--- a/yutilskt/build.gradle
+++ b/yutilskt/build.gradle
@@ -1,6 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
-apply plugin: 'kotlin-android-extensions'
+
android {
compileSdkVersion 30
@@ -27,7 +27,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//implementation 'com.android.support:appcompat-v7:28.0.0'
- testImplementation 'junit:junit:4.13'
- androidTestImplementation 'androidx.test.ext:junit:1.1.1'
- androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
+ testImplementation 'junit:junit:4.13.2'
+ androidTestImplementation 'androidx.test.ext:junit:1.1.2'
+ androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
diff --git a/yutilskt/src/main/java/com/yechaoa/yutilskt/ParseUtil.kt b/yutilskt/src/main/java/com/yechaoa/yutilskt/ParseUtil.kt
index d02684b..4dd7cfa 100644
--- a/yutilskt/src/main/java/com/yechaoa/yutilskt/ParseUtil.kt
+++ b/yutilskt/src/main/java/com/yechaoa/yutilskt/ParseUtil.kt
@@ -26,8 +26,10 @@ object ParseUtil {
fun parseCode(response: String?): String {
var code = ""
try {
- val json = JSONObject(response)
- code = json.getString("code")
+ response?.let {
+ val json = JSONObject(response)
+ code = json.getString("code")
+ }
} catch (e: JSONException) {
e.printStackTrace()
}
@@ -40,8 +42,10 @@ object ParseUtil {
fun parseFlag(response: String?): Boolean {
var flag = false
try {
- val json = JSONObject(response)
- flag = json.getBoolean("flag")
+ response?.let {
+ val json = JSONObject(response)
+ flag = json.getBoolean("flag")
+ }
} catch (e: JSONException) {
e.printStackTrace()
}
@@ -54,8 +58,10 @@ object ParseUtil {
fun parseData(response: String?): String {
var data = ""
try {
- val json = JSONObject(response)
- data = json.getString("data")
+ response?.let {
+ val json = JSONObject(response)
+ data = json.getString("data")
+ }
} catch (e: JSONException) {
e.printStackTrace()
}
@@ -68,10 +74,12 @@ object ParseUtil {
fun parseInfo(response: String?): String {
var info = ""
try {
- val json = JSONObject(response)
- info = json.getString("info")
- } catch (`var`: JSONException) {
- `var`.printStackTrace()
+ response?.let {
+ val json = JSONObject(response)
+ info = json.getString("info")
+ }
+ } catch (e: JSONException) {
+ e.printStackTrace()
}
return info
}
@@ -79,13 +87,15 @@ object ParseUtil {
/**
* 根据key值解析,只支持json中的一级字段
*/
- fun parseByKey(response: String?, key: String?): String {
+ fun parseByKey(response: String?, key: String): String {
var value = ""
try {
- val json = JSONObject(response)
- value = json.getString(key)
- } catch (`var`: JSONException) {
- `var`.printStackTrace()
+ response?.let {
+ val json = JSONObject(response)
+ value = json.getString(key)
+ }
+ } catch (e: JSONException) {
+ e.printStackTrace()
}
return value
}
diff --git a/yutilskt/src/main/java/com/yechaoa/yutilskt/ShareUtil.kt b/yutilskt/src/main/java/com/yechaoa/yutilskt/ShareUtil.kt
index 00703be..8b8418e 100644
--- a/yutilskt/src/main/java/com/yechaoa/yutilskt/ShareUtil.kt
+++ b/yutilskt/src/main/java/com/yechaoa/yutilskt/ShareUtil.kt
@@ -16,7 +16,7 @@ object ShareUtil {
/**
* 调用系统分享图片
*/
- fun shareImage(title: String?, uri: Uri?) {
+ fun shareImage(title: String, uri: Uri) {
val intent = Intent()
intent.action = Intent.ACTION_SEND
intent.type = "image/*"
@@ -27,7 +27,7 @@ object ShareUtil {
/**
* 调用系统分享文字
*/
- fun shareText(title: String?, text: String?) {
+ fun shareText(title: String, text: String) {
val intent = Intent()
intent.action = Intent.ACTION_SEND
intent.type = "text/plain"
diff --git a/yutilskt/src/main/java/com/yechaoa/yutilskt/SpUtil.kt b/yutilskt/src/main/java/com/yechaoa/yutilskt/SpUtil.kt
index 67b3091..0560343 100644
--- a/yutilskt/src/main/java/com/yechaoa/yutilskt/SpUtil.kt
+++ b/yutilskt/src/main/java/com/yechaoa/yutilskt/SpUtil.kt
@@ -18,73 +18,73 @@ object SpUtil {
/**
* String
*/
- fun setString(key: String?, value: String?) {
+ fun setString(key: String, value: String) {
sp.edit().putString(key, value).apply()
}
- fun getString(key: String?, defValue: String = ""): String {
+ fun getString(key: String, defValue: String = ""): String {
return sp.getString(key, defValue)!!
}
/**
* StringSet
*/
- fun setStringSet(key: String?, value: Set?) {
+ fun setStringSet(key: String, value: Set?) {
sp.edit().putStringSet(key, value).apply()
}
- fun getStringSet(key: String?): Set {
+ fun getStringSet(key: String): Set {
return HashSet(sp.getStringSet(key, HashSet()))
}
/**
* Int
*/
- fun setInt(key: String?, value: Int) {
+ fun setInt(key: String, value: Int) {
sp.edit().putInt(key, value).apply()
}
- fun getInt(key: String?, defValue: Int = 0): Int {
+ fun getInt(key: String, defValue: Int = 0): Int {
return sp.getInt(key, defValue)
}
/**
* Boolean
*/
- fun setBoolean(key: String?, value: Boolean) {
+ fun setBoolean(key: String, value: Boolean) {
sp.edit().putBoolean(key, value).apply()
}
- fun getBoolean(key: String?, defValue: Boolean = false): Boolean {
+ fun getBoolean(key: String, defValue: Boolean = false): Boolean {
return sp.getBoolean(key, defValue)
}
/**
* Float
*/
- fun setFloat(key: String?, value: Float?) {
- sp.edit().putFloat(key, value!!).apply()
+ fun setFloat(key: String, value: Float) {
+ sp.edit().putFloat(key, value).apply()
}
- fun getFloat(key: String?, defValue: Float = 0f): Float {
+ fun getFloat(key: String, defValue: Float = 0f): Float {
return sp.getFloat(key, defValue)
}
/**
* Long
*/
- fun setLong(key: String?, value: Long?) {
- sp.edit().putLong(key, value!!).apply()
+ fun setLong(key: String, value: Long) {
+ sp.edit().putLong(key, value).apply()
}
- fun getLong(key: String?, defValue: Long = 0): Long {
+ fun getLong(key: String, defValue: Long = 0): Long {
return sp.getLong(key, defValue)
}
/**
* Remove
*/
- fun removeByKey(key: String?) {
+ fun removeByKey(key: String) {
sp.edit().remove(key).apply()
}
diff --git a/yutilskt/src/main/java/com/yechaoa/yutilskt/TimeUtil.kt b/yutilskt/src/main/java/com/yechaoa/yutilskt/TimeUtil.kt
index c3e2c09..506d32e 100644
--- a/yutilskt/src/main/java/com/yechaoa/yutilskt/TimeUtil.kt
+++ b/yutilskt/src/main/java/com/yechaoa/yutilskt/TimeUtil.kt
@@ -51,7 +51,7 @@ object TimeUtil {
/**
* 转换为年月日
*/
- fun formatDate(mDate: String?): String {
+ fun formatDate(mDate: String): String {
val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
var date: Date? = null
try {
@@ -59,6 +59,6 @@ object TimeUtil {
} catch (e: ParseException) {
e.printStackTrace()
}
- return sdf.format(date)
+ return if (null != date) sdf.format(date) else ""
}
}
\ No newline at end of file
diff --git a/yutilskt/src/main/java/com/yechaoa/yutilskt/ToastUtil.kt b/yutilskt/src/main/java/com/yechaoa/yutilskt/ToastUtil.kt
index 7b4a550..07f9882 100644
--- a/yutilskt/src/main/java/com/yechaoa/yutilskt/ToastUtil.kt
+++ b/yutilskt/src/main/java/com/yechaoa/yutilskt/ToastUtil.kt
@@ -44,12 +44,12 @@ object ToastUtil {
*/
@SuppressLint("ShowToast")
private fun createToast(msg: String) {
- if (toast == null) {
+ if (null == toast) {
toast = Toast.makeText(YUtils.getApp().applicationContext, msg, Toast.LENGTH_SHORT)
} else {
- toast!!.setText(msg)
+ toast?.setText(msg)
}
- toast!!.show()
+ toast?.show()
}
/**
@@ -62,7 +62,7 @@ object ToastUtil {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg)
} else {
- ActivityUtil.currentActivity!!.runOnUiThread { createCenterToast(msg) }
+ ActivityUtil.currentActivity?.runOnUiThread { createCenterToast(msg) }
}
}
@@ -70,7 +70,7 @@ object ToastUtil {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg)
} else {
- ActivityUtil.currentActivity!!.runOnUiThread { createCenterToast(msg) }
+ ActivityUtil.currentActivity?.runOnUiThread { createCenterToast(msg) }
}
}
@@ -81,13 +81,13 @@ object ToastUtil {
*/
@SuppressLint("ShowToast")
private fun createCenterToast(msg: String) {
- if (toast == null) {
+ if (null == toast) {
toast = Toast.makeText(YUtils.getApp().applicationContext, msg, Toast.LENGTH_SHORT)
} else {
- toast!!.setText(msg)
+ toast?.setText(msg)
}
- toast!!.setGravity(Gravity.CENTER, 0, 0)
- toast!!.show()
+ toast?.setGravity(Gravity.CENTER, 0, 0)
+ toast?.show()
}
/**
diff --git a/yutilskt/src/main/java/com/yechaoa/yutilskt/YUtils.kt b/yutilskt/src/main/java/com/yechaoa/yutilskt/YUtils.kt
index 0970b09..89b508d 100644
--- a/yutilskt/src/main/java/com/yechaoa/yutilskt/YUtils.kt
+++ b/yutilskt/src/main/java/com/yechaoa/yutilskt/YUtils.kt
@@ -85,8 +85,8 @@ object YUtils {
/**
* Loading加载框
*/
- fun showLoading(activity: Activity, msg: String, cancelable: Boolean = true) {
- yLoadingDialog = YLoadingDialog(activity, msg, cancelable)
+ fun showLoading(activity: Activity, msg: String, cancelable: Boolean = true, cancelListener: (() -> Unit)? = null) {
+ yLoadingDialog = YLoadingDialog(activity, msg, cancelable, cancelListener = cancelListener)
yLoadingDialog?.show()
}
@@ -100,6 +100,13 @@ object YUtils {
}
}
+ /**
+ * getLoadingDialog
+ */
+ fun getLoadingDialog(): YLoadingDialog? {
+ return yLoadingDialog
+ }
+
/**
* loading是否显示,需在showLoading()之后调用,否则为null
*/
@@ -126,14 +133,14 @@ object YUtils {
/**
* 获取版本名
*/
- fun getVersionName(): String? {
+ fun getVersionName(): String {
return try {
val packageManager = getApp().packageManager
val packageInfo = packageManager.getPackageInfo(getApp().packageName, 0)
packageInfo.versionName
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
- null
+ ""
}
}
@@ -154,7 +161,7 @@ object YUtils {
/**
* 检验手机号
*/
- fun checkPhoneNumber(number: String?): Boolean {
+ fun checkPhoneNumber(number: String): Boolean {
var p: Pattern? = null
var m: Matcher? = null
var b = false
@@ -210,19 +217,17 @@ object YUtils {
*/
fun foreground(view: View?, color: Int, start: Int, end: Int): View? {
if (view is Button) {
- val btn = view
// 获取文字
- val span: Spannable = SpannableString(btn.text.toString())
+ val span: Spannable = SpannableString(view.text.toString())
//设置颜色和起始位置
span.setSpan(ForegroundColorSpan(color), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
- btn.text = span
- return btn
+ view.text = span
+ return view
} else if (view is TextView) { //EditText extends TextView
- val text = view
- val span: Spannable = SpannableString(text.text.toString())
+ val span: Spannable = SpannableString(view.text.toString())
span.setSpan(ForegroundColorSpan(color), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
- text.text = span
- return text
+ view.text = span
+ return view
}
return null
}
@@ -239,9 +244,8 @@ object YUtils {
* 关闭软键盘
*/
fun closeSoftKeyboard() {
- val inputManger =
- ActivityUtil.currentActivity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
- inputManger.hideSoftInputFromWindow(ActivityUtil.currentActivity!!.window.decorView.windowToken, 0)
+ val inputManger = ActivityUtil.currentActivity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
+ inputManger.hideSoftInputFromWindow(ActivityUtil.currentActivity?.window?.decorView?.windowToken, 0)
}
/**
diff --git a/yutilskt/src/main/java/com/yechaoa/yutilskt/widget/YLoadingDialog.kt b/yutilskt/src/main/java/com/yechaoa/yutilskt/widget/YLoadingDialog.kt
index 52cf4f1..969ed0f 100644
--- a/yutilskt/src/main/java/com/yechaoa/yutilskt/widget/YLoadingDialog.kt
+++ b/yutilskt/src/main/java/com/yechaoa/yutilskt/widget/YLoadingDialog.kt
@@ -13,7 +13,13 @@ import com.yechaoa.yutilskt.R
* GitHub : https://github.com/yechaoa
* CSDN : http://blog.csdn.net/yechaoa
*/
-class YLoadingDialog constructor(context: Context, msg: String, cancelable: Boolean = true, theme: Int = R.style.yLoadingDialog) : Dialog(context, theme) {
+class YLoadingDialog(
+ context: Context,
+ msg: String,
+ cancelable: Boolean = true,
+ theme: Int = R.style.yLoadingDialog,
+ private val cancelListener: (() -> Unit)? = null
+) : Dialog(context, theme) {
init {
setContentView(R.layout.dialog_y_loading)
@@ -25,6 +31,11 @@ class YLoadingDialog constructor(context: Context, msg: String, cancelable: Bool
window?.attributes = lp
val tvTitle = findViewById(R.id.tv_y_loading)
tvTitle?.text = msg
+
+ this.setOnCancelListener {
+ cancelListener?.invoke()
+ }
+
}
}
\ No newline at end of file