A WanAndroid and open eye a pot of client

  • The entire client is written in Kotlin
  • Network requests use retroFIT2 + OKHttp3 + RxJavA2 MVP architecture
  • Images are loaded using Glide4
  • Fragmentation is operated using Fragmentation
  • Web page content is displayed using AgentWeb
  • Ijkplayer + GSYVideoPlayer is used for video playback

Pull up to load more

SwipeRefreshLayout provides pull-down refresh support but does not provide pull-up loading. There are many frameworks on the web that do this, but some of them are not necessarily needed here

refresh.setOnChildScrollUpCallback { parent, child -> child? .let {if(! child.canScrollVertically(1)){ get(page)return@setOnChildScrollUpCallback true
        }
        return@setOnChildScrollUpCallback false
    }
    return@setOnChildScrollUpCallback false
}
Copy the code

With SwipeRefreshLayout. SetOnChildScrollUpCallback () method to set up sliding when listening in to call the canScrollVertically () to determine the child View whether can slip up, If you can’t call interface to request more data, this method when the parameter is greater than 0 judgment up when less than 0 to determine down, also similar to the canScrollHorizontally () is used to judge about the scroll.

Dynamic URL and redirection interception

Sometimes the request we send can not be determined in advance, for example, the article list only gives a small amount of basic information of the article and a web page link, if we want to click to open the web page, we need to realize it by ourselves, just open a web page is ok, let the WebView load. However, the URL of the video in the video list cannot be handled in this way, because these links are automatically redirected requests and the player cannot handle them directly, so we have to use retrofit2 to intercept the redirection again and fetch the redirected location and hand it to the player to play:

    @GET
    fun getVideoPath(@Url url: String): Observable<RealUrl>
Copy the code

You can request any link by passing in the Url as an argument to the method in the interface

 val httpClient = OkHttpClient.Builder()
                .addNetworkInterceptor(interceptor)
                .addInterceptor(Interceptor{
                    val req: Request = it.request()
                    var resp:Response = it.proceed(req)
                    if(resp.code() == HTTP_MOVED_TEMP){
                        resp.header("location")
                        val json = "{\"url\":\"" + resp.header("location") + "\"}"
                        val buffer:Buffer = Buffer().writeString(json, UTF_8)
                        resp = resp.newBuilder().code(HTTP_OK)
                                .body(RealResponseBody("application/json",
                                        -1,
                                        buffer))
                                .build()
                    }
                    return@Interceptor resp
                })
                .followRedirects(false)
                .build()
Copy the code

To intercept redirects and get new locations, I built okHTTP to disallow redirect followRedirects(false) and added interceptors. Get the location in the header, wrap it in JSON, and rebuild the response if the response code is 302, which is the constant HTTP_MOVED_TEMP.

conclusion

I have written an APP with the most basic functions, and all functions tend to be realized in the lightest way possible. In the final analysis, it is lazy. The = kotlin-Android-Extensions dispenses with a lot of findViewById() data so that the Bean doesn’t need to write a lot of getters/setters. In short, the people who invented the language were absolutely 2 ̀_ · ́ mixes

Making github.com/woodygithub…