Skip to content

Commit

Permalink
Refactored NFC to be easier to use
Browse files Browse the repository at this point in the history
  • Loading branch information
Maliotis committed Apr 23, 2021
1 parent d734176 commit 71817ee
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 29 deletions.
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'


android {
compileSdkVersion 30
Expand All @@ -16,6 +16,10 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
viewBinding true
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
Expand Down
9 changes: 3 additions & 6 deletions app/src/main/java/com/maliotis/nfclib/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@ class MainActivity : AppCompatActivity() {

override fun onResume() {
super.onResume()
val readNfcFactory = NFCFactory.create<ReadNFC>(this)
val writeNfcFactory = NFCFactory.create<WriteNFC>(this)

readNFC = readNfcFactory.getNFC()
readNFC?.enableNFCInForeground()
readNFC = NFCFactory.create(this)
writeNFC = NFCFactory.create(this)

readNFC.enableNFCInForeground()
// No need to enable NFC in foreground again..
writeNFC = writeNfcFactory.getNFC()
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
3 changes: 2 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'


android {
compileSdkVersion 30
Expand All @@ -16,6 +16,7 @@ android {
consumerProguardFiles 'consumer-rules.pro'
}


buildTypes {
release {
minifyEnabled false
Expand Down
22 changes: 7 additions & 15 deletions library/src/main/java/com/maliotis/library/factories/NFCFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,22 @@ import kotlin.reflect.KClass
* Created by petrosmaliotis on 28/05/2020.
*/
abstract class NFCFactory<T: NFC> {
abstract fun getNFC(): T
abstract fun getNFC(activity: Activity): T
abstract fun handleIntent(intent: Intent)



companion object {

@JvmStatic
@PublishedApi internal
var nfcManager: NfcManager? = null

@JvmStatic
@PublishedApi internal
var activityContext: Activity? = null


inline fun <reified T: NFC>create(activity: Activity): NFCFactory<T> {
this.activityContext = activity
inline fun <reified T: NFC>create(activity: Activity): T {
this.nfcManager = activity.getSystemService(Context.NFC_SERVICE) as NfcManager

return when (T::class) {
ReadNFC::class -> ReadNFCFactory() as NFCFactory<T>
WriteNFC::class -> WriteNFCFactory() as NFCFactory<T>
ReadNFC::class -> ReadNFCFactory().getNFC(activity) as T
WriteNFC::class -> WriteNFCFactory().getNFC(activity) as T
else -> throw IllegalArgumentException()
}
}
Expand All @@ -45,13 +38,12 @@ abstract class NFCFactory<T: NFC> {
* Java alternative method
*/
@JvmStatic
fun <T: NFC> create(typeOf: Class<T>, activity: Activity): NFCFactory<T> {
this.activityContext = activity
fun <T: NFC> create(typeOf: Class<T>, activity: Activity): T {
this.nfcManager = activity.getSystemService(Context.NFC_SERVICE) as NfcManager

return when (typeOf) {
ReadNFC::javaClass -> ReadNFCFactory() as NFCFactory<T>
WriteNFC::javaClass -> WriteNFCFactory() as NFCFactory<T>
ReadNFC::javaClass -> ReadNFCFactory().getNFC(activity) as T
WriteNFC::javaClass -> WriteNFCFactory().getNFC(activity) as T
else -> throw IllegalArgumentException()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.maliotis.library.factories

import android.app.Activity
import android.content.Intent
import com.maliotis.library.nfc.ReadNFC

/**
* Created by petrosmaliotis on 29/05/2020.
*/
class ReadNFCFactory : NFCFactory<ReadNFC>() {
class ReadNFCFactory() : NFCFactory<ReadNFC>() {

override fun getNFC(): ReadNFC {
override fun getNFC(activity: Activity): ReadNFC {
val readNFC = ReadNFC()
readNFC.let {
it.activityContext = activityContext
it.activityContext = activity
it.nfcManager = nfcManager
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.maliotis.library.factories

import android.app.Activity
import android.content.Intent
import com.maliotis.library.nfc.WriteNFC

Expand All @@ -8,10 +9,10 @@ import com.maliotis.library.nfc.WriteNFC
*/
class WriteNFCFactory: NFCFactory<WriteNFC>() {

override fun getNFC(): WriteNFC {
override fun getNFC(activity: Activity): WriteNFC {
val writeNFC = WriteNFC()
writeNFC.let {
it.activityContext = activityContext
it.activityContext = activity
it.nfcManager = nfcManager
}

Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/maliotis/library/nfc/WriteNFC.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.util.*
/**
* Created by petrosmaliotis on 29/05/2020.
*/
class WriteNFC: WriteNFCI {
class WriteNFC internal constructor(): WriteNFCI {
private val TAG = WriteNFC::class.java.canonicalName

override var activityContext: Activity? = null
Expand Down

0 comments on commit 71817ee

Please sign in to comment.