Skip to content

Commit

Permalink
Chore: Initialize and authenticate features
Browse files Browse the repository at this point in the history
  • Loading branch information
jdsdhp committed Jan 27, 2024
1 parent 1113dda commit d125868
Show file tree
Hide file tree
Showing 51 changed files with 1,052 additions and 172 deletions.
23 changes: 16 additions & 7 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
plugins {
alias(libs.plugins.com.android.application)
alias(libs.plugins.org.jetbrains.kotlin.android)
kotlin("kapt")
id("com.google.dagger.hilt.android")
}

android {
Expand All @@ -10,7 +12,7 @@ android {

defaultConfig {
applicationId = "com.github.jdsdhp.enzona.payment.embedded.sample"
minSdk = 22
minSdk = 21
targetSdk = 34
versionCode = 1
versionName = "1.0"
Expand All @@ -31,17 +33,17 @@ android {
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = JavaVersion.VERSION_17.toString()
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.4.3"
kotlinCompilerExtensionVersion = "1.5.8"
}
packaging {
resources {
Expand All @@ -51,15 +53,22 @@ android {
}

dependencies {
implementation(project(":enzona-payment-embedded"))

// Hilt
implementation(libs.hilt.android)
kapt(libs.hilt.android.compiler)

implementation(libs.core.ktx)
implementation(libs.lifecycle.runtime.ktx)
implementation(libs.activity.compose)
implementation(platform(libs.compose.bom))
implementation(libs.activity.compose)
implementation(libs.androidx.lifecycle.runtime.compose)
implementation(libs.lifecycle.runtime.ktx)
implementation(libs.ui)
implementation(libs.ui.graphics)
implementation(libs.ui.tooling.preview)
implementation(libs.material3)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.espresso.core)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.jdsdhp.crashalytics
package com.github.jdsdhp.embedded

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Crashalytics"
android:theme="@style/Theme.App"
tools:targetApi="31">
<activity
android:name="com.github.jdsdhp.enzona.payment.embedded.sample.MainActivity"
android:exported="true"
android:theme="@style/Theme.Crashalytics">
android:theme="@style/Theme.App">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.jdsdhp.enzona.payment.embedded.sample

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class App : Application()
Original file line number Diff line number Diff line change
@@ -1,46 +1,134 @@
package com.github.jdsdhp.enzona.payment.embedded.sample

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.github.jdsdhp.enzona.payment.embedded.sample.ui.theme.CrashalyticsTheme
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.github.jdsdhp.enzona.payment.embedded.sample.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

private val viewModel: MainViewModel by viewModels()

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

setContent {
CrashalyticsTheme {
AppTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
Surface(modifier = Modifier.fillMaxSize()) {
MainScreen(viewModel = viewModel)
}
}
}
}
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
internal fun MainScreen(
modifier: Modifier = Modifier,
viewModel: MainViewModel,
) {

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
CrashalyticsTheme {
Greeting("Android")
val context = LocalContext.current

val uiState by viewModel.uiState.collectAsStateWithLifecycle()

Column(
modifier = modifier.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Spacer(modifier = Modifier.height(16.dp))

Text(
text = stringResource(id = R.string.app_name),
style = MaterialTheme.typography.titleLarge,
)

Spacer(modifier = Modifier.height(16.dp))

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = stringResource(R.string.consumer_key)) },
value = uiState.consumerKey,
onValueChange = { viewModel.onConsumerKeyChanged(it) },
)

Spacer(modifier = Modifier.height(8.dp))

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = stringResource(R.string.consumer_secret)) },
value = uiState.consumerSecret,
onValueChange = { viewModel.onConsumerSecretChanged(it) },
)

Spacer(modifier = Modifier.height(8.dp))

TextField(
modifier = Modifier.fillMaxWidth(),
label = { Text(text = stringResource(R.string.merchant_uuid)) },
value = uiState.merchantUUID,
onValueChange = { viewModel.onMerchantUUIDChanged(it) },
)

Spacer(modifier = Modifier.height(16.dp))

Button(onClick = { viewModel.onAuthButtonClick() }) {
Text(text = stringResource(R.string.authenticate))
}

Spacer(modifier = Modifier.height(16.dp))

HorizontalDivider()

Spacer(modifier = Modifier.height(16.dp))

}

if (uiState.isLoading) {
Box(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background),
contentAlignment = Alignment.Center,
) {
CircularProgressIndicator()
}
}

uiState.textMessage?.let {
Toast.makeText(context, it, Toast.LENGTH_LONG).show()
}

uiState.error?.let {
Toast.makeText(context, it, Toast.LENGTH_LONG).show()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.github.jdsdhp.enzona.payment.embedded.sample

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.jdsdhp.enzona.payment.embedded.Enzona
import com.github.jdsdhp.enzona.payment.embedded.util.ResultValue
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel() {

data class UiState(
val isLoading: Boolean = false,
val error: String? = null,
val textMessage: String? = null,
val consumerKey: String = "81go6n9Vz1gcSL2nK5mfZxJzDRwa",
val consumerSecret: String = "MLZucMGr3z9k5kqp49jvwJCrWU4a",
val merchantUUID: String = "2a5d8dfd49794387b408d2168a461da5",
)

private val _uiState: MutableStateFlow<UiState> = MutableStateFlow(UiState())
val uiState: StateFlow<UiState> = _uiState.asStateFlow()

fun onConsumerKeyChanged(value: String) {
viewModelScope.launch {
_uiState.update { it.copy(consumerKey = value) }
}
}

fun onConsumerSecretChanged(value: String) {
viewModelScope.launch {
_uiState.update { it.copy(consumerSecret = value) }
}
}

fun onMerchantUUIDChanged(value: String) {
viewModelScope.launch {
_uiState.update { it.copy(merchantUUID = value) }
}
}

fun onAuthButtonClick() {
viewModelScope.launch {
_uiState.update { it.copy(isLoading = true, textMessage = null, error = null) }
enzona.initialize(
apiUrl = Enzona.ApiUrl.OFFICIAL,
merchantConsumerKey = _uiState.value.consumerKey,
merchantConsumerSecret = _uiState.value.consumerSecret,
merchantUUID = _uiState.value.merchantUUID,
)
when (val res = enzona.authenticate()) {
is ResultValue.Success -> {
_uiState.update {
it.copy(
isLoading = false,
textMessage = "Authenticated Successfully!",
)
}
}

is ResultValue.Error -> {
_uiState.update {
it.copy(
isLoading = false,
error = res.exception.toString(),
)
}
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private val LightColorScheme = lightColorScheme(
)

@Composable
fun CrashalyticsTheme(
fun AppTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<resources>
<string name="app_name">Enzona Sample</string>
<string name="consumer_key">Consumer Key</string>
<string name="consumer_secret">Consumer Secret</string>
<string name="merchant_uuid">Merchant UUID</string>
<string name="authenticate">Authenticate</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="Theme.Crashalytics" parent="android:Theme.Material.Light.NoActionBar" />
<style name="Theme.App" parent="android:Theme.Material.Light.NoActionBar" />
</resources>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.jdsdhp.crashalytics
package com.github.jdsdhp.embedded

import org.junit.Test

Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ plugins {
alias(libs.plugins.com.android.application) apply false
alias(libs.plugins.org.jetbrains.kotlin.android) apply false
alias(libs.plugins.com.android.library) apply false
id("com.google.dagger.hilt.android") version "2.48" apply false
}
true // Needed to make the Suppress annotation work for the plugins block
Loading

0 comments on commit d125868

Please sign in to comment.