Skip to content

Commit

Permalink
[ADD] Example usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Arencibia committed Jun 26, 2019
1 parent 9651541 commit 884b7b8
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 100 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
# apklisupdate
# Check app update in [Apklis](https://www.apklis.cu/es/)
[![](https://jitpack.io/v/adrian011494/apklisupdate.svg)](https://jitpack.io/#adrian011494/apklisupdate)

### Installing
* Step 1. Add the JitPack repository to your build file
```groovy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
* Step 2. Add the dependency
```groovy
implementation 'com.github.adrian011494:apklisupdate:$VERSION'
```
### Usage

```kotlin

```
3 changes: 1 addition & 2 deletions apklisupdate/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ dependencies {
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

/** rx java **/
implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.2'
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'
Expand All @@ -91,6 +91,5 @@ dependencies {

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

implementation 'com.squareup.picasso:picasso:2.71828'
}
35 changes: 31 additions & 4 deletions apklisupdate/src/main/java/cu/uci/apklisupdate/ApklisUpdate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import io.reactivex.schedulers.Schedulers
import java.lang.Exception


object ApklisUpdate {
Expand All @@ -26,10 +27,36 @@ object ApklisUpdate {
} 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)

if (it != null) {
if (versionCode < it.last_release.version_code && info.versionName != it.last_release.version_name)
callback.onNewUpdate(it)
else
callback.onOldUpdate(it)
} else
callback.onError(Exception("Not data"))
}, {
it.printStackTrace()
callback.onError(it)
})

}

@SuppressLint("CheckResult")
fun hasAppUpdate(packageName: String, versionCode: Long = 0, callback: UpdateCallback) {

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

if (it != null) {
if (versionCode < it.last_release.version_code)
callback.onNewUpdate(it)
else
callback.onOldUpdate(it)
} else
callback.onError(Exception("Not data"))
}, {
it.printStackTrace()
callback.onError(it)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package cu.uci.apklisupdate

import cu.uci.apklisupdate.model.ApiResponce
import cu.uci.apklisupdate.model.AppUpdateInfo
import io.reactivex.Single
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

/**
* Created by Adrian Arencibia Herrera on 5/29/18.
* Email: adrian011494@gmail.com
*/
interface LastReleaseApi {
@GET("api/v1/application/{package}/")
fun lastRelease(@Path("package") appPackage: String): Single<AppUpdateInfo>
@GET("api/v1/application/")
fun lastRelease(@Query("package_name") appPackage: String): Single<ApiResponce>

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import io.reactivex.Single

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

fun lastRelease(appPackage: String): Single<AppUpdateInfo> {
fun lastRelease(appPackage: String): Single<AppUpdateInfo?> {
return mApi.lastRelease(appPackage)
.map {
it.results.firstOrNull()
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package cu.uci.apklisupdate.model

data class ApiResponce(val count: Int = 0, val results: List<AppUpdateInfo> = listOf())
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ data class LastRelease(
var version_sdk: Int,
var version_sdk_name: String,
var version_target_sdk: Int,
var version_target_sdk_name: Int
var version_target_sdk_name: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorInt
import androidx.core.content.IntentCompat
import androidx.fragment.app.Fragment
import com.squareup.picasso.Picasso
import cu.uci.apklisupdate.R
Expand Down Expand Up @@ -38,13 +40,13 @@ class ApklisUpdateFragment : Fragment() {
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, ""))
requireContext().startActivity(Intent.createChooser(i, getString(R.string.open_web)))
}

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

Picasso.get().load(updateInfo.last_release.icon).into(logo)
Expand All @@ -66,7 +68,7 @@ class ApklisUpdateFragment : Fragment() {
fun newInstance(
updateInfo: AppUpdateInfo,
background: Drawable = ColorDrawable(Color.WHITE),
actionsColor: Int = Color.BLACK
@ColorInt actionsColor: Int = Color.BLACK
): ApklisUpdateFragment {
return ApklisUpdateFragment().apply {
this.updateInfo = updateInfo
Expand Down
119 changes: 60 additions & 59 deletions apklisupdate/src/main/res/layout/apklis_fragment_update.xml
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@android:color/holo_blue_dark">
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@android:color/holo_blue_dark">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="@dimen/default_margin">

<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/default_padding">
android:layout_centerInParent="true"
android:layout_margin="@dimen/default_margin">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/logo"
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:maxHeight="120dp"
android:scaleType="center" />
android:orientation="vertical"
android:padding="@dimen/default_padding">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:maxHeight="120dp"
android:scaleType="center"/>

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
android:textSize="20sp"
tools:text="Title" />
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
android:textSize="20sp"
tools:text="Title"/>

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/version"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
tools:text="0.11.20" />
android:id="@+id/version"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
tools:text="0.11.20"/>

<org.sufficientlysecure.htmltextview.HtmlTextView
android:id="@+id/changelog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="250dp"
android:layout_margin="8dp"
tools:text="Bla Bla
android:id="@+id/changelog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="250dp"
android:layout_margin="8dp"
tools:text="Bla Bla
Expand All @@ -70,31 +70,32 @@
Bla" />
Bla"/>

<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">

<com.google.android.material.button.MaterialButton
android:id="@+id/fromApklis"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/update"
android:textAllCaps="true" />
android:id="@+id/fromApklis"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/update"
android:textAllCaps="true"/>

<com.google.android.material.button.MaterialButton
android:id="@+id/download"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/download"
android:textAllCaps="true" />
android:id="@+id/download"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/download"
android:textAllCaps="true"
/>

</androidx.appcompat.widget.LinearLayoutCompat>

Expand Down
1 change: 1 addition & 0 deletions apklisupdate/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<string name="update">Abrir en apklis</string>
<string name="download">Descargar</string>
<string name="changelog">Principales cambios:</string>
<string name="open_web">Abrir Apklis</string>
</resources>
8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.core:core-ktx:1.1.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.palette:palette:1.0.0'
implementation project(':apklisupdate')
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cu.uci.ejemploapklis">

<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/cu/uci/ejemploapklis/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,41 @@ package cu.uci.ejemploapklis

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.core.content.ContextCompat
import cu.uci.apklisupdate.ApklisUpdate
import cu.uci.apklisupdate.UpdateCallback
import cu.uci.apklisupdate.model.AppUpdateInfo
import cu.uci.apklisupdate.view.ApklisUpdateFragment

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


ApklisUpdate.hasAppUpdate("cu.uci.android.apklis", callback = object : UpdateCallback {

override fun onNewUpdate(appUpdateInfo: AppUpdateInfo) {

//Start info fragment or do what you want.
supportFragmentManager.beginTransaction().add(
R.id.container, ApklisUpdateFragment.newInstance(
updateInfo = appUpdateInfo,
actionsColor = ContextCompat.getColor(this@MainActivity, R.color.colorAccent)
)
).commit()

}

override fun onOldUpdate(appUpdateInfo: AppUpdateInfo) {
Log.d("MainActivity", "onOldUpdate $appUpdateInfo")
}

override fun onError(e: Throwable) {
e.printStackTrace()
}
})
}
}
Loading

0 comments on commit 884b7b8

Please sign in to comment.