Skip to content

Commit

Permalink
[ADD] fragment update view
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Arencibia Herrera committed Jan 22, 2019
1 parent fc3a6ca commit 064669d
Show file tree
Hide file tree
Showing 21 changed files with 596 additions and 26 deletions.
46 changes: 45 additions & 1 deletion apklisupdate/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28



defaultConfig {
minSdkVersion 9
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName "1.0"
Expand All @@ -20,17 +24,49 @@ android {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}


dev {


}

debug {

}
}

sourceSets {
test.java.srcDirs += 'src/test/kotlin'
}

}

afterEvaluate {
android.sourceSets.all { sourceSet ->
if (!sourceSet.name.startsWith('test') || !sourceSet.name.startsWith('androidTest')) {
sourceSet.kotlin.setSrcDirs([])
}
}
}


dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
testImplementation 'org.mockito:mockito-core:2.4.5'


implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

testImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testImplementation "com.nhaarman:mockito-kotlin:1.1.0"
testImplementation 'org.amshove.kluent:kluent:1.14'

/**Retrofit**/
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
Expand All @@ -42,4 +78,12 @@ dependencies {
implementation 'io.reactivex.rxjava2:rxkotlin:2.3.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'

implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.palette:palette:1.0.0'

//html
implementation 'org.sufficientlysecure:html-textview:3.6'

implementation 'com.squareup.picasso:picasso:2.71828'
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cu.uci.apklisupdate;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down
39 changes: 39 additions & 0 deletions apklisupdate/src/main/java/cu/uci/apklisupdate/ApklisUpdate.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cu.uci.apklisupdate

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import io.reactivex.schedulers.Schedulers


object ApklisUpdate {

@SuppressLint("CheckResult")
fun hasAppUpdate(context: Context, callback: UpdateCallback) {

LastReleaseClient.instance()
.lastRelease(context.packageName)
.subscribeOn(Schedulers.newThread())
.subscribe({

val manager = context.packageManager
val info = manager.getPackageInfo(
context.packageName, 0
)

val versionCode: Long = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
info.longVersionCode
} else {
info.versionCode.toLong()
}
if (versionCode < it.last_release.version_code && info.versionName != it.last_release.version_name)
callback.onNewUpdate(it)
else
callback.onOldUpdate(it)
}, {
it.printStackTrace()
callback.onError(it)
})

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cu.uci.apklisupdate

import cu.todus.android.rest.model.response.LastApplicationRelease
import cu.uci.apklisupdate.model.AppUpdateInfo
import io.reactivex.Single
import retrofit2.http.GET
import retrofit2.http.Path
Expand All @@ -10,7 +10,7 @@ import retrofit2.http.Path
* Email: adrian011494@gmail.com
*/
interface LastReleaseApi {
@GET("https://www.apklis.cu/api/v1/application/{package}/")
fun lastRelease(@Path("package") appPackage: String): Single<LastApplicationRelease>
@GET("api/v1/application/{package}/")
fun lastRelease(@Path("package") appPackage: String): Single<AppUpdateInfo>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cu.uci.apklisupdate

import cu.uci.apklisupdate.base.RestClient
import cu.uci.apklisupdate.model.AppUpdateInfo
import io.reactivex.Single

/**
* Created by Adrian Arencibia Herrera on 5/29/18.
* Email: adrian011494@gmail.com
*/

class LastReleaseClient : RestClient<LastReleaseApi>(LastReleaseApi::class.java) {

fun lastRelease(appPackage: String): Single<AppUpdateInfo> {
return mApi.lastRelease(appPackage)
}

companion object {
fun instance() = LastReleaseClient()
}
}
10 changes: 10 additions & 0 deletions apklisupdate/src/main/java/cu/uci/apklisupdate/UpdateCallback.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cu.uci.apklisupdate

import cu.uci.apklisupdate.model.AppUpdateInfo
import java.lang.Exception

interface UpdateCallback {
fun onNewUpdate(appUpdateInfo: AppUpdateInfo)
fun onOldUpdate(appUpdateInfo: AppUpdateInfo)
fun onError(e: Throwable)
}
64 changes: 64 additions & 0 deletions apklisupdate/src/main/java/cu/uci/apklisupdate/base/RestClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cu.uci.apklisupdate.base

import cu.uci.apklisupdate.BuildConfig
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

abstract class RestClient<T>(
type: Class<T>
) {


companion object {

val LOG_TAG: String = this::class.java.simpleName


private const val CONNECTION_TIMEOUT_MS = 30000L
private const val READ_TIMEOUT_MS = 30000L
private const val WRITE_TIMEOUT_MS = 30000L
}

var mApi: T

// http client
private var mOkHttpClient: OkHttpClient

private val BASE_URL: String = "https://www.apklis.cu/"

init {


val userAgentInterceptor = Interceptor { chain ->
val original = chain.request()
val requestBuilder = original.newBuilder()
.header("User-Agent", BuildConfig.APPLICATION_ID)
val request = requestBuilder.build()
return@Interceptor chain.proceed(request)
}

mOkHttpClient = OkHttpClient().newBuilder()
.addInterceptor(userAgentInterceptor)
.connectTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.readTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.writeTimeout(WRITE_TIMEOUT_MS, TimeUnit.MILLISECONDS)

.build()

val retrofit = Retrofit.Builder()
.client(mOkHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)

.build()

mApi = retrofit.create(type)
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cu.uci.apklisupdate.view;

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.squareup.picasso.Picasso
import cu.uci.apklisupdate.R
import cu.uci.apklisupdate.model.AppUpdateInfo
import kotlinx.android.synthetic.main.apklis_fragment_update.*

class ApklisUpdateFragment : Fragment() {

fun layout(): Int = R.layout.apklis_fragment_update

lateinit var updateInfo: AppUpdateInfo

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(layout(), container, false)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

changelog.setHtml("${context?.getString(R.string.changelog)}\n${updateInfo.last_release.changelog}")
version.text = updateInfo.last_release.version_name
title.text = updateInfo.name
fromApklis.setOnClickListener {
val i = Intent(Intent.ACTION_VIEW)
i.data = Uri.parse("https://www.apklis.cu/es/application/${updateInfo.package_name}/latest")
requireContext().startActivity(Intent.createChooser(i, ""))
}

download.setOnClickListener {
val i = Intent(Intent.ACTION_VIEW)
i.data = Uri.parse(updateInfo.last_release.apk_file)
requireContext().startActivity(Intent.createChooser(i, ""))
}

Picasso.get().load(updateInfo.last_release.icon).into(logo)

getView()?.setBackgroundDrawable(background)


fromApklis.setTextColor(actionsColor)
download.setTextColor(actionsColor)
}


private var background: Drawable = ColorDrawable(Color.WHITE)

private var actionsColor: Int = 0

companion object {

fun newInstance(updateInfo: AppUpdateInfo, background: Drawable = ColorDrawable(Color.WHITE), actionsColor: Int = Color.BLACK): ApklisUpdateFragment {
return ApklisUpdateFragment().apply {
this.updateInfo = updateInfo
this.background = background
this.actionsColor = actionsColor
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cu.uci.apklisupdate.view;

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.fragment.app.Fragment
import cu.uci.apklisupdate.R
import cu.uci.apklisupdate.model.AppUpdateInfo
import kotlinx.android.synthetic.main.fragment_update.*

class UpdateFragment : Fragment() {

fun layout(): Int = R.layout.fragment_update

lateinit var updateInfo: AppUpdateInfo

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(layout(), container, false)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

changelog.setHtml("${context?.getString(R.string.changelog)}\n${updateInfo.last_release.changelog}")
version.text = updateInfo.last_release.changelog

fromApklis.setOnClickListener {
val i = Intent(Intent.ACTION_VIEW)
i.data = Uri.parse("https://www.apklis.cu/es/application/${updateInfo.package_name}/latest")
requireContext().startActivity(Intent.createChooser(i, ""))
}

download.setOnClickListener {
val i = Intent(Intent.ACTION_VIEW)
i.data = Uri.parse(updateInfo.last_release.apk_file)
requireContext().startActivity(Intent.createChooser(i, ""))
}
}


private var background: Int = 0

private var actionsColor: Int = 0

companion object {

fun newInstance(updateInfo: AppUpdateInfo, @DrawableRes background: Int = android.R.color.white, @ColorRes actionsColor: Int = android.R.color.black): UpdateFragment {
return UpdateFragment().apply {
this.updateInfo = updateInfo
this.background = background
this.actionsColor = actionsColor
}
}
}

}
Loading

0 comments on commit 064669d

Please sign in to comment.