Skip to content

Commit

Permalink
Optimized binding of most viewholders
Browse files Browse the repository at this point in the history
  • Loading branch information
feelfreelinux committed Jan 30, 2018
1 parent a8cc387 commit dfab972
Show file tree
Hide file tree
Showing 41 changed files with 204 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,45 @@ import io.github.feelfreelinux.wykopmobilny.api.links.LinksRetrofitApi
import io.github.feelfreelinux.wykopmobilny.models.dataclass.Link
import io.github.feelfreelinux.wykopmobilny.models.mapper.apiv2.LinkMapper
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.LinkResponse
import io.github.feelfreelinux.wykopmobilny.utils.LinksPreferencesApi
import io.reactivex.Single
import retrofit2.Retrofit

class HitsRepository(val retrofit: Retrofit, val userTokenRefresher: UserTokenRefresher) : HitsApi {
class HitsRepository(val retrofit: Retrofit, val userTokenRefresher: UserTokenRefresher, val linksPreferencesApi: LinksPreferencesApi) : HitsApi {
private val hitsApi by lazy { retrofit.create(HitsRetrofitApi::class.java) }
override fun byMonth(year: Int, month: Int): Single<List<Link>> =
hitsApi.byMonth(year, month)
.retryWhen(userTokenRefresher)
.compose<List<LinkResponse>>(ErrorHandlerTransformer())
.map { it.map { LinkMapper.map(it) } }
.map { it.map { LinkMapper.map(it, linksPreferencesApi) } }


override fun currentDay(): Single<List<Link>> =
hitsApi.currentDay()
.retryWhen(userTokenRefresher)
.compose<List<LinkResponse>>(ErrorHandlerTransformer())
.map { it.map { LinkMapper.map(it) } }
.map { it.map { LinkMapper.map(it, linksPreferencesApi) } }



override fun byYear(year: Int): Single<List<Link>> =
hitsApi.byYear(year)
.retryWhen(userTokenRefresher)
.compose<List<LinkResponse>>(ErrorHandlerTransformer())
.map { it.map { LinkMapper.map(it) } }
.map { it.map { LinkMapper.map(it, linksPreferencesApi) } }


override fun currentWeek(): Single<List<Link>> =
hitsApi.currentWeek()
.retryWhen(userTokenRefresher)
.compose<List<LinkResponse>>(ErrorHandlerTransformer())
.map { it.map { LinkMapper.map(it) } }
.map { it.map { LinkMapper.map(it, linksPreferencesApi) } }


override fun popular(): Single<List<Link>> =
hitsApi.popular()
.retryWhen(userTokenRefresher)
.compose<List<LinkResponse>>(ErrorHandlerTransformer())
.map { it.map { LinkMapper.map(it) } }
.map { it.map { LinkMapper.map(it, linksPreferencesApi) } }

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,33 @@ import io.github.feelfreelinux.wykopmobilny.api.getRequestBody
import io.github.feelfreelinux.wykopmobilny.models.dataclass.*
import io.github.feelfreelinux.wykopmobilny.models.mapper.apiv2.*
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.*
import io.github.feelfreelinux.wykopmobilny.utils.LinksPreferencesApi
import io.reactivex.Single
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Retrofit
import java.lang.reflect.Type

class LinksRepository(val retrofit: Retrofit, val userTokenRefresher: UserTokenRefresher) : LinksApi {
class LinksRepository(val retrofit: Retrofit, val userTokenRefresher: UserTokenRefresher, val linksPreferencesApi: LinksPreferencesApi) : LinksApi {
private val linksApi by lazy { retrofit.create(LinksRetrofitApi::class.java) }

override fun getPromoted(page : Int): Single<List<Link>> =
linksApi.getPromoted(page)
.retryWhen(userTokenRefresher)
.compose<List<LinkResponse>>(ErrorHandlerTransformer())
.map { it.map { LinkMapper.map(it) } }
.map { it.map { LinkMapper.map(it, linksPreferencesApi) } }

override fun getUpcoming(page : Int, sortBy: String): Single<List<Link>> =
linksApi.getUpcoming(page, sortBy)
.retryWhen(userTokenRefresher)
.compose<List<LinkResponse>>(ErrorHandlerTransformer())
.map { it.map { LinkMapper.map(it) } }
.map { it.map { LinkMapper.map(it, linksPreferencesApi) } }

override fun getObserved(page : Int): Single<List<Link>> =
linksApi.getObserved(page)
.retryWhen(userTokenRefresher)
.compose<List<LinkResponse>>(ErrorHandlerTransformer())
.map { it.map { LinkMapper.map(it) } }
.map { it.map { LinkMapper.map(it, linksPreferencesApi) } }

override fun getLinkComments(linkId: Int, sortBy: String) =
linksApi.getLinkComments(linkId, sortBy)
Expand All @@ -53,7 +54,7 @@ class LinksRepository(val retrofit: Retrofit, val userTokenRefresher: UserTokenR
linksApi.getLink(linkId)
.retryWhen(userTokenRefresher)
.compose<LinkResponse>(ErrorHandlerTransformer())
.map { LinkMapper.map(it) }
.map { LinkMapper.map(it, linksPreferencesApi) }

override fun commentVoteUp(linkId: Int): Single<LinkVoteResponse> =
linksApi.commentVoteUp(linkId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@ import io.github.feelfreelinux.wykopmobilny.api.errorhandler.ErrorHandlerTransfo
import io.github.feelfreelinux.wykopmobilny.models.dataclass.EntryLink
import io.github.feelfreelinux.wykopmobilny.models.mapper.apiv2.EntryLinkMapper
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.EntryLinkResponse
import io.github.feelfreelinux.wykopmobilny.utils.LinksPreferencesApi
import io.reactivex.Single
import retrofit2.Retrofit

class MyWykopRepository(val retrofit: Retrofit, val userTokenRefresher: UserTokenRefresher) : MyWykopApi {
class MyWykopRepository(val retrofit: Retrofit, val userTokenRefresher: UserTokenRefresher, val linksPreferencesApi : LinksPreferencesApi) : MyWykopApi {
private val mywykopApi by lazy { retrofit.create(MyWykopRetrofitApi::class.java) }

override fun getIndex(page : Int): Single<List<EntryLink>> =
mywykopApi.getIndex(page)
.retryWhen(userTokenRefresher)
.compose<List<EntryLinkResponse>>(ErrorHandlerTransformer())
.map { it.map { EntryLinkMapper.map(it) } }
.map { it.map { EntryLinkMapper.map(it, linksPreferencesApi) } }

override fun byUsers(page : Int): Single<List<EntryLink>> =
mywykopApi.byUsers(page)
.retryWhen(userTokenRefresher)
.compose<List<EntryLinkResponse>>(ErrorHandlerTransformer())
.map { it.map { EntryLinkMapper.map(it) } }
.map { it.map { EntryLinkMapper.map(it, linksPreferencesApi) } }

override fun byTags(page : Int): Single<List<EntryLink>> =
mywykopApi.byTags(page)
.retryWhen(userTokenRefresher)
.compose<List<EntryLinkResponse>>(ErrorHandlerTransformer())
.map { it.map { EntryLinkMapper.map(it) } }
.map { it.map { EntryLinkMapper.map(it, linksPreferencesApi) } }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import io.github.feelfreelinux.wykopmobilny.models.mapper.apiv2.LinkMapper
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.AuthorResponse
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.EntryResponse
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.LinkResponse
import io.github.feelfreelinux.wykopmobilny.utils.LinksPreferencesApi
import retrofit2.Retrofit

class SearchRepository(val retrofit: Retrofit, val userTokenRefresher: UserTokenRefresher) : SearchApi {
class SearchRepository(val retrofit: Retrofit, val userTokenRefresher: UserTokenRefresher, val linksPreferencesApi: LinksPreferencesApi) : SearchApi {
private val searchApi by lazy { retrofit.create(SearchRetrofitApi::class.java) }

override fun searchLinks(page: Int, query: String) = searchApi
.searchLinks(page, query)
.retryWhen(userTokenRefresher)
.compose<List<LinkResponse>>(ErrorHandlerTransformer())
.map { it.map { LinkMapper.map(it) } }
.map { it.map { LinkMapper.map(it, linksPreferencesApi) } }

override fun searchEntries(page: Int, query: String) = searchApi
.searchEntries(page, query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.ObservedTag
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.TagStateResponse
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.responses.TagEntriesResponse
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.responses.TagLinksResponse
import io.github.feelfreelinux.wykopmobilny.utils.LinksPreferencesApi
import io.reactivex.Single
import retrofit2.Retrofit

class TagRepository(retrofit: Retrofit, val userTokenRefresher: UserTokenRefresher) : TagApi {
class TagRepository(retrofit: Retrofit, val userTokenRefresher: UserTokenRefresher, val linksPreferencesApi: LinksPreferencesApi) : TagApi {
private val tagApi by lazy { retrofit.create(TagRetrofitApi::class.java) }

override fun getTagEntries(tag : String, page : Int) = tagApi.getTagEntries(tag, page)
Expand All @@ -23,7 +24,7 @@ class TagRepository(retrofit: Retrofit, val userTokenRefresher: UserTokenRefresh
override fun getTagLinks(tag : String, page : Int) = tagApi.getTagLinks(tag, page)
.retryWhen(userTokenRefresher)
.flatMap(ErrorHandler<TagLinksResponse>())
.map { TagLinksMapper.map(it)}
.map { TagLinksMapper.map(it, linksPreferencesApi)}

override fun getObservedTags() : Single<List<ObservedTagResponse>> =
tagApi.getObservedTags()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import io.github.feelfreelinux.wykopmobilny.ui.modules.Navigator
import io.github.feelfreelinux.wykopmobilny.ui.modules.NavigatorApi
import io.github.feelfreelinux.wykopmobilny.ui.modules.notifications.WykopNotificationManager
import io.github.feelfreelinux.wykopmobilny.ui.modules.notifications.WykopNotificationManagerApi
import io.github.feelfreelinux.wykopmobilny.utils.ClipboardHelper
import io.github.feelfreelinux.wykopmobilny.utils.ClipboardHelperApi
import io.github.feelfreelinux.wykopmobilny.utils.SettingsPreferences
import io.github.feelfreelinux.wykopmobilny.utils.SettingsPreferencesApi
import io.github.feelfreelinux.wykopmobilny.utils.*
import io.github.feelfreelinux.wykopmobilny.utils.api.CredentialsPreferences
import io.github.feelfreelinux.wykopmobilny.utils.api.CredentialsPreferencesApi
import io.github.feelfreelinux.wykopmobilny.utils.usermanager.UserManager
Expand Down Expand Up @@ -63,6 +60,9 @@ class NetworkModule {
@Provides
fun provideSettingsPreferences(context: Context) : SettingsPreferencesApi = SettingsPreferences(context)

@Provides
fun provideLinksPreferencesApi(context: Context) : LinksPreferencesApi = LinksPreferences(context)

@Provides
fun provideUserManagerApi(credentialsPreferencesApi: CredentialsPreferencesApi) : UserManagerApi = UserManager(credentialsPreferencesApi)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import io.github.feelfreelinux.wykopmobilny.api.tag.TagApi
import io.github.feelfreelinux.wykopmobilny.api.tag.TagRepository
import io.github.feelfreelinux.wykopmobilny.api.user.LoginApi
import io.github.feelfreelinux.wykopmobilny.api.user.LoginRepository
import io.github.feelfreelinux.wykopmobilny.utils.LinksPreferencesApi
import io.github.feelfreelinux.wykopmobilny.utils.api.CredentialsPreferencesApi
import retrofit2.Retrofit

Expand All @@ -36,13 +37,13 @@ class RepositoryModule {
fun provideNotificationsApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher) : NotificationsApi = NotificationsRepository(retrofit, userTokenRefresher)

@Provides
fun provideMyWykopApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher) : MyWykopApi = MyWykopRepository(retrofit, userTokenRefresher)
fun provideMyWykopApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher, linksPreferencesApi: LinksPreferencesApi) : MyWykopApi = MyWykopRepository(retrofit, userTokenRefresher, linksPreferencesApi)

@Provides
fun provideLinksApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher) : LinksApi = LinksRepository(retrofit, userTokenRefresher)
fun provideLinksApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher, linksPreferencesApi: LinksPreferencesApi) : LinksApi = LinksRepository(retrofit, userTokenRefresher, linksPreferencesApi)

@Provides
fun provideTagApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher) : TagApi = TagRepository(retrofit, userTokenRefresher)
fun provideTagApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher, linksPreferencesApi: LinksPreferencesApi) : TagApi = TagRepository(retrofit, userTokenRefresher, linksPreferencesApi)

@Provides
fun provideUserApi(retrofit: Retrofit, credentialsPreferencesApi : CredentialsPreferencesApi) : LoginApi
Expand All @@ -55,8 +56,8 @@ class RepositoryModule {
fun provideSuggestApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher) : SuggestApi = SuggestRepository(retrofit, userTokenRefresher)

@Provides
fun provideSearchApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher) : SearchApi = SearchRepository(retrofit, userTokenRefresher)
fun provideSearchApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher, linksPreferencesApi: LinksPreferencesApi) : SearchApi = SearchRepository(retrofit, userTokenRefresher, linksPreferencesApi)

@Provides
fun provideHitsApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher) : HitsApi = HitsRepository(retrofit, userTokenRefresher)
fun provideHitsApi(retrofit: Retrofit, userTokenRefresher: UserTokenRefresher, linksPreferencesApi: LinksPreferencesApi) : HitsApi = HitsRepository(retrofit, userTokenRefresher, linksPreferencesApi)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package io.github.feelfreelinux.wykopmobilny.models.dataclass

data class Notification(
class Notification(
val id : Int,
val author : Author?,
val body : String,
val date : String,
val type : String,
val url : String?,
var new : Boolean
)
) {
override fun equals(other: Any?): Boolean {
return if (other !is Notification) false
else (other.id == id)
}

override fun hashCode(): Int {
return id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package io.github.feelfreelinux.wykopmobilny.models.mapper.apiv2
import io.github.feelfreelinux.wykopmobilny.models.dataclass.Conversation
import io.github.feelfreelinux.wykopmobilny.models.mapper.Mapper
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.ConversationResponse
import io.github.feelfreelinux.wykopmobilny.utils.toPrettyDate

class ConversationMapper {
companion object : Mapper<ConversationResponse, Conversation> {
override fun map(value: ConversationResponse): Conversation {
return Conversation(AuthorMapper.map(value.receiver), value.lastUpdate)
return Conversation(AuthorMapper.map(value.receiver), value.lastUpdate.toPrettyDate())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.feelfreelinux.wykopmobilny.models.mapper.apiv2
import io.github.feelfreelinux.wykopmobilny.models.dataclass.EntryComment
import io.github.feelfreelinux.wykopmobilny.models.mapper.Mapper
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.EntryCommentResponse
import io.github.feelfreelinux.wykopmobilny.utils.toPrettyDate

class EntryCommentMapper {
companion object : Mapper<EntryCommentResponse, EntryComment> {
Expand All @@ -12,7 +13,7 @@ class EntryCommentMapper {
0,
AuthorMapper.map(value.author),
value.body ?: "",
value.date,
value.date.toPrettyDate(),
value.userVote > 0,
if (value.embed != null) EmbedMapper.map(value.embed) else null,
value.voteCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package io.github.feelfreelinux.wykopmobilny.models.mapper.apiv2
import io.github.feelfreelinux.wykopmobilny.models.dataclass.EntryLink
import io.github.feelfreelinux.wykopmobilny.models.mapper.Mapper
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.EntryLinkResponse
import io.github.feelfreelinux.wykopmobilny.utils.LinksPreferencesApi

class EntryLinkMapper {
companion object : Mapper<EntryLinkResponse, EntryLink> {
override fun map(value: EntryLinkResponse): EntryLink {
companion object {
fun map(value: EntryLinkResponse, linksPreferencesApi : LinksPreferencesApi): EntryLink {
return EntryLink(
if (value.link != null) LinkMapper.map(value.link) else null,
if (value.link != null) LinkMapper.map(value.link, linksPreferencesApi) else null,
if (value.entry != null) EntryMapper.map(value.entry) else null
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package io.github.feelfreelinux.wykopmobilny.models.mapper.apiv2
import io.github.feelfreelinux.wykopmobilny.models.dataclass.Entry
import io.github.feelfreelinux.wykopmobilny.models.mapper.Mapper
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.EntryResponse
import io.github.feelfreelinux.wykopmobilny.utils.toPrettyDate

class EntryMapper {
companion object : Mapper<EntryResponse, Entry> {
override fun map(value: EntryResponse): Entry {
return Entry(value.id, AuthorMapper.map(value.author),
value.body ?: "", value.date,
value.body ?: "", value.date.toPrettyDate(),
value.userVote > 0, value.favorite,
if (value.survey != null) SurveyMapper.map(value.survey) else null,
if (value.embed != null) EmbedMapper.map(value.embed) else null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package io.github.feelfreelinux.wykopmobilny.models.mapper.apiv2
import io.github.feelfreelinux.wykopmobilny.models.dataclass.LinkComment
import io.github.feelfreelinux.wykopmobilny.models.mapper.Mapper
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.LinkCommentResponse
import io.github.feelfreelinux.wykopmobilny.utils.toPrettyDate

class LinkCommentMapper {
companion object : Mapper<LinkCommentResponse, LinkComment> {
override fun map(value: LinkCommentResponse): LinkComment {
return LinkComment(value.id, AuthorMapper.map(value.author), value.date,
return LinkComment(value.id, AuthorMapper.map(value.author), value.date.toPrettyDate(),
value.body, value.blocked,
value.favorite, value.voteCount,
value.voteCountPlus,
Expand Down
Loading

0 comments on commit dfab972

Please sign in to comment.