Skip to content

Commit

Permalink
Merge branch 'main' into PM-86-android-15
Browse files Browse the repository at this point in the history
  • Loading branch information
a-szotyori committed Jul 15, 2024
2 parents eedde29 + f218e15 commit d7a5306
Show file tree
Hide file tree
Showing 19 changed files with 329 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.IntentCompat
Expand Down Expand Up @@ -43,12 +45,14 @@ class MainActivity : AppCompatActivity() {
private var cancellationToken: CancellationToken? =
null // should be kept across configuration changes
private val permissionHandler = PermissionHandler(this)
private var configurationActivityLauncher: ActivityResultLauncher<Intent>? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
addInputHandlers()
setupActivityResultLauncher()
showVersions()
if (savedInstanceState == null) {
if (isIntentActionViewOrSend(intent)) {
Expand Down Expand Up @@ -89,6 +93,29 @@ class MainActivity : AppCompatActivity() {

}

private fun setupActivityResultLauncher() {
configurationActivityLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
when (result.resultCode) {
RESULT_CANCELED -> {}
RESULT_OK -> {
val configurationResult: Configuration? = result.data?.getParcelableExtra(
CONFIGURATION_BUNDLE
)
if (configurationResult != null) {
configurationViewModel.setConfiguration(configurationResult)
}

configurationViewModel.disableCameraPermission(
result.data?.getBooleanExtra(
CAMERA_PERMISSION_BUNDLE, false
) ?: false
)
}
}
}
}

private fun addInputHandlers() {
binding.buttonStartScanner.setOnClickListener {
checkIfAppShouldAskForCameraPermission(EntryPoint.BUTTON)
Expand All @@ -105,22 +132,18 @@ class MainActivity : AppCompatActivity() {
}

binding.textGiniBankVersion.setOnClickListener {
startActivityForResult(
Intent(
this, ConfigurationActivity::class.java
)
configurationActivityLauncher?.launch(
Intent(this, ConfigurationActivity::class.java)
.putExtra(
CONFIGURATION_BUNDLE,
configurationViewModel.configurationFlow.value
)
.putExtra(
CAMERA_PERMISSION_BUNDLE,
configurationViewModel.disableCameraPermissionFlow.value
),
REQUEST_CONFIGURATION
)
)
}

}

private fun checkIfAppShouldAskForCameraPermission(entryPoint: EntryPoint) {
Expand Down Expand Up @@ -226,31 +249,6 @@ class MainActivity : AppCompatActivity() {
)
}

override fun onActivityResult(
requestCode: Int, resultCode: Int, data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CONFIGURATION) {
when (resultCode) {
RESULT_CANCELED -> {}
RESULT_OK -> {
var configurationResult: Configuration? = data?.getParcelableExtra(
CONFIGURATION_BUNDLE
)
if (configurationResult != null) {
configurationViewModel.setConfiguration(configurationResult)
}

configurationViewModel.disableCameraPermission(
data?.getBooleanExtra(
CAMERA_PERMISSION_BUNDLE, false
) ?: false
)
}
}
}
}


companion object {
const val CONFIGURATION_BUNDLE = "CONFIGURATION_BUNDLE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.app.Activity.RESULT_OK
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.os.Build
import android.os.Bundle
Expand All @@ -15,6 +14,8 @@ import android.provider.MediaStore
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.setFragmentResult
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
Expand Down Expand Up @@ -43,14 +44,14 @@ import net.gini.android.capture.internal.util.getLayoutInflaterWithGiniCaptureTh
private const val ARG_DOCUMENT_IMPORT_FILE_TYPES = "GC_EXTRA_IN_DOCUMENT_IMPORT_FILE_TYPES"
private const val GRID_SPAN_COUNT_PHONE = 3
private const val GRID_SPAN_COUNT_TABLET = 6
private const val REQ_CODE_CHOOSE_FILE = 1

/**
* Internal use only.
*/
class FileChooserFragment : BottomSheetDialogFragment() {
private var docImportEnabledFileTypes: DocumentImportEnabledFileTypes? = null
private var binding: GcFragmentFileChooserBinding by autoCleared()
private var chooseFileLauncher: ActivityResultLauncher<Intent>? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -61,6 +62,7 @@ class FileChooserFragment : BottomSheetDialogFragment() {
it.getSerializable(ARG_DOCUMENT_IMPORT_FILE_TYPES) as? DocumentImportEnabledFileTypes
}
}
setupFileChooserListener()
}

override fun onGetLayoutInflater(savedInstanceState: Bundle?): LayoutInflater {
Expand Down Expand Up @@ -98,6 +100,47 @@ class FileChooserFragment : BottomSheetDialogFragment() {
binding.gcFileProviders.layoutManager = GridLayoutManager(requireContext(), getGridSpanCount())
}

private fun setupFileChooserListener() {
chooseFileLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
findNavController().popBackStack()
when (result.resultCode) {
RESULT_OK -> {
result.data?.let { data ->
setFragmentResult(REQUEST_KEY, Bundle().apply {
putParcelable(RESULT_KEY, FileChooserResult.FilesSelected(data))
})
} ?: run {
setFragmentResult(REQUEST_KEY, Bundle().apply {
putParcelable(
RESULT_KEY, FileChooserResult.Error(
GiniCaptureError(
GiniCaptureError.ErrorCode.DOCUMENT_IMPORT,
"Activity result data was null."
)
)
)
})
}
}

RESULT_CANCELED -> setFragmentResult(REQUEST_KEY, Bundle().apply {
putParcelable(RESULT_KEY, FileChooserResult.Cancelled)
})

else -> setFragmentResult(REQUEST_KEY, Bundle().apply {
putParcelable(
RESULT_KEY, FileChooserResult.Error(
GiniCaptureError(
GiniCaptureError.ErrorCode.DOCUMENT_IMPORT,
"Unexpected result code for activity result."
)
)
)
})
}
}
}

private fun getGridSpanCount(): Int =
if (ContextHelper.isTablet(requireContext())) GRID_SPAN_COUNT_TABLET else GRID_SPAN_COUNT_PHONE

Expand Down Expand Up @@ -137,62 +180,7 @@ class FileChooserFragment : BottomSheetDialogFragment() {
item.resolveInfo.activityInfo.packageName,
item.resolveInfo.activityInfo.name
)
startActivityForResult(item.intent, REQ_CODE_CHOOSE_FILE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

findNavController().popBackStack()

if (requestCode == REQ_CODE_CHOOSE_FILE) {
when (resultCode) {
RESULT_OK -> {
if (data != null) {
setFragmentResult(REQUEST_KEY, Bundle().apply {
putParcelable(RESULT_KEY, FileChooserResult.FilesSelected(data))
})
} else {
setFragmentResult(REQUEST_KEY, Bundle().apply {
putParcelable(
RESULT_KEY, FileChooserResult.Error(
GiniCaptureError(
GiniCaptureError.ErrorCode.DOCUMENT_IMPORT,
"Activity result data was null."
)
)
)
})
}
}

RESULT_CANCELED -> setFragmentResult(REQUEST_KEY, Bundle().apply {
putParcelable(RESULT_KEY, FileChooserResult.Cancelled)
})

else -> setFragmentResult(REQUEST_KEY, Bundle().apply {
putParcelable(
RESULT_KEY, FileChooserResult.Error(
GiniCaptureError(
GiniCaptureError.ErrorCode.DOCUMENT_IMPORT,
"Unexpected result code for activity result."
)
)
)
})
}
} else {
setFragmentResult(REQUEST_KEY, Bundle().apply {
putParcelable(
RESULT_KEY, FileChooserResult.Error(
GiniCaptureError(
GiniCaptureError.ErrorCode.DOCUMENT_IMPORT,
"Unexpected request code for activity result."
)
)
)
})
}
chooseFileLauncher?.launch(item.intent)
}

private fun getImageProviderItems(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import ch.qos.logback.classic.android.LogcatAppender
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.coroutines.launch
import net.gini.android.health.sdk.exampleapp.configuration.ConfigurationFragment
import net.gini.android.health.sdk.exampleapp.databinding.ActivityMainBinding
import net.gini.android.health.sdk.exampleapp.invoices.ui.AppCompatThemeInvoicesActivity
import net.gini.android.health.sdk.exampleapp.invoices.ui.InvoicesActivity
Expand Down Expand Up @@ -71,11 +72,26 @@ class MainActivity : AppCompatActivity() {
}

binding.invoicesScreen.setOnClickListener {
startActivity(Intent(this, InvoicesActivity::class.java))
startActivity(Intent(this, InvoicesActivity::class.java).apply {
viewModel.getPaymentComponentConfiguration()?.let {
putExtra(PAYMENT_COMPONENT_CONFIG, it)
}
})
}

binding.appcompatThemeInvoicesScreen.setOnClickListener {
startActivity(Intent(this, AppCompatThemeInvoicesActivity::class.java))
startActivity(Intent(this, AppCompatThemeInvoicesActivity::class.java).apply {
viewModel.getPaymentComponentConfiguration()?.let {
putExtra(PAYMENT_COMPONENT_CONFIG, it)
}
})
}

with(binding.giniHealthVersion) {
text = "${getString(R.string.gini_health_version)} ${net.gini.android.health.sdk.BuildConfig.VERSION_NAME}"
setOnClickListener {
openConfigurationScreen()
}
}

configureLogging()
Expand Down Expand Up @@ -119,7 +135,15 @@ class MainActivity : AppCompatActivity() {
root.addAppender(logcatAppender)
}

private fun openConfigurationScreen() {
supportFragmentManager.beginTransaction()
.add(binding.configurationContainer.id, ConfigurationFragment.newInstance(), ConfigurationFragment::class.java.name)
.addToBackStack(ConfigurationFragment::class.java.name)
.commit()
}

companion object {
private val LOG = LoggerFactory.getLogger(MainActivity::class.java)
val PAYMENT_COMPONENT_CONFIG = "payment_component_config"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import net.gini.android.health.sdk.GiniHealth
import net.gini.android.health.sdk.exampleapp.pager.PagerAdapter
import net.gini.android.health.sdk.paymentcomponent.PaymentComponentConfiguration
import java.io.File

class MainViewModel(
Expand All @@ -20,6 +21,7 @@ class MainViewModel(

private var currentIndex = 0
private var currentFileUri: Uri? = null
private var paymentComponentConfiguration : PaymentComponentConfiguration? = null

fun getNextPageUri(context: Context): Uri {
val uriForFile = FileProvider.getUriForFile(
Expand All @@ -43,4 +45,10 @@ class MainViewModel(
giniHealth.setDocumentForReview(documentId)
}
}

fun setPaymentComponentConfiguration(config: PaymentComponentConfiguration) {
paymentComponentConfiguration = config
}

fun getPaymentComponentConfiguration() = paymentComponentConfiguration
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.gini.android.health.sdk.exampleapp.configuration

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import net.gini.android.health.sdk.exampleapp.MainViewModel
import net.gini.android.health.sdk.exampleapp.databinding.FragmentConfigurationBinding
import net.gini.android.health.sdk.exampleapp.review.ReviewViewModel
import net.gini.android.health.sdk.paymentcomponent.PaymentComponentConfiguration

class ConfigurationFragment: Fragment() {

private lateinit var binding: FragmentConfigurationBinding
private val viewModel by activityViewModels<MainViewModel>()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentConfigurationBinding.inflate(layoutInflater)
with(binding) {
setupSwitchListeners()
}
return binding.root
}

private fun FragmentConfigurationBinding.setupSwitchListeners() {
ghsHidePoweredBy.isChecked = !(viewModel.getPaymentComponentConfiguration()?.isPaymentComponentBranded ?: true)

ghsHidePoweredBy.setOnCheckedChangeListener { _, newValue ->
viewModel.setPaymentComponentConfiguration(PaymentComponentConfiguration(isPaymentComponentBranded = !newValue))
}
}

companion object {
fun newInstance() = ConfigurationFragment()
}
}
Loading

0 comments on commit d7a5306

Please sign in to comment.