Skip to content

Commit 3db8da3

Browse files
committed
Adaptive Formatter logic
1 parent 31b4bb2 commit 3db8da3

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

app/build.gradle

-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,4 @@ dependencies {
3838
} else {
3939
implementation 'com.github.yjfnypeu:UsefulCodes:0.0.1'
4040
}
41-
42-
implementation 'com.orhanobut:logger:2.2.0'
4341
}

app/src/main/java/com/haoge/sample/easyandroid/App.kt

-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ package com.haoge.sample.easyandroid
22

33
import android.app.Application
44
import com.haoge.easyandroid.EasyAndroid
5-
import com.orhanobut.logger.AndroidLogAdapter
6-
import com.orhanobut.logger.Logger
7-
85

96
class App : Application() {
107

118
override fun onCreate() {
129
super.onCreate()
1310
EasyAndroid.init(this)
14-
Logger.addLogAdapter(AndroidLogAdapter())
1511
}
1612
}

docs/APIs.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@
1313

1414
- ActivityStack.top()
1515
> 获取顶层Activity提供使用
16-
16+
- SingleCache.mainHandler
17+
> 获取主线程的Handler进行使用
18+
- SingleCache.getApplicationContext()
19+
> 获取进程的Application Context实例

utils/src/main/java/com/haoge/easyandroid/easy/EasyFormatter.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.haoge.easyandroid.easy
22

3-
import android.text.TextUtils
43
import android.util.Log
54
import com.haoge.easyandroid.tools.CommonUtil
65
import org.json.JSONArray
@@ -63,7 +62,7 @@ class EasyFormatter private constructor(private val builder: Builder) {
6362
if (index != length - 1) {
6463
sub.append(",")
6564
}
66-
appendSubString(result, sub)
65+
appendSubString(result, sub, isFlat)
6766
}
6867
if (!isFlat) {
6968
result.append("\n")
@@ -96,7 +95,7 @@ class EasyFormatter private constructor(private val builder: Builder) {
9695
if (hasNext) {
9796
sub.append(", ")
9897
}
99-
appendSubString(result, sub)
98+
appendSubString(result, sub, isFlat)
10099
}
101100
if (!isFlat) {
102101
result.append("\n")
@@ -197,18 +196,20 @@ class EasyFormatter private constructor(private val builder: Builder) {
197196
if (hasNext) {
198197
sub.append(", ")
199198
}
200-
appendSubString(container, sub)
199+
appendSubString(container, sub, isFlat)
201200
}
202201
if (!isFlat) {
203202
container.append("\n")
204203
}
205204
}
206205

207-
private fun appendSubString(container:StringBuilder, subString:StringBuilder) {
206+
private fun appendSubString(container: StringBuilder, subString: StringBuilder, isFlat: Boolean) {
208207
val lines = subString.lines()
209208
for ((index, value) in lines.withIndex()) {
210209
if (index == 0) {
211-
container.append(value).append(if (lines.size > 1) "\n" else "")
210+
if (!isFlat) container.append("\t")
211+
container.append(value)
212+
if (lines.size > 1) container.append("\n")
212213
} else if (value.isNotEmpty()) {
213214
container.append("\t").append(value)
214215
.append(if (index == lines.size - 1) "" else "\n")

utils/src/main/java/com/haoge/easyandroid/tools/ActivityStack.kt

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.app.Activity
44
import android.app.Application
55
import android.content.Context
66
import android.os.Bundle
7+
import com.haoge.easyandroid.cache.SingleCache
78
import java.util.*
89

910
/**
@@ -12,11 +13,19 @@ import java.util.*
1213
*/
1314
object ActivityStack {
1415
private var application: Application? = null
15-
private val stack:LinkedList<Activity> = LinkedList()
16+
val stack:LinkedList<Activity> = LinkedList()
1617

18+
/**
19+
* 获取顶层Activity实例
20+
*/
1721
@Suppress("UNCHECKED_CAST")
1822
fun <T:Activity> top():T? = if (stack.isEmpty()) null else stack.last as T
1923

24+
/**
25+
* 获取一个有效的Context实例。
26+
*/
27+
fun getValidContext():Context = if (stack.isEmpty()) application!! else stack.last
28+
2029
fun registerCallback(context: Context?) {
2130
if (application != null || context == null) {
2231
return

0 commit comments

Comments
 (0)