forked from nickbutcher/plaid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert some more core classes to kotlin
- Loading branch information
1 parent
421b28a
commit d81d653
Showing
10 changed files
with
190 additions
and
216 deletions.
There are no files selected for viewing
30 changes: 0 additions & 30 deletions
30
core/src/main/java/io/plaidapp/core/data/DataLoadingSubject.java
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
core/src/main/java/io/plaidapp/core/data/DataLoadingSubject.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 0 additions & 83 deletions
83
core/src/main/java/io/plaidapp/core/data/api/DeEnvelopingConverter.java
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
core/src/main/java/io/plaidapp/core/data/api/DeEnvelopingConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
core/src/main/java/io/plaidapp/core/data/api/EnvelopePayload.java
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
core/src/main/java/io/plaidapp/core/data/api/EnvelopePayload.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
core/src/main/java/io/plaidapp/core/data/pocket/PocketUtils.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.