https://kotlinlang.org logo
#android
Title
# android
p

Pablichjenkov

10/13/2019, 4:02 AM
If it needs to be accessed from different Activities/Services/BroadcastRecievers I would move it to the Application object, then inject it all the way down. I like it better than making it an
object
.
s

spierce7

10/13/2019, 4:14 AM
This is a library, so I don’t own the
Application
object. Otherwise, that’s what I’d do.
I’d really like to keep it as an
interface
. so that a mock one for testing can be created. The current approach that I’m taking is that in the constructor of the object, I’m assigning it a
Long
id
from an
AtomicLong
, and inserting it into an internal
object
where it’s held with a
WeakReference
. Then I pass the id of it into the
Activity
, and the
Activity
pulls it from the internal object that keeps track of any active instance. I’m not very happy with this approach though. It’s VERY hacky. I’m unsure of any alternatives though
s

spierce7

10/14/2019, 4:32 AM
@louiscad how does this solve my problem?
l

louiscad

10/14/2019, 6:37 AM
@spierce7 Now I read in your previous message out of the thread that you have a more complex requirement than I thought when I read
BroadcastReceiver
and
object
. It can help have the application Context for cases where the activity's one is not needed, but I think that's irrelevant for your need, I'm sorry for the noise.
s

spierce7

10/14/2019, 3:22 PM
thx for trying.
p

Pablichjenkov

10/14/2019, 4:32 PM
I see your point now. Initially I did not get well your intention. In such a case, I don't think what you are doing is too crazy. My only suggestion is changing the name of
CompanyAuth()
to
CompanyAuthClient()
or
CompanyAuthAccessor()
or
CompanyAuthConnection()
. Some name that indicates a user of this class that you don't talk directly to the
CompanyAuth()
implementation but to an intermediary class.
s

spierce7

10/14/2019, 5:20 PM
I thought of a happy medium yesterday. I maintain the interface
CompanyAuth
, with the function
CompanyAuth
to get an instance of it, but then I have the instance returned from the function to be an object:
Copy code
internal object InternalCompanyAuth : CompanyAuth {
  // ...
}
Copy code
fun CompanyAuth(): CompanyAuth = InternalCompanyAuth
p

Pablichjenkov

10/14/2019, 5:42 PM
I see, although I would insist in naming your "builder" function more descriptive. Something like
getCompanyAuth()
. Using
CompanyAuth()
sounds like you are creating a new class instance, and is not the case
s

spierce7

10/14/2019, 5:46 PM
that’s an interesting point
2 Views