Skip to content

Commit

Permalink
renaming some classes (#107)
Browse files Browse the repository at this point in the history
* renaming some classes

* added changes into changelog

* readme adjustment
  • Loading branch information
andretietz authored Jun 8, 2021
1 parent 52de263 commit 96371cd
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Removing the Retrofit CallAdapter in order to enrich the requests using [this method](https://andretietz.com/2021/04/06/custom-retrofit2-annotations-revisited/)
* using the github api instead of facebook in the demo.
* Renaming retroauth-android to android-accountmanager. WARNING: This changes the project-import!!!
* Renaming AndroidOwnerStorage to AndroidAccountManagerOwnerStorage and AndroidCredentialStorage to AndroidAccountManagerCredentialStorage
## 3.1.0 (2021-01-22)
* Removing [startup hack](https://andretietz.medium.com/auto-initialize-your-android-library-2349daf06920) with google version of it.
* increasing min SDK level from 14 to 21
Expand Down
44 changes: 38 additions & 6 deletions android-accountmanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,46 @@ interface SomeAuthenticatedService {
```

* Create the Retrofit object and instantiate it
```java
Retrofit retrofit = RetroauthAndroidBuilder.create(application, YourAuthenticator()))
```kotlin
val authenticator: Authenticator = YourAuthenticator() // See Usage
val baseRetrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://api.awesome.com/")
// add whatever you used to do with retrofit2
// i.e.:
// setup your retrofit as you wish...
.addConverterFactory(GsonConverterFactory.create())

.build();
.build()

// Either this (which also works in plain java)
val authRetrofit: Retrofit = RetroauthAndroid.setup(
retrofit,
application,
authenticator
)
// OR this
val authRetrofit = baseRetrofit.androidAuthentication(application, authenticator)

// create your services
val service = authRetrofit.create(SomeAuthenticatedService.class)
// use them
service.someAuthenticatedCall().execute()
```
Another option is to create the retrofit instance completely yourself:
```kotlin
// OR if you want to have full control:
val authenticator: Authenticator = YourAuthenticator() // See Usage
val ownerStorage: OwnerStorage<String, Account, AndroidCredentialType> = AndroidAccountManagerOwnerStorage(application)
val credentialStorage: CredentialStorage<Account, AndroidCredentialType, AndroidCredentials> = AndroidAccountManagerCredentialStorage(application)
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://api.awesome.com/")
// setup your retrofit as you wish...
.addConverterFactory(GsonConverterFactory.create())
.client(
OkHttpClient.Builder()
.addInterceptor(CredentialInterceptor(authenticator, ownerStorage, credentialStorage))
.build()
)
.build()


// create your services
val service = retrofit.create(SomeAuthenticatedService.class)
// use them
Expand Down
2 changes: 1 addition & 1 deletion android-accountmanager/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {

defaultConfig {
minSdkVersion 21
testInstrumentationRunner "androidx.test.ext.junit.runners.AndroidJUnit4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class AccountAuthenticator(
accountType: String,
credentialType: String?,
accountName: String?
): Bundle? = Bundle().apply {
): Bundle = Bundle().apply {
putParcelable(
AccountManager.KEY_INTENT,
Intent(action).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import java.util.concurrent.FutureTask
/**
* This is the implementation of a [CredentialStorage] in Android using the Android [AccountManager]
*/
class AndroidCredentialStorage constructor(
class AndroidAccountManagerCredentialStorage constructor(
private val application: Application
) : CredentialStorage<Account, AndroidCredentialType, AndroidCredentials> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import java.util.concurrent.Future
import java.util.concurrent.TimeUnit

/**
* This is the Android implementation of an [OwnerStorage]. It does all the Android [Account] handling
* This is the Android implementation of an [OwnerStorage]. It does all the Android [Account]
* handling using tha Android [AccountManager].
*/
@Suppress("unused")
class AndroidOwnerStorage constructor(
class AndroidAccountManagerOwnerStorage constructor(
private val application: Application
) : OwnerStorage<String, Account, AndroidCredentialType> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ abstract class AuthenticationActivity : AppCompatActivity() {
private lateinit var accountManager: AccountManager
private var credentialType: String? = null
private lateinit var resultBundle: Bundle
private lateinit var credentialStorage: AndroidCredentialStorage
private val ownerManager by lazy { AndroidOwnerStorage(application) }
private lateinit var credentialStorage: AndroidAccountManagerCredentialStorage
private val ownerManager by lazy { AndroidAccountManagerOwnerStorage(application) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
accountManager = AccountManager.get(application)
credentialStorage = AndroidCredentialStorage(application)
credentialStorage = AndroidAccountManagerCredentialStorage(application)

accountAuthenticatorResponse = intent.getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE)
accountAuthenticatorResponse?.onRequestContinued()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ object RetroauthAndroid {
return Retroauth.setup(
retrofit,
authenticator,
AndroidOwnerStorage(application),
AndroidCredentialStorage(application)
AndroidAccountManagerOwnerStorage(application),
AndroidAccountManagerCredentialStorage(application)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.andretietz.retroauth.demo.auth
import android.accounts.Account
import android.app.Application
import android.content.Context
import com.andretietz.retroauth.AndroidCredentialStorage
import com.andretietz.retroauth.AndroidAccountManagerCredentialStorage
import com.andretietz.retroauth.AndroidCredentialType
import com.andretietz.retroauth.AndroidCredentials
import com.andretietz.retroauth.AndroidOwnerStorage
import com.andretietz.retroauth.AndroidAccountManagerOwnerStorage
import com.andretietz.retroauth.Authenticator
import com.andretietz.retroauth.demo.R
import okhttp3.Request
Expand All @@ -19,8 +19,8 @@ import okhttp3.Request
*/
class GithubAuthenticator(application: Application) : Authenticator<String, Account, AndroidCredentialType, AndroidCredentials>() {

private val credentialStorage by lazy { AndroidCredentialStorage(application) }
private val ownerStorage by lazy { AndroidOwnerStorage(application) }
private val credentialStorage by lazy { AndroidAccountManagerCredentialStorage(application) }
private val ownerStorage by lazy { AndroidAccountManagerOwnerStorage(application) }

companion object {
const val CLIENT_ID = "bb86ddeb2dd22163192f"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.andretietz.retroauth.demo.di

import android.app.Application
import com.andretietz.retroauth.AndroidCredentialStorage
import com.andretietz.retroauth.AndroidOwnerStorage
import com.andretietz.retroauth.AndroidAccountManagerCredentialStorage
import com.andretietz.retroauth.AndroidAccountManagerOwnerStorage
import com.andretietz.retroauth.androidAuthentication
import com.andretietz.retroauth.demo.auth.GithubAuthenticator
import com.github.scribejava.apis.GitHubApi
Expand Down Expand Up @@ -52,10 +52,6 @@ class ApiModule {
val httpClient = OkHttpClient.Builder()
.addNetworkInterceptor(HttpLoggingInterceptor()
.also { it.level = HttpLoggingInterceptor.Level.BODY })
.addInterceptor(HttpLoggingInterceptor()
.also {
it.level = HttpLoggingInterceptor.Level.HEADERS
})
.addInterceptor { chain: Interceptor.Chain ->
val request = chain.request().newBuilder()
.addHeader("Accept", "application/vnd.github.v3+json")
Expand All @@ -78,11 +74,11 @@ class ApiModule {

@Singleton
@Provides
fun providesOwnerStorage(application: Application) = AndroidOwnerStorage(application)
fun providesOwnerStorage(application: Application) = AndroidAccountManagerOwnerStorage(application)

@Singleton
@Provides
fun providesCredentialStorage(application: Application) = AndroidCredentialStorage(application)
fun providesCredentialStorage(application: Application) = AndroidAccountManagerCredentialStorage(application)

@Singleton
@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.andretietz.retroauth.demo.screen.main

import android.accounts.Account
import androidx.lifecycle.ViewModel
import com.andretietz.retroauth.AndroidCredentialStorage
import com.andretietz.retroauth.AndroidOwnerStorage
import com.andretietz.retroauth.AndroidAccountManagerCredentialStorage
import com.andretietz.retroauth.AndroidAccountManagerOwnerStorage
import com.andretietz.retroauth.demo.auth.GithubAuthenticator
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
Expand All @@ -15,8 +15,8 @@ import javax.inject.Inject
@HiltViewModel
class AndroidMainViewModel @Inject constructor(
retrofit: Retrofit,
private val ownerStorage: AndroidOwnerStorage,
credentialStorage: AndroidCredentialStorage,
private val ownerStorage: AndroidAccountManagerOwnerStorage,
credentialStorage: AndroidAccountManagerCredentialStorage,
private val authenticator: GithubAuthenticator,
cache: Cache
) : ViewModel(), MainViewModel<Account> by CommonMainViewModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.andretietz.retroauth.AndroidOwnerStorage
import com.andretietz.retroauth.AndroidAccountManagerOwnerStorage
import com.andretietz.retroauth.demo.R
import com.andretietz.retroauth.demo.databinding.ActivityRepositoryListBinding
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -27,7 +27,7 @@ class MainActivity : AppCompatActivity() {
private val viewModel: AndroidMainViewModel by viewModels()

@Inject
lateinit var ownerStorage: AndroidOwnerStorage
lateinit var ownerStorage: AndroidAccountManagerOwnerStorage

private lateinit var binding: ActivityRepositoryListBinding

Expand Down

0 comments on commit 96371cd

Please sign in to comment.