Writing in the front
First, this is a Kotlin library, so if your project is written in JAVA, it won’t help you for a while. Second, this is not the official Android-KTX.
As the project grows, we must extract a large number of utility classes and common modules. Java’s utility classes already look simple, but Kotlin’s extensions bring it up to a level of simplicity and ease of use, as well as some AOP functionality. I don’t want to talk about the benefits of Kotlin anymore. I am a Kotlin fan and promoter. All the background, Android and front-end core codes of our company are Kotlin, which greatly improves the development efficiency. If you want to have fun writing code, use Kotlin!
The rapid early adopters
This library encapsulates common usage scenarios, including Http, and aims to replace all utility classes in your project. Let’s take a look at some examples:
- Click the View
view.click {
toast("do something")}Copy the code
In addition to the concise writing, it also realizes the event throttling. Limit users to one click within 350ms. You’ll probably have situations where users click buttons quickly to start multiple activities.
- Hash of a string
"123456".md5()
"123456".sha1()
"123456".sha1hmac (salt) // Random number enhanced hash //...Copy the code
The project often encountered the string hash, class library provides common MD5, SHA1, SHA256, random number increment hash, AES, DES and other algorithms encapsulation.
- Print log
"I am the test.".v()
"I am the test.".i()
"I am the test.".w()
"I am the test.".d()
"I am the test.".e()
Copy the code
- Span related
val str = "I am the test word."tvSizeResult.sizeSpan(str, 0.. 2) tvSizeResult.sizeSpan(str, 0.. 2, scale =.7f) // Change scale to zoom in or out. Scale defaults to 1.5Copy the code
tvColorResult.colorSpan(str,2.. 6)Copy the code
tvStrikethrougthResult.strikeThrougthSpan(str,2.. 6)Copy the code
- ImageView associated
The main package is to load the image:
Imageview.load (URL) ImageView.load (URL, placeholder = R.map.ic_launcher, isCircle =true)
imageView.load(url, placeholder = R.mipmap.ic_launcher, roundRadius = 20)
Copy the code
- OkHttp related
Not satisfied with both OkHttpUtils and OkGo, they built one. Request:
//Get request val user ="http://192.168.1.103:3000/json".http().get<User>() //Post request, passing header and params val User ="http://192.168.1.103:3000/json".http()
.headers("device" to "HuaWeiMate20", ...)
.params("token" to "188sas9cf99a9d"."file"To file, // upload files...) .post<User>()Copy the code
The above example itself needs to be used in coroutines; It is also my favorite and most recommended method. If you don’t use coroutines, you can use callback style:
"http://192.168.1.103:3000/json".http().get(object : HttpCallback<String> {
override fun onSuccess(t: String) {
}
override fun onFail(e: IOException) {
super.onFail(e)
}
})
Copy the code
Built-in simple and practical log printer:
Other Settings:
// Set custom Client okwrapper.setClient (...) // set global header okwrapper.headers ("header1" to "a"."header2" to "b",...). // set the okwrapper.interceptors (...) // Cancel the request"http://192.168.1.103:3000/json".http(tag = "abc").get<String>() // You need to specify the tag okwrapper.cancel ("abc")
Copy the code
The last
The above is only a small part of the class library, see Github README for more details, I hope you can help.
Github address: github.com/li-xiaojun/…
Suggestions and comments are welcome in the Issue to help improve the library. If you like it, you can also give it a Star.