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
[ | |
{ | |
"albumId": 1, | |
"id": 1, | |
"title": "Infected Mushroom", | |
"url": "https://pbs.twimg.com/media/DqyMicTVYAEpfzs.jpg", | |
"thumbnailUrl": "https://pbs.twimg.com/media/DqyMicTVYAEpfzs.jpg" | |
}, | |
{ | |
"albumId": 1, |
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
{"version":"0.1"} |
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
data class Post( | |
val body: String, | |
val id: Int, | |
val title: String, | |
val userId: Int | |
) |
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
interface webService { | |
@GET("posts") | |
suspend fun getPosts() : MutableList<Post> | |
} |
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
interface RemoteErrorEmitter { | |
fun onError(msg: String) | |
fun onError(errorType: ErrorType) | |
} | |
enum class ErrorType { | |
NETWORK, // IO | |
TIMEOUT, // Socket | |
UNKNOWN //Anything else | |
} |
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
class MainActivity : AppCompatActivity() , RemoteErrorEmitter { | |
val webService : webService = RetrofitBuilder.api | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
var thePosts = apiCall<MutableList<Post>>(this,{webService.getPosts()}).observe( | |
this, | |
Observer { |
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
fun <T> apiCall(emitter: RemoteErrorEmitter,responseFunction: suspend () -> T) : LiveData<T?>{ | |
return liveData { | |
val respone = privateApiCall(emitter,{responseFunction()}) | |
emit(respone) | |
} | |
} |
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
private suspend fun <T> privateApiCall(emitter: RemoteErrorEmitter, responseFunction: suspend () -> T): T? { | |
try{ | |
return withTimeout(5000){ | |
responseFunction() | |
} | |
}catch (e: Exception){ | |
withContext(Dispatchers.Main){ | |
e.printStackTrace() | |
Log.e("ApiCalls", "Call error: ${e.localizedMessage}", e.cause) | |
when(e){ |
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
public interface ShapeBuilderInterface { | |
ShapeBuilderInterface addColor(int color); | |
ShapeBuilderInterface addName(String name); | |
} |
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
public final class Shape { | |
public float perimeter; | |
public float area; | |
public int color; | |
public String name; | |
private Shape(ShapeBuilder builder) { | |
perimeter = builder.perimeter; | |
area = builder.area; |
OlderNewer