Browse Source

Start implementing different approach to retrofit

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 6 years ago
parent
commit
57c2858737

+ 0 - 11
app/src/main/java/com/nextcloud/talk/dagger/modules/RestModule.java

@@ -48,7 +48,6 @@ import java.security.UnrecoverableKeyException;
 import java.security.cert.CertificateException;
 import java.util.concurrent.TimeUnit;
 
-import javax.inject.Singleton;
 import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.X509KeyManager;
 
@@ -76,13 +75,11 @@ public class RestModule {
     private static final String TAG = "RestModule";
 
     @Provides
-    @Singleton
     NcApi provideNcApi(Retrofit retrofit) {
         return retrofit.create(NcApi.class);
     }
 
     @Provides
-    @Singleton
     Proxy provideProxy(AppPreferences appPreferences) {
         if (!TextUtils.isEmpty(appPreferences.getProxyType()) && !"No proxy".equals(appPreferences.getProxyType())
                 && !TextUtils.isEmpty(appPreferences.getProxyHost())) {
@@ -102,7 +99,6 @@ public class RestModule {
     }
 
     @Provides
-    @Singleton
     Retrofit provideRetrofit(OkHttpClient httpClient) {
         Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
                 .client(httpClient)
@@ -114,13 +110,11 @@ public class RestModule {
     }
 
     @Provides
-    @Singleton
     MagicTrustManager provideMagicTrustManager() {
         return new MagicTrustManager();
     }
 
     @Provides
-    @Singleton
     MagicKeyManager provideKeyManager(AppPreferences appPreferences, UserUtils userUtils) {
         KeyStore keyStore = null;
         try {
@@ -146,27 +140,23 @@ public class RestModule {
     }
 
     @Provides
-    @Singleton
     SSLSocketFactoryCompat provideSslSocketFactoryCompat(MagicKeyManager keyManager, MagicTrustManager
             magicTrustManager) {
         return new SSLSocketFactoryCompat(keyManager, magicTrustManager);
     }
 
     @Provides
-    @Singleton
     CookieManager provideCookieManager() {
         return new CookieManager();
     }
 
     @Provides
-    @Singleton
     Cache provideCache() {
         int cacheSize = 128 * 1024 * 1024; // 128 MB
         return new Cache(NextcloudTalkApplication.getSharedApplication().getCacheDir(), cacheSize);
     }
 
     @Provides
-    @Singleton
     Dispatcher provideDispatcher() {
         Dispatcher dispatcher = new Dispatcher();
         dispatcher.setMaxRequestsPerHost(100);
@@ -175,7 +165,6 @@ public class RestModule {
     }
 
     @Provides
-    @Singleton
     OkHttpClient provideHttpClient(Proxy proxy, AppPreferences appPreferences,
                                    MagicTrustManager magicTrustManager,
                                    SSLSocketFactoryCompat sslSocketFactoryCompat, Cache cache,

+ 9 - 0
app/src/main/java/com/nextcloud/talk/utils/database/user/UserUtils.java

@@ -108,6 +108,15 @@ public class UserUtils {
 
     }
 
+    public UserEntity getUserWithId(long id) {
+        Result findUserQueryResult = dataStore.select(User.class).where(UserEntity.ID.eq(id)
+                .and(UserEntity.SCHEDULED_FOR_DELETION.notEqual(true)))
+                .limit(1).get();
+
+        return (UserEntity) findUserQueryResult.firstOrNull();
+    }
+
+
     public void disableAllUsersWithoutId(long userId) {
         Result findUserQueryResult = dataStore.select(User.class).where(UserEntity.ID.notEqual(userId)).get();
 

+ 71 - 0
app/src/main/java/com/nextcloud/talk/utils/singletons/ApiHolder.java

@@ -0,0 +1,71 @@
+/*
+ * Nextcloud Talk application
+ *
+ * @author Mario Danic
+ * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.talk.utils.singletons;
+
+import android.support.annotation.Nullable;
+import android.text.TextUtils;
+
+import com.nextcloud.talk.api.NcApi;
+import com.nextcloud.talk.application.NextcloudTalkApplication;
+import com.nextcloud.talk.models.database.UserEntity;
+import com.nextcloud.talk.utils.database.user.UserUtils;
+
+import java.util.HashMap;
+
+import javax.inject.Inject;
+
+import autodagger.AutoInjector;
+import retrofit2.Retrofit;
+
+@AutoInjector(NextcloudTalkApplication.class)
+public class ApiHolder {
+    private static final String TAG = "ApiHolder";
+
+    private HashMap<Long, NcApi> ncApiHashMap;
+
+    @Inject
+    UserUtils userUtils;
+
+    @Inject
+    Retrofit retrofit;
+
+    private static final ApiHolder holder = new ApiHolder();
+
+    public static ApiHolder getInstance() {
+        return holder;
+    }
+
+    public NcApi getNcApiInstanceForAccountId(long accountId, @Nullable String baseUrl) {
+        NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
+        if (!ncApiHashMap.containsKey(accountId)) {
+            UserEntity userAccount = userUtils.getUserWithId(accountId);
+            if (userAccount == null || !TextUtils.isEmpty(baseUrl)) {
+                retrofit = retrofit.newBuilder().baseUrl(baseUrl).build();
+                return retrofit.create(NcApi.class);
+            } else {
+                retrofit = retrofit.newBuilder().baseUrl(userAccount.getBaseUrl()).build();
+                ncApiHashMap.put(accountId, retrofit.create(NcApi.class));
+            }
+        }
+
+        return ncApiHashMap.get(accountId);
+    }
+}