2018-03-28 04:47:00 +11:00
|
|
|
/* Copyright 2018 charlag
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Tusky 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 Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.di
|
|
|
|
|
2018-04-23 01:20:01 +10:00
|
|
|
import android.content.Context
|
2018-03-28 04:47:00 +11:00
|
|
|
import android.text.Spanned
|
|
|
|
import com.google.gson.Gson
|
|
|
|
import com.google.gson.GsonBuilder
|
|
|
|
import com.google.gson.JsonDeserializer
|
2018-04-23 01:20:01 +10:00
|
|
|
import com.keylesspalace.tusky.BuildConfig
|
2018-03-28 04:47:00 +11:00
|
|
|
import com.keylesspalace.tusky.db.AccountManager
|
|
|
|
import com.keylesspalace.tusky.json.SpannedTypeAdapter
|
|
|
|
import com.keylesspalace.tusky.network.InstanceSwitchAuthInterceptor
|
|
|
|
import com.keylesspalace.tusky.network.MastodonApi
|
|
|
|
import com.keylesspalace.tusky.util.OkHttpUtils
|
|
|
|
import dagger.Module
|
|
|
|
import dagger.Provides
|
|
|
|
import dagger.multibindings.ClassKey
|
|
|
|
import dagger.multibindings.IntoMap
|
|
|
|
import dagger.multibindings.IntoSet
|
|
|
|
import okhttp3.OkHttpClient
|
2018-04-23 01:20:01 +10:00
|
|
|
import okhttp3.logging.HttpLoggingInterceptor
|
2018-03-28 04:47:00 +11:00
|
|
|
import retrofit2.Converter
|
|
|
|
import retrofit2.Retrofit
|
2018-10-04 05:27:52 +10:00
|
|
|
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
|
2018-03-28 04:47:00 +11:00
|
|
|
import retrofit2.converter.gson.GsonConverterFactory
|
|
|
|
import javax.inject.Singleton
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by charlag on 3/24/18.
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Module
|
|
|
|
class NetworkModule {
|
2019-02-13 05:22:37 +11:00
|
|
|
|
2018-03-28 04:47:00 +11:00
|
|
|
@Provides
|
2019-02-12 02:59:01 +11:00
|
|
|
@IntoMap
|
2018-03-28 04:47:00 +11:00
|
|
|
@ClassKey(Spanned::class)
|
|
|
|
fun providesSpannedTypeAdapter(): JsonDeserializer<*> = SpannedTypeAdapter()
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
@Singleton
|
|
|
|
fun providesGson(adapters: @JvmSuppressWildcards Map<Class<*>, JsonDeserializer<*>>): Gson {
|
|
|
|
return GsonBuilder()
|
|
|
|
.apply {
|
|
|
|
for ((k, v) in adapters) {
|
|
|
|
registerTypeAdapter(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.create()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
@IntoSet
|
|
|
|
@Singleton
|
|
|
|
fun providesConverterFactory(gson: Gson): Converter.Factory = GsonConverterFactory.create(gson)
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
@Singleton
|
2018-04-23 01:20:01 +10:00
|
|
|
fun providesHttpClient(accountManager: AccountManager,
|
|
|
|
context: Context): OkHttpClient {
|
|
|
|
return OkHttpUtils.getCompatibleClientBuilder(context)
|
2018-03-28 04:47:00 +11:00
|
|
|
.apply {
|
2018-04-23 01:20:01 +10:00
|
|
|
addInterceptor(InstanceSwitchAuthInterceptor(accountManager))
|
|
|
|
if (BuildConfig.DEBUG) {
|
2018-06-25 22:44:29 +10:00
|
|
|
addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
|
2018-03-28 04:47:00 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
@Singleton
|
|
|
|
fun providesRetrofit(httpClient: OkHttpClient,
|
|
|
|
converters: @JvmSuppressWildcards Set<Converter.Factory>): Retrofit {
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-15 08:05:08 +11:00
|
|
|
return Retrofit.Builder().baseUrl("https://" + MastodonApi.PLACEHOLDER_DOMAIN)
|
2018-03-28 04:47:00 +11:00
|
|
|
.client(httpClient)
|
|
|
|
.let { builder ->
|
|
|
|
// Doing it this way in case builder will be immutable so we return the final
|
|
|
|
// instance
|
|
|
|
converters.fold(builder) { b, c ->
|
|
|
|
b.addConverterFactory(c)
|
|
|
|
}
|
2018-10-04 05:27:52 +10:00
|
|
|
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
|
2018-03-28 04:47:00 +11:00
|
|
|
}
|
|
|
|
.build()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Provides
|
|
|
|
@Singleton
|
|
|
|
fun providesApi(retrofit: Retrofit): MastodonApi = retrofit.create(MastodonApi::class.java)
|
|
|
|
}
|