Skip to content

Commit

Permalink
ktlint used
Browse files Browse the repository at this point in the history
  • Loading branch information
ren6 committed Dec 5, 2023
1 parent f7015ed commit 261076f
Show file tree
Hide file tree
Showing 121 changed files with 2,543 additions and 2,096 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.apphud.demo.myapplication

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
Expand All @@ -21,4 +19,4 @@ class ExampleInstrumentedTest {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.apphud.demo.myapplication", appContext.packageName)
}
}
}
12 changes: 4 additions & 8 deletions demo/src/main/java/com/apphud/demo/ApphudApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@ package com.apphud.demo

import android.app.Application
import android.content.Context
import android.util.Log
import com.apphud.sdk.Apphud
import com.apphud.sdk.ApphudUserPropertyKey
import com.apphud.sdk.flutter.ApphudFlutter

class ApphudApplication : Application() {

val API_KEY = "app_4sY9cLggXpMDDQMmvc5wXUPGReMp8G"

companion object {
private lateinit var instance: ApphudApplication

fun applicationContext() : Context {
fun applicationContext(): Context {
return instance.applicationContext
}

fun application() : Application {
fun application(): Application {
return instance
}
}
Expand All @@ -31,8 +27,8 @@ class ApphudApplication : Application() {
super.onCreate()

Apphud.enableDebugLogs()
//Apphud.optOutOfTracking()
// Apphud.optOutOfTracking()
Apphud.start(this, API_KEY)
Apphud.collectDeviceIdentifiers()
}
}
}
50 changes: 27 additions & 23 deletions demo/src/main/java/com/apphud/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.google.android.material.navigation.NavigationView
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.drawerlayout.widget.DrawerLayout
import androidx.core.view.GravityCompat
import com.android.billingclient.api.ProductDetails
import com.apphud.demo.databinding.ActivityMainBinding
import com.google.android.material.navigation.NavigationView

class MainActivity : AppCompatActivity() {

private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
private lateinit var me: MainActivity
Expand All @@ -28,20 +26,23 @@ class MainActivity : AppCompatActivity() {
setContentView(binding.root)

val toolbar = findViewById<Toolbar>(R.id.toolbar)
toolbar?.let{
toolbar?.let {
setSupportActionBar(it)
}

val drawerLayout: DrawerLayout = binding.drawerLayout
val navView: NavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_content_main)

appBarConfiguration = AppBarConfiguration(
setOf(R.id.nav_customer,
R.id.nav_groups,
R.id.nav_purchases
), drawerLayout
)
appBarConfiguration =
AppBarConfiguration(
setOf(
R.id.nav_customer,
R.id.nav_groups,
R.id.nav_purchases,
),
drawerLayout,
)
setupActionBarWithNavController(navController, appBarConfiguration)

navView.setNavigationItemSelectedListener {
Expand All @@ -57,21 +58,24 @@ class MainActivity : AppCompatActivity() {
}

private var backPress: Long = 0

override fun onBackPressed() {
if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
binding.drawerLayout.closeDrawer(GravityCompat.START)
} else {
if(!findNavController(R.id.nav_host_fragment_content_main).navigateUp()){
if (backPress + 2000 > System.currentTimeMillis()) {
super.onBackPressed()
} else {
Toast.makeText(
baseContext, "Please press again to exit!",
Toast.LENGTH_SHORT
).show()
backPress = System.currentTimeMillis()
if (!findNavController(R.id.nav_host_fragment_content_main).navigateUp())
{
if (backPress + 2000 > System.currentTimeMillis()) {
super.onBackPressed()
} else {
Toast.makeText(
baseContext,
"Please press again to exit!",
Toast.LENGTH_SHORT,
).show()
backPress = System.currentTimeMillis()
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import com.android.billingclient.api.QueryPurchasesParams
import org.greenrobot.eventbus.EventBus

class BillingClientWrapper(
context: Context
context: Context,
) : PurchasesUpdatedListener, ProductDetailsResponseListener {

companion object {
private const val TAG = "BillingClient"

Expand All @@ -35,35 +34,38 @@ class BillingClientWrapper(
}

var productWithProductDetails = mutableMapOf<String, ProductDetails>()
var purchasesList = mutableListOf<Purchase>()
var purchaseSuccessListener: ((purchase: Purchase?, billingResult :BillingResult)->Unit)? = null
var purchasesList = mutableListOf<Purchase>()
var purchaseSuccessListener: ((purchase: Purchase?, billingResult: BillingResult) -> Unit)? = null

// Initialize the BillingClient.
private val billingClient = BillingClient.newBuilder(context)
.setListener(this)
.enablePendingPurchases()
.build()
private val billingClient =
BillingClient.newBuilder(context)
.setListener(this)
.enablePendingPurchases()
.build()

// Establish a connection to Google Play.
fun startBillingConnection(billingConnectionState: MutableLiveData<Boolean>) {
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
Log.d(TAG, "Billing response OK")
// The BillingClient is ready. You can query purchases and product details here
queryPurchases()
queryProductDetails()
billingConnectionState.postValue(true)
} else {
Log.e(TAG, billingResult.debugMessage)
billingClient.startConnection(
object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
Log.d(TAG, "Billing response OK")
// The BillingClient is ready. You can query purchases and product details here
queryPurchases()
queryProductDetails()
billingConnectionState.postValue(true)
} else {
Log.e(TAG, billingResult.debugMessage)
}
}
}

override fun onBillingServiceDisconnected() {
Log.i(TAG, "Billing connection disconnected")
startBillingConnection(billingConnectionState)
}
})
override fun onBillingServiceDisconnected() {
Log.i(TAG, "Billing connection disconnected")
startBillingConnection(billingConnectionState)
}
},
)
}

// Query Google Play Billing for existing purchases.
Expand All @@ -74,7 +76,7 @@ class BillingClientWrapper(
}
// Query for existing subscription products that have been purchased.
billingClient.queryPurchasesAsync(
QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build()
QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build(),
) { billingResult, purchaseList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
if (!purchaseList.isNullOrEmpty()) {
Expand All @@ -93,12 +95,11 @@ class BillingClientWrapper(
val params = QueryProductDetailsParams.newBuilder()
val productList = mutableListOf<QueryProductDetailsParams.Product>()
for (product in LIST_OF_PRODUCTS) {

productList.add(
QueryProductDetailsParams.Product.newBuilder()
.setProductId(product)
.setProductType(BillingClient.ProductType.SUBS)
.build()
.build(),
)

params.setProductList(productList).let { productDetailsParams ->
Expand All @@ -113,7 +114,7 @@ class BillingClientWrapper(
// to [_productWithProductDetails].
override fun onProductDetailsResponse(
billingResult: BillingResult,
productDetailsList: MutableList<ProductDetails>
productDetailsList: MutableList<ProductDetails>,
) {
val responseCode = billingResult.responseCode
val debugMessage = billingResult.debugMessage
Expand All @@ -124,14 +125,15 @@ class BillingClientWrapper(
Log.e(
TAG,
"onProductDetailsResponse: " +
"Found null or empty ProductDetails. " +
"Check to see if the Products you requested are correctly " +
"published in the Google Play Console."
"Found null or empty ProductDetails. " +
"Check to see if the Products you requested are correctly " +
"published in the Google Play Console.",
)
} else {
newMap = productDetailsList.associateBy {
it.productId
}
newMap =
productDetailsList.associateBy {
it.productId
}
}
productWithProductDetails.putAll(newMap)
EventBus.getDefault().post(RefreshEvent())
Expand All @@ -143,7 +145,10 @@ class BillingClientWrapper(
}

// Launch Purchase flow
fun launchBillingFlow(activity: Activity, params: BillingFlowParams) {
fun launchBillingFlow(
activity: Activity,
params: BillingFlowParams,
) {
if (!billingClient.isReady) {
Log.e(TAG, "launchBillingFlow: BillingClient is not ready")
}
Expand All @@ -153,11 +158,11 @@ class BillingClientWrapper(
// PurchasesUpdatedListener that helps handle new purchases returned from the API
override fun onPurchasesUpdated(
billingResult: BillingResult,
purchases: List<Purchase>?
purchases: List<Purchase>?,
) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
// Post new purchase List to _purchases
purchases?.let{
purchases?.let {
purchasesList.clear()
purchasesList.addAll(it)

Expand All @@ -180,15 +185,17 @@ class BillingClientWrapper(
private fun acknowledgePurchases(purchase: Purchase?) {
purchase?.let {
if (!it.isAcknowledged) {
val params = AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(it.purchaseToken)
.build()
val params =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(it.purchaseToken)
.build()

billingClient.acknowledgePurchase(
params
params,
) { billingResult ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK
&& it.purchaseState == Purchase.PurchaseState.PURCHASED) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK &&
it.purchaseState == Purchase.PurchaseState.PURCHASED
) {
purchaseSuccessListener?.invoke(purchase, billingResult)
}
}
Expand Down
Loading

0 comments on commit 261076f

Please sign in to comment.