Skip to content

Commit

Permalink
style(*): change the coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
pokk committed Sep 29, 2020
1 parent 3b92f58 commit c454f32
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 103 deletions.
1 change: 0 additions & 1 deletion kotlinknifer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ dependencies {
implementation "com.google.code.gson:gson:2.8.6"
// 'implementation' can't import to the new project.
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutine_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$kotlin_coroutine_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutine_version"
implementation "androidx.appcompat:appcompat:1.2.0"
implementation "androidx.core:core-ktx:1.3.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlin.math.pow
fun View.registerCircularRevealAnimation(
revealSettings: RevealAnimationSetting,
startColor: Int,
endColor: Int
endColor: Int,
) {
val duration = 1000L
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Expand All @@ -29,7 +29,7 @@ fun View.registerCircularRevealAnimation(
oldLeft: Int,
oldTop: Int,
oldRight: Int,
oldBottom: Int
oldBottom: Int,
) {
v.removeOnLayoutChangeListener(this)
val (cx, cy, width, height) = revealSettings
Expand All @@ -50,7 +50,7 @@ fun View.startCircularExitAnimation(
revealSettings: RevealAnimationSetting,
startColor: Int,
endColor: Int,
listener: () -> Unit
listener: () -> Unit,
) {
val duration = 1000L
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Expand Down Expand Up @@ -78,7 +78,7 @@ fun View.startCircularExitAnimation(
fun View.startColorAnimation(
startColor: Int,
endColor: Int,
duration: Long
duration: Long,
) {
ValueAnimator().apply {
this.duration = duration
Expand All @@ -92,7 +92,7 @@ data class RevealAnimationSetting(
var centerX: Int,
var centerY: Int,
var width: Int,
var height: Int
var height: Int,
) : Parcelable {
companion object CREATOR : Parcelable.Creator<RevealAnimationSetting> {
override fun createFromParcel(parcel: Parcel) = RevealAnimationSetting(parcel)
Expand Down
18 changes: 11 additions & 7 deletions kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Bitmap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import androidx.palette.graphics.Palette
import java.io.ByteArrayOutputStream

fun Resources.createBitmap(
drawableResId: Int, opts: BitmapFactory.Options? = null,
rect: Rect? = null
drawableResId: Int,
opts: BitmapFactory.Options? = null,
rect: Rect? = null,
): Bitmap? {
var bitmap: Bitmap? = null
openRawResource(drawableResId).use {
Expand Down Expand Up @@ -67,7 +68,7 @@ fun Bitmap.scale(ratio: Float) = scale(ratio, ratio)
fun Resources.createCompressedBitmap(
@DrawableRes drawableResId: Int,
simpleSize: Int = 1,
bitmapConf: Bitmap.Config? = null
bitmapConf: Bitmap.Config? = null,
): Bitmap {
val opts = BitmapFactory.Options().apply {
inJustDecodeBounds = false
Expand All @@ -89,7 +90,7 @@ fun Resources.createScaledBitmap(@DrawableRes drawableResId: Int, ratio: Float)
fun Resources.createRegionBitmap(
drawableResId: Int,
rect: Rect,
opts: BitmapFactory.Options = BitmapFactory.Options()
opts: BitmapFactory.Options = BitmapFactory.Options(),
) = openRawResource(drawableResId).use {
// Create a region decoder.
val decoder = BitmapRegionDecoder.newInstance(it, false)
Expand All @@ -111,15 +112,18 @@ fun Bitmap.toBytes(format: Bitmap.CompressFormat = Bitmap.CompressFormat.PNG, qu
fun Bitmap.resizeImageAsRatio(aspectRatio: Double): Bitmap = also {
val ratio: Double = it.width.toDouble() / it.height.toDouble()

if (ratio > aspectRatio)
if (ratio > aspectRatio) {
it.width = (aspectRatio * it.height).toInt()
else
}
else {
it.height = (it.width / aspectRatio).toInt()
}
}

fun Bitmap?.safeRecycle() {
if (this != null && !isRecycled)
if (this != null && !isRecycled) {
recycle()
}
}

fun Bitmap.decorateGradientMask(shaderDst: Shader): Bitmap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import kotlin.reflect.KProperty
*/
class SoftRef<T>(
default: T? = null,
private val queue: ReferenceQueue<T>? = null
private val queue: ReferenceQueue<T>? = null,
) : ReadWriteProperty<Any?, T?> {
private var variable: SoftReference<T>?

Expand Down Expand Up @@ -50,7 +50,7 @@ class SoftRef<T>(
*/
class WeakRef<T>(
default: T? = null,
private val queue: ReferenceQueue<T>? = null
private val queue: ReferenceQueue<T>? = null,
) : ReadWriteProperty<Any?, T?> {
private var variable: WeakReference<T>?

Expand Down Expand Up @@ -80,7 +80,7 @@ class WeakRef<T>(
*/
class PhantomRef<T>(
default: T? = null,
private val queue: ReferenceQueue<T>
private val queue: ReferenceQueue<T>,
) : ReadWriteProperty<Any?, T?> {
private var variable: PhantomReference<T>?

Expand Down
24 changes: 12 additions & 12 deletions kotlinknifer/src/main/java/com/devrapid/kotlinknifer/EditText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ fun setCursorPointerColor(view: EditText, @ColorInt color: Int, @DrawableRes dra
// Get the editor.
val editor = TextView::class.java.getDeclaredField("mEditor").accessible().get(view)
// Get all drawable's selection points.
val drawables = if (-1 == drawable)
listOf("mTextSelectHandleRes" // Selection cursor point.
, "mTextSelectHandleLeftRes" // Selection the highlight left cursor point.
, "mTextSelectHandleRightRes") // Selection the highlight right cursor point.
val drawables = if (-1 == drawable) {
listOf("mTextSelectHandleRes", // Selection cursor point.
"mTextSelectHandleLeftRes", // Selection the highlight left cursor point.
"mTextSelectHandleRightRes") // Selection the highlight right cursor point.
.asSequence()
.map { TextView::class.java.getDeclaredField(it).accessible() } // Get the drawables' field.
.map { it.getInt(view) } // Get the drawable resource id.
.map { requireNotNull(ContextCompat.getDrawable(view.context, it)) } // Get the drawable.
.map { it.setColorFilter(color, SRC_IN); it } // Change the color we set.
.map { TextView::class.java.getDeclaredField(it).accessible() } // Get the drawables' field.
.map { it.getInt(view) } // Get the drawable resource id.
.map { requireNotNull(ContextCompat.getDrawable(view.context, it)) } // Get the drawable.
.map { it.setColorFilter(color, SRC_IN); it } // Change the color we set.
.toList()
else
}
else {
(0..2).map { requireNotNull(ContextCompat.getDrawable(view.context, drawable)) }
}
// Get all handle fields.
val fields = listOf("mSelectHandleCenter"
, "mSelectHandleLeft"
, "mSelectHandleRight")
val fields = listOf("mSelectHandleCenter", "mSelectHandleLeft", "mSelectHandleRight")
.map { editor.javaClass.getDeclaredField(it).accessible() }
fields.zip(drawables).forEach { (field, drawable) -> field.set(editor, drawable) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fun FragmentManager.addFragment(
needBack: Boolean = false,
fragmentStack: Stack<Fragment>? = null,
sharedElements: HashMap<View, String> = hashMapOf(),
block: ((FragmentTransaction) -> Unit)? = null
block: ((FragmentTransaction) -> Unit)? = null,
) = transaction {
block?.invoke(this)
sharedElements.forEach { value -> addSharedElement(value.key, value.value) }
Expand Down
10 changes: 0 additions & 10 deletions kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Keyboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,3 @@ fun isShowingSoftKeyboard(rootView: View): Boolean {
inline fun Context.toggleSoftKeyboard() =
(getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)

// FIXME(jieyi): 2018/01/23 Those functions don't work well.
//inline fun Context.hideSoftKeyboard() = if (isShowingSoftKeyboard()) toggleSoftKeyboard() else Unit
//
//inline fun Context.showSoftKeyboard() = if (isHidingSoftKeyboard()) toggleSoftKeyboard() else Unit
//
//inline fun Context.isHidingSoftKeyboard() = !isShowingSoftKeyboard()
//
//inline fun Context.isShowingSoftKeyboard() =
// (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).isAcceptingText
25 changes: 17 additions & 8 deletions kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Logs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ fun logi(vararg msg: Any?) = Logs.i(*msg)
fun logw(vararg msg: Any?) = Logs.w(*msg)
fun loge(vararg msg: Any?) = Logs.e(*msg)

fun logv(msg: Any?) = Logs.v(msg)
fun logd(msg: Any?) = Logs.d(msg)
fun logi(msg: Any?) = Logs.i(msg)
fun logw(msg: Any?) = Logs.w(msg)
fun loge(msg: Any?) = Logs.e(msg)

internal object Logs {
var _IS_DEBUG = true // Debug mode's switch, default is turn off.
var TAG = "MY_LOG" // TAG
var _IS_DEBUG = true // Debug mode's switch, default is turn off.
var TAG = "MY_LOG" // TAG
private const val COLON = ":"
private const val LEFT_PARENTHESIS = "("
private const val RIGHT_PARENTHESIS = ")"
private const val SPACE_STRING = " "
private const val METHOD_INDEX = 4
private val lockLog = Any() // Avoid the threading's race condition.
private val strBuilder = StringBuilder() // String builder.
private val lockLog = Any() // Avoid the threading's race condition.
private val strBuilder = StringBuilder() // String builder.

/**
* VERBOSE log.
Expand Down Expand Up @@ -53,10 +59,12 @@ internal object Logs {
* @param msg output message
*/
internal fun e(vararg msg: Any?) {
if (1 == msg.size && msg[0] is Exception)
if (1 == msg.size && msg[0] is Exception) {
LogWrapper().debugCheck(Log::class.java, getExceptionMsg(msg[0] as Exception))
else
}
else {
showLog(*msg)
}
}

/**
Expand All @@ -73,11 +81,12 @@ internal object Logs {
fun debugCheck(cls: Class<*>, msg: Any): Boolean {
// Checking the debug mode.
if (_IS_DEBUG) {
// Because the level of the function depth, the index is 4.
// Because the level of the function depth, the index is 4.
var methodName = Thread.currentThread().stackTrace[METHOD_INDEX].methodName.substringBefore("$")
// Only exception msg only is 3.
if (1 < methodName.length)
if (1 < methodName.length) {
methodName = Thread.currentThread().stackTrace[METHOD_INDEX - 1].methodName.substringBefore("$")
}
// Avoid the race condition.
synchronized(lockLog) {
return logMsg(cls, methodName, msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ fun hasNetwork(context: Context): Boolean {
var isConnected = false // Initial Value
val connectivityManager = cast<ConnectivityManager>(context.getSystemService(CONNECTIVITY_SERVICE))
val activeNetwork = connectivityManager.activeNetworkInfo
if (activeNetwork != null && activeNetwork.isConnected)
if (activeNetwork != null && activeNetwork.isConnected) {
isConnected = true
}
return isConnected
}
Loading

0 comments on commit c454f32

Please sign in to comment.