-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: Initialize and authenticate features
- Loading branch information
Showing
51 changed files
with
1,052 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...p/crashalytics/ExampleInstrumentedTest.kt → ...dsdhp/embedded/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/github/jdsdhp/enzona/payment/embedded/sample/App.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
126 changes: 107 additions & 19 deletions
126
app/src/main/java/com/github/jdsdhp/enzona/payment/embedded/sample/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
app/src/main/java/com/github/jdsdhp/enzona/payment/embedded/sample/MainViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
2 changes: 1 addition & 1 deletion
2
...ub/jdsdhp/crashalytics/ExampleUnitTest.kt → ...github/jdsdhp/embedded/ExampleUnitTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.