Guest User

Untitled

a guest
Jan 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. fun addPost(): Unit {
  2. viewModel.addPost(dataSource.text.get(), dataSource.title.get(), { postID: Long ->
  3. if (postID.equals(0)) {
  4. Toast.makeText(MyApp.instance.getContext(), "Error", Toast.LENGTH_LONG).show()
  5. } else {
  6. (arguments.get(ARG_RunOnPositiveDismiss) as (DMPost) -> Unit)(DMPost(postID, MyOptions.User.GET()!!.UserID, dataSource.title.get(), dataSource.text.get()))
  7. dismiss()
  8. }
  9. })
  10. }
  11.  
  12. fun addPost(title:String,text:String,onSuccess: (Long) -> Unit): Unit {
  13. rep.insertPost(DMPost(0, MyOptions.User.GET()!!.UserID, title, text),onSuccess)
  14. }
  15.  
  16. fun insertPost(post: DMPost, onSuccess: (Long) -> Unit) {
  17. if (MyOptions.Online.GET()!!) {
  18. val client = retrofit.create(APIPost::class.java)
  19. val insertPost: Call<Long> = client.insertPost(post)
  20. insertPost.enqueue(object : Callback<Long> {
  21. override fun onResponse(call: Call<Long>, response: Response<Long>) {
  22. if (response.isSuccessful) {
  23. onSuccess(response.body()!!)
  24. } else {
  25. onSuccess(0)
  26. }
  27. }
  28.  
  29. override fun onFailure(call: Call<Long>, t: Throwable) {
  30. onSuccess(0)
  31. }
  32. })
  33.  
  34. } else {
  35. AsyncTask.execute {
  36. try {
  37. onSuccess(daoPost.insertPost(EntityPost(post)))
  38. } catch (e: Exception) {
  39. onSuccess(0)
  40. }
  41. }
  42. }
  43. }
Add Comment
Please, Sign In to add comment