Dependency injection improvement (#596)
* inject MastodonApi into LoginActivity * inject AccountManager into MainActivity * inject AccountManager into SplashActivity, convert to Kotlin * inject AccountManager into AccountActivity * inject AccountManager into LoginActivity * inject AccountManager into NotificationsFragment and NotificationClearBroadcastReceiver, fix MainActivity * ooops * use same OkHttpClient for Retrofit & Picasso * fix ordering of okhttp interceptors * remove dependencies on TuskyApplication * bugfix
This commit is contained in:
parent
d17ff3eb0f
commit
b4ba457d89
18 changed files with 196 additions and 176 deletions
|
@ -42,28 +42,35 @@ public final class InstanceSwitchAuthInterceptor implements Interceptor {
|
|||
public Response intercept(@NonNull Chain chain) throws IOException {
|
||||
|
||||
Request originalRequest = chain.request();
|
||||
AccountEntity currentAccount = accountManager.getActiveAccount();
|
||||
|
||||
Request.Builder builder = originalRequest.newBuilder();
|
||||
// only switch domains if the request comes from retrofit
|
||||
if (originalRequest.url().host().equals(MastodonApi.PLACEHOLDER_DOMAIN)) {
|
||||
AccountEntity currentAccount = accountManager.getActiveAccount();
|
||||
|
||||
String instanceHeader = originalRequest.header(MastodonApi.DOMAIN_HEADER);
|
||||
if (instanceHeader != null) {
|
||||
// use domain explicitly specified in custom header
|
||||
builder.url(swapHost(originalRequest.url(), instanceHeader));
|
||||
builder.removeHeader(MastodonApi.DOMAIN_HEADER);
|
||||
} else if (currentAccount != null) {
|
||||
//use domain of current account
|
||||
builder.url(swapHost(originalRequest.url(), currentAccount.getDomain()))
|
||||
.header("Authorization",
|
||||
String.format("Bearer %s", currentAccount.getAccessToken()));
|
||||
Request.Builder builder = originalRequest.newBuilder();
|
||||
|
||||
String instanceHeader = originalRequest.header(MastodonApi.DOMAIN_HEADER);
|
||||
if (instanceHeader != null) {
|
||||
// use domain explicitly specified in custom header
|
||||
builder.url(swapHost(originalRequest.url(), instanceHeader));
|
||||
builder.removeHeader(MastodonApi.DOMAIN_HEADER);
|
||||
} else if (currentAccount != null) {
|
||||
//use domain of current account
|
||||
builder.url(swapHost(originalRequest.url(), currentAccount.getDomain()))
|
||||
.header("Authorization",
|
||||
String.format("Bearer %s", currentAccount.getAccessToken()));
|
||||
}
|
||||
Request newRequest = builder.build();
|
||||
|
||||
return chain.proceed(newRequest);
|
||||
|
||||
} else {
|
||||
return chain.proceed(originalRequest);
|
||||
}
|
||||
Request newRequest = builder.build();
|
||||
|
||||
return chain.proceed(newRequest);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private HttpUrl swapHost(@NonNull HttpUrl url, @NonNull String host) {
|
||||
private static HttpUrl swapHost(@NonNull HttpUrl url, @NonNull String host) {
|
||||
return url.newBuilder().host(host).build();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,6 +53,7 @@ import retrofit2.http.Query;
|
|||
public interface MastodonApi {
|
||||
String ENDPOINT_AUTHORIZE = "/oauth/authorize";
|
||||
String DOMAIN_HEADER = "domain";
|
||||
String PLACEHOLDER_DOMAIN = "dummy.placeholder";
|
||||
|
||||
@GET("api/v1/timelines/home")
|
||||
Call<List<Status>> homeTimeline(
|
||||
|
@ -246,6 +247,7 @@ public interface MastodonApi {
|
|||
@FormUrlEncoded
|
||||
@POST("api/v1/apps")
|
||||
Call<AppCredentials> authenticateApp(
|
||||
@Header(DOMAIN_HEADER) String domain,
|
||||
@Field("client_name") String clientName,
|
||||
@Field("redirect_uris") String redirectUris,
|
||||
@Field("scopes") String scopes,
|
||||
|
@ -254,6 +256,7 @@ public interface MastodonApi {
|
|||
@FormUrlEncoded
|
||||
@POST("oauth/token")
|
||||
Call<AccessToken> fetchOAuthToken(
|
||||
@Header(DOMAIN_HEADER) String domain,
|
||||
@Field("client_id") String clientId,
|
||||
@Field("client_secret") String clientSecret,
|
||||
@Field("redirect_uri") String redirectUri,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue