GithubHelp home page GithubHelp logo

compareversion's Introduction

CompareVersion

This is a class about compare apk's version

 /**
     *
     * 0:数据异常,未完成比较
     * 1:需要
     * -1:不需要
     */
    fun compareVersion(currentVersion: String, remoteVersion: String): Int {
        //Step 1:判空
        if (TextUtils.isEmpty(currentVersion) || TextUtils.isEmpty(remoteVersion)) {
            return 0
        }
        //Step 2:判断是否相等
        if (currentVersion == remoteVersion) {
            return 0
        }
        //Step 3:拆分版本名
        val oldVersionArray = currentVersion.split(".")
        val remoteVersionArray = remoteVersion.split(".")
        //Step 4:获取最小长度值
        val minLen = oldVersionArray.size.coerceAtMost(remoteVersionArray.size)
        var diff = 0
        var index = 0
        //Step 5:循环进行单个字符串比较
        while (index < minLen
            && (oldVersionArray[index].toInt() - remoteVersionArray[index].toInt())
                .also { diff = it } == 0
        ) {
            index++
        }
        //Step 6:如果并没有比出大小,再比较多余的位数
        if (diff == 0) {
            for (i in index until oldVersionArray.size) {
                if (oldVersionArray[i].toInt() > 0) {
                    return -1
                }
            }
            for (i in index until remoteVersionArray.size) {
                if (remoteVersionArray[i].toInt() > 0) {
                    return 1
                }
            }
        }
        return if (diff > 0) -1 else 1
    }
    

compareversion's People

Contributors

catherinelhl avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.