Skip to content

Commit

Permalink
optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
yechao committed Oct 16, 2021
1 parent d251111 commit 6be4b48
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 69 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -54,10 +54,12 @@ LogUtil.setIsLog(true);

* Context getAppContext()<br>获取全局上下文

* void showLoading(Activity activity,String msg)<br>Loading加载框
* void showLoading(Activity activity,String msg)<br>Loading加载框,kotlin版本参数更多

* void hideLoading()<br>hideLoading

* boolean getLoadingDialog()<br>获取dialog

* boolean loadingIsShowing()<br>loading是否显示

* void finishBySleep(final long millis)<br>根据时间休眠然后关闭当前页面,比如:3秒自动返回
Expand Down Expand Up @@ -232,6 +234,9 @@ like this

## 近期更新

#### 3.2.2(2021-10-16)
- optimized code

#### 3.2.1(2021-05-25)
- 优化调整
- 添加Toast扩展
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/yechaoa/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {

repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.2'
Expand All @@ -19,7 +19,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

Expand Down
8 changes: 4 additions & 4 deletions yutilskt/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 30

Expand All @@ -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'
}
40 changes: 25 additions & 15 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/ParseUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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()
}
Expand All @@ -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()
}
Expand All @@ -68,24 +74,28 @@ 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
}

/**
* 根据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
}
Expand Down
4 changes: 2 additions & 2 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/ShareUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"
Expand All @@ -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"
Expand Down
30 changes: 15 additions & 15 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/SpUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>?) {
fun setStringSet(key: String, value: Set<String>?) {
sp.edit().putStringSet(key, value).apply()
}

fun getStringSet(key: String?): Set<String> {
fun getStringSet(key: String): Set<String> {
return HashSet<String>(sp.getStringSet(key, HashSet<String>()))
}

/**
* 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()
}

Expand Down
4 changes: 2 additions & 2 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/TimeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ 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 {
date = sdf.parse(mDate)
} catch (e: ParseException) {
e.printStackTrace()
}
return sdf.format(date)
return if (null != date) sdf.format(date) else ""
}
}
18 changes: 9 additions & 9 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/ToastUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/**
Expand All @@ -62,15 +62,15 @@ object ToastUtil {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg)
} else {
ActivityUtil.currentActivity!!.runOnUiThread { createCenterToast(msg) }
ActivityUtil.currentActivity?.runOnUiThread { createCenterToast(msg) }
}
}

fun showCenter(msg: String) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg)
} else {
ActivityUtil.currentActivity!!.runOnUiThread { createCenterToast(msg) }
ActivityUtil.currentActivity?.runOnUiThread { createCenterToast(msg) }
}
}

Expand All @@ -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()
}

/**
Expand Down
Loading

0 comments on commit 6be4b48

Please sign in to comment.