Skip to content

Commit

Permalink
refactor: sharedInstance might return non-nullable instance
Browse files Browse the repository at this point in the history
The `sharedInstance` that creates a new instance of `ParselyTracker` should not have nullable return type
  • Loading branch information
wzieba committed Dec 13, 2023
1 parent 7b02055 commit 857127a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class FunctionalTests {
field.set(this, url)
return ParselyTracker.sharedInstance(
siteId, flushInterval.inWholeSeconds.toInt(), activity.application
)!!
)
}

private companion object {
Expand Down
19 changes: 11 additions & 8 deletions parsely/src/main/java/com/parsely/parselyandroid/ParselyTracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -407,26 +407,29 @@ open class ParselyTracker protected constructor(siteId: String, flushInterval: I
*/
@JvmStatic
fun sharedInstance(): ParselyTracker? {
return if (instance == null) {
null
} else instance
return instance
}

/**
* Singleton instance factory Note: this must be called before [.sharedInstance]
*
* @param siteId The Parsely public site id (eg "example.com")
* @param flushInterval The interval at which the events queue should flush, in seconds
* @param c The current Android application context
* @param context The current Android application context
* @return The singleton instance
*/
@JvmStatic
@JvmOverloads
fun sharedInstance(siteId: String, flushInterval: Int = DEFAULT_FLUSH_INTERVAL_SECS, c: Context): ParselyTracker? {
if (instance == null) {
instance = ParselyTracker(siteId, flushInterval, c)
fun sharedInstance(
siteId: String,
flushInterval: Int = DEFAULT_FLUSH_INTERVAL_SECS,
context: Context
): ParselyTracker {
return instance ?: run {
val newInstance = ParselyTracker(siteId, flushInterval, context)
instance = newInstance
return newInstance
}
return instance
}
}
}

0 comments on commit 857127a

Please sign in to comment.