Skip to content

Instantly share code, notes, and snippets.

View sasssass's full-sized avatar

Ali Shobeyri sasssass

View GitHub Profile
[
{
"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,
{"version":"0.1"}
@sasssass
sasssass / Post
Created May 19, 2020 20:36
Post Model Data
data class Post(
val body: String,
val id: Int,
val title: String,
val userId: Int
)
@sasssass
sasssass / webService
Created May 19, 2020 20:39
webService Interface
interface webService {
@GET("posts")
suspend fun getPosts() : MutableList<Post>
}
interface RemoteErrorEmitter {
fun onError(msg: String)
fun onError(errorType: ErrorType)
}
enum class ErrorType {
NETWORK, // IO
TIMEOUT, // Socket
UNKNOWN //Anything else
}
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 {
fun <T> apiCall(emitter: RemoteErrorEmitter,responseFunction: suspend () -> T) : LiveData<T?>{
return liveData {
val respone = privateApiCall(emitter,{responseFunction()})
emit(respone)
}
}
@sasssass
sasssass / apiCall.kt
Last active May 19, 2020 22:10
privateApiCall
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){
public interface ShapeBuilderInterface {
ShapeBuilderInterface addColor(int color);
ShapeBuilderInterface addName(String name);
}
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;