Skip to content

Commit

Permalink
Convert some more core classes to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
keyboardsurfer committed Dec 3, 2019
1 parent 421b28a commit d81d653
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 216 deletions.
30 changes: 0 additions & 30 deletions core/src/main/java/io/plaidapp/core/data/DataLoadingSubject.java

This file was deleted.

29 changes: 29 additions & 0 deletions core/src/main/java/io/plaidapp/core/data/DataLoadingSubject.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2019 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.core.data

/**
* An interface for classes offering data loading state to be observed.
*/
interface DataLoadingSubject {
fun registerCallback(callbacks: DataLoadingCallbacks)

interface DataLoadingCallbacks {
fun dataStartedLoading()
fun dataFinishedLoading()
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/io/plaidapp/core/data/PlaidItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package io.plaidapp.core.data

/**
* Base class for all model types.
* // TODO - make the item immutable
* // TODO - make the item immutable (https://github.com/android/plaid/issues/795)
*/
abstract class PlaidItem(
@Transient open val id: Long,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2019 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.core.data.api

import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.reflect.TypeToken
import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type
import javax.inject.Inject

/**
* A [retrofit2.Converter.Factory] which removes unwanted wrapping envelopes from API responses.
*/
class DeEnvelopingConverter @Inject constructor(internal val gson: Gson) : Converter.Factory() {

override fun responseBodyConverter(
type: Type,
annotations: Array<Annotation>,
retrofit: Retrofit
): Converter<ResponseBody, Any>? {

// This converter requires an annotation providing the name of the payload in the envelope;
// if one is not supplied then return null to continue down the converter chain.
val payloadName = getPayloadName(annotations) ?: return null

val adapter: TypeAdapter<*> = gson.getAdapter(TypeToken.get(type))
return Converter { body: ResponseBody ->
gson.newJsonReader(body.charStream()).use { jsonReader ->
jsonReader.beginObject()
while (jsonReader.hasNext()) {
if (payloadName == jsonReader.nextName()) {
return@use adapter.read(jsonReader)
} else {
jsonReader.skipValue()
}
}
return@use null
}
}
}

private fun getPayloadName(annotations: Array<Annotation>?): String? {
val annotation = annotations?.firstOrNull { it is EnvelopePayload }
return if (annotation != null) {
(annotation as EnvelopePayload).value
} else {
null
}
}
}
34 changes: 0 additions & 34 deletions core/src/main/java/io/plaidapp/core/data/api/EnvelopePayload.java

This file was deleted.

31 changes: 31 additions & 0 deletions core/src/main/java/io/plaidapp/core/data/api/EnvelopePayload.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.core.data.api

import kotlin.annotation.AnnotationRetention.RUNTIME

/**
* An annotation for identifying the payload that we want to extract from an API response wrapped in
* an envelope object.
*/
@Target(
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER
)
@Retention(RUNTIME)
annotation class EnvelopePayload(val value: String = "")
67 changes: 0 additions & 67 deletions core/src/main/java/io/plaidapp/core/data/pocket/PocketUtils.java

This file was deleted.

Loading

0 comments on commit d81d653

Please sign in to comment.