Skip to content

Commit

Permalink
Log 打印异常,反射调用方法
Browse files Browse the repository at this point in the history
增加常用的两种Dialog使用,简化代码
保留小数与转换DP
Toast
  • Loading branch information
luohaolun committed Jul 1, 2019
1 parent 6665ef8 commit 4c692fe
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 60 deletions.
144 changes: 93 additions & 51 deletions app/src/main/java/lhl/kotlinextends/Log.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,60 @@ import android.util.Log
import java.lang.Exception

const val ClassName = "lhl.kotlinextends.LogKt"
private var MaxLength = 3900
private var maxLength = 3900

fun setLogMaxLength(int: Int) {
MaxLength = int
maxLength = int
}

fun Any.e(maxLen: Int = MaxLength) {
fun Any.e() {
print("e", this, maxLength)
}

fun Any.e(maxLen: Int = maxLength) {
print("e", this, maxLen)
}

fun Any.d(maxLen: Int = MaxLength) {
fun Any.d(maxLen: Int = maxLength) {
print("d", this, maxLen)
}

fun Any.i(maxLen: Int = MaxLength) {
fun Any.d() {
print("d", this, maxLength)
}

fun Any.i(maxLen: Int = maxLength) {
print("i", this, maxLen)
}

fun Any.v(maxLen: Int = MaxLength) {
fun Any.i() {
print("i", this, maxLength)
}

fun Any.v(maxLen: Int = maxLength) {
print("v", this, maxLen)
}

fun Any.w(maxLen: Int = MaxLength) {
fun Any.v() {
print("v", this, maxLength)
}

fun Any.w(maxLen: Int = maxLength) {
print("w", this, maxLen)
}

fun Any.wtf(maxLen: Int = MaxLength) {
fun Any.w() {
print("w", this, maxLength)
}

fun Any.wtf(maxLen: Int = maxLength) {
print("wtf", this, maxLen)
}

fun Any.wtf() {
print("wtf", this, maxLength)
}


private fun print(logLevel: String, log: Any, maxLen: Int) {
var tag = Thread.currentThread().name + try {
Expand All @@ -43,51 +67,69 @@ private fun print(logLevel: String, log: Any, maxLen: Int) {
} catch (e: Exception) {
"TAG"
}
val str = log.toString()

val sliceCount = str.length / maxLen + if ((str.length % maxLen) > 0) 1 else 0
when (logLevel) {
"e" -> {
for (i in 0 until sliceCount) {
val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
Log.e(tag, str.substring(i * maxLen, space))
}
}
"v" -> {
for (i in 0 until sliceCount) {
val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
Log.v(tag, str.substring(i * maxLen, space))
}
}
"d" -> {
for (i in 0 until sliceCount) {
val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
Log.d(tag, str.substring(i * maxLen, space))
}
}
"i" -> {
for (i in 0 until sliceCount) {
val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
Log.i(tag, str.substring(i * maxLen, space))
}
}
"w" -> {
for (i in 0 until sliceCount) {
val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
Log.w(tag, str.substring(i * maxLen, space))
}
}
"wtf" -> {
for (i in 0 until sliceCount) {
val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
Log.wtf(tag, str.substring(i * maxLen, space))
}
}
else -> {
for (i in 0 until sliceCount) {
val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
Log.e(tag, str.substring(i * maxLen, space))
val str = when (log) {
is Throwable -> {
var strBuilder = StringBuilder()
strBuilder.append(log.toString())
log.stackTrace.forEach {
strBuilder.append("\n - ${it.className}.${it.methodName}(${it.fileName}:${it.lineNumber})")
}
strBuilder.toString()
}
else -> log.toString()
}

val sliceCount = str.length / maxLen + if ((str.length % maxLen) > 0) 1 else 0

val method = Log::class.java.getDeclaredMethod(logLevel, String::class.java, String::class.java)
for (i in 0 until sliceCount) {
val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
method.invoke(null, tag, str.substring(i * maxLen, space))
}

// when (logLevel) {
// "e" -> {
// for (i in 0 until sliceCount) {
// val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
// Log.e(tag, str.substring(i * maxLen, space))
// }
// }
// "v" -> {
// for (i in 0 until sliceCount) {
// val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
// Log.v(tag, str.substring(i * maxLen, space))
// }
// }
// "d" -> {
// for (i in 0 until sliceCount) {
// val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
// Log.d(tag, str.substring(i * maxLen, space))
// }
// }
// "i" -> {
// for (i in 0 until sliceCount) {
// val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
// Log.i(tag, str.substring(i * maxLen, space))
// }
// }
// "w" -> {
// for (i in 0 until sliceCount) {
// val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
// Log.w(tag, str.substring(i * maxLen, space))
// }
// }
// "wtf" -> {
// for (i in 0 until sliceCount) {
// val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
// Log.wtf(tag, str.substring(i * maxLen, space))
// }
// }
// else -> {
// for (i in 0 until sliceCount) {
// val space = if (i == sliceCount - 1) str.length else (i * maxLen + maxLen)
// Log.e(tag, str.substring(i * maxLen, space))
// }
// }
// }
}
23 changes: 23 additions & 0 deletions app/src/main/java/lhl/kotlinextends/Number.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package lhl.kotlinextends

import com.blankj.utilcode.util.ConvertUtils

private var defaultNumber = 2
fun setDefaultNumber(num: Int) {
defaultNumber = num
}

/**
* 保留defaultNumber位小数
*/
fun Double.kp(num: Int = defaultNumber): String {
return String.format("%.${num}f", this)
}

fun Float.kp(num: Int = defaultNumber): String {
return String.format("%.${num}f", this)
}

fun Int.dp(): Int = this.toFloat().dp()
fun Double.dp(): Int = this.toFloat().dp()
fun Float.dp(): Int = ConvertUtils.px2dp(this)
46 changes: 46 additions & 0 deletions app/src/main/java/lhl/kotlinextends/PDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package lhl.kotlinextends

import android.app.Activity
import android.app.ProgressDialog

class PDialog(activity: Activity, message: String, cancel: Int, dismissListener: DialogListener? = null) {

constructor(activity: Activity, message: String) : this(activity, message, CANCEL_NO)

constructor(activity: Activity, message: String, dismissListener: DialogListener) : this(activity, message, CANCEL_BACK, dismissListener)

private var progressDialog = ProgressDialog(activity)

init {
progressDialog.setMessage(message)
when (cancel) {
CANCEL_ALL -> {
progressDialog.setCancelable(true)
progressDialog.setCanceledOnTouchOutside(true)
}
CANCEL_BACK -> {
progressDialog.setCancelable(true)
progressDialog.setCanceledOnTouchOutside(false)
}
CANCEL_NO -> {
progressDialog.setCancelable(false)
progressDialog.setCanceledOnTouchOutside(false)
}
}
progressDialog.setOnDismissListener { dismissListener?.invoke() }
}


fun setMessage(text: String) {
progressDialog.setMessage(text)
}

fun show() {
progressDialog.show()
}

fun dismiss() {
progressDialog.dismiss()
}

}
67 changes: 67 additions & 0 deletions app/src/main/java/lhl/kotlinextends/TDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package lhl.kotlinextends

import android.app.Activity
import android.app.AlertDialog
import android.app.Dialog

typealias DialogListener = () -> Unit

const val CANCEL_ALL = 0
const val CANCEL_BACK = 1
const val CANCEL_NO = 2

class TDialog(activity: Activity, message: String = "", private val cancel: Int = CANCEL_ALL, positive: DialogListener? = null) {

private var builder: AlertDialog.Builder = AlertDialog.Builder(activity)
private var dialog: Dialog? = null

init {
builder.setTitle("提示")
builder.setMessage(message)
builder.setPositiveButton("确定", null)
if (cancel == CANCEL_NO)
builder.setCancelable(false)
positive?.let { setPositiveButton(listener = it) }
}

fun show() {
dialog = builder.create()
if (cancel != CANCEL_ALL)
dialog?.setCanceledOnTouchOutside(false)
dialog?.show()
}

fun dismiss() {
dialog?.dismiss()
}

fun setPositiveButton(text: String = "确定", listener: DialogListener? = null) {
builder.setPositiveButton(text) { _, _ -> listener?.invoke() }
show()
}

fun setNegativeButton(text: String = "取消", listener: DialogListener? = null) {
builder.setNegativeButton(text) { _, _ -> listener?.invoke() }
show()
}

fun setButtons(textPositive: String = "确定", textNegative: String = "取消", positive: DialogListener? = null, negative: DialogListener? = null) {
builder.setPositiveButton(textPositive) { _, _ -> positive?.invoke() }
builder.setNegativeButton(textNegative) { _, _ -> negative?.invoke() }
show()
}

fun setNeutralButton(text: String, listener: DialogListener? = null) {
builder.setNeutralButton(text) { _, _ -> listener?.invoke() }
}

fun setTitle(text: String) {
builder.setTitle(text)
}

fun setMessage(text: String) {
builder.setMessage(text)
}


}
7 changes: 5 additions & 2 deletions app/src/main/java/lhl/kotlinextends/Toast.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package lhl.kotlinextends

import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.Utils

fun String.toast(){
fun String.toast() {
ToastUtils.showShort(this)
}

fun String.longToast() {
ToastUtils.showLong(this)
}
3 changes: 3 additions & 0 deletions test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// implementation 'com.blankj:utilcode:1.23.7'
implementation project(':app')
// RxJava
implementation 'io.reactivex.rxjava2:rxjava:2.2.4'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
}
Loading

0 comments on commit 4c692fe

Please sign in to comment.