Skip to content

Commit

Permalink
refactor: improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanpierrefortune committed Dec 5, 2023
1 parent 107cd8b commit fd7c4dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package org.calypsonet.keyple.demo.reload.remote.data

import kotlin.jvm.Throws
import org.eclipse.keyple.core.common.KeyplePluginExtensionFactory
import org.eclipse.keyple.core.service.Plugin
import org.eclipse.keyple.core.service.SmartCardServiceProvider
import org.eclipse.keypop.reader.CardReader
import org.eclipse.keypop.reader.ObservableCardReader
Expand All @@ -26,11 +27,11 @@ import timber.log.Timber
object ReaderRepository {

/** Register any keyple plugin */
fun registerPlugin(factory: KeyplePluginExtensionFactory) {
try {
fun registerPlugin(factory: KeyplePluginExtensionFactory): Plugin? {
return try {
SmartCardServiceProvider.getService().registerPlugin(factory)
} catch (e: Exception) {
Timber.e(e)
null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.calypsonet.keyple.demo.reload.remote.data.model.CardReaderResponse
import org.calypsonet.keyple.demo.reload.remote.data.model.DeviceEnum
import org.calypsonet.keyple.demo.reload.remote.data.model.Status
import org.eclipse.keyple.core.service.KeyplePluginException
import org.eclipse.keyple.core.service.Plugin
import org.eclipse.keyple.distributed.LocalServiceClient
import org.eclipse.keyple.plugin.android.nfc.AndroidNfcPlugin
import org.eclipse.keyple.plugin.android.nfc.AndroidNfcPluginFactoryProvider
Expand Down Expand Up @@ -88,18 +89,24 @@ abstract class AbstractCardActivity :
/** Android Nfc Reader is strongly dependent and Android Activity component. */
@Throws(KeyplePluginException::class)
fun initAndActivateAndroidKeypleNfcReader() {
readerRepository.registerPlugin(
AndroidNfcPluginFactoryProvider(this@AbstractCardActivity).getFactory())
val plugin: Plugin? =
readerRepository.registerPlugin(
AndroidNfcPluginFactoryProvider(this@AbstractCardActivity).getFactory())

val androidNfcReader = readerRepository.getObservableReader(selectedDeviceReaderName)
androidNfcReader.setReaderObservationExceptionHandler(this@AbstractCardActivity)
androidNfcReader.addObserver(this@AbstractCardActivity)
androidNfcReader.setReaderObservationExceptionHandler(this@AbstractCardActivity)
if (plugin == null) {
return
}

(readerRepository.getReader(selectedDeviceReaderName) as ConfigurableCardReader)
.activateProtocol(AndroidNfcSupportedProtocols.ISO_14443_4.name, "ISO_14443_4")
val observableCardReader: ObservableCardReader =
readerRepository.getObservableReader(selectedDeviceReaderName)
observableCardReader.setReaderObservationExceptionHandler(this@AbstractCardActivity)
observableCardReader.addObserver(this@AbstractCardActivity)
observableCardReader.setReaderObservationExceptionHandler(this@AbstractCardActivity)

(observableCardReader as ConfigurableCardReader).activateProtocol(
AndroidNfcSupportedProtocols.ISO_14443_4.name, "ISO_14443_4")

androidNfcReader.startCardDetection(ObservableCardReader.DetectionMode.REPEATING)
observableCardReader.startCardDetection(ObservableCardReader.DetectionMode.REPEATING)
}

@Throws(KeyplePluginException::class)
Expand Down

0 comments on commit fd7c4dc

Please sign in to comment.