-
Notifications
You must be signed in to change notification settings - Fork 1
/
UpcomingPresenter.kt
124 lines (84 loc) · 3.58 KB
/
UpcomingPresenter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.demo.themoviedb.presenter
import com.demo.themoviedb.MyApp
import com.demo.themoviedb.data.MoviesDatabase
import com.demo.themoviedb.entity.MoviesResponse
import com.demo.themoviedb.interactor.UpcomingIntegrator
import com.demo.themoviedb.utils.NetworkUtils
import com.demo.themoviedb.view.acitivty.DetailActivity
import com.demo.themoviedb.view.contract.UpcomingContract
import com.github.kittinunf.result.Result
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import ru.terrakok.cicerone.Router
class UpcomingPresenter(private var view: UpcomingContract.View?) : UpcomingContract.Presenter,
UpcomingContract.InteractorOutput {
private val router:Router? by lazy { MyApp.INSTANCE.cicerone.router }
private var interactor: UpcomingContract.Interactor? = UpcomingIntegrator()
override fun listItemClicked(movie: MoviesResponse.Result?) {
router?.navigateTo(DetailActivity.TAG, movie)
}
override fun onViewCreated() {
view?.showLoading()
//Check internet connections
if (NetworkUtils.isOnline()){
interactor?.fetchPopularMovies(pageCount = 1) { result ->
when (result) {
is Result.Success -> {
val jokesJsonObject = result.get().obj()
//delete the local db data if api call successfully
view?.deleteTable()
val type = object : TypeToken<List<MoviesResponse.Result>>() {}.type
val moviesList: List<MoviesResponse.Result> =
Gson().fromJson(jokesJsonObject.getJSONArray("results").toString(), type)
//add response to local db for persistent storage
view?.addToTable(moviesList)
this.onResultSuccess(moviesList)
}
is Result.Failure -> {
val ex = result.getException()
println(ex)
this.onResultFailed()
}
}
}
}else{
view?.hideLoading()
//Get the data from local db if user is not connected to internet
view?.getFromTable()
}
}
override fun onDestroyed() {
interactor = null
view = null
}
// call this method if user scroll to more than one page
override fun onLoadNextPage(pageCount: Int) {
if (pageCount!=1)
interactor?.fetchPopularMovies(pageCount = pageCount) { result ->
when (result) {
is Result.Success -> {
val jokesJsonObject = result.get().obj()
val type = object : TypeToken<List<MoviesResponse.Result>>() {}.type
val moviesList: List<MoviesResponse.Result> =
Gson().fromJson(jokesJsonObject.getJSONArray("results").toString(), type)
//add response to local db for persistent storage
view?.addToTable(moviesList)
this.onResultSuccess(moviesList)
}
is Result.Failure -> {
val ex = result.getException()
println(ex)
this.onResultFailed()
}
}
}
}
override fun onResultSuccess(data: List<MoviesResponse.Result>) {
view?.hideLoading()
view?.publishData(data)
}
override fun onResultFailed() {
view?.hideLoading()
view?.showInfo("Error please try again")
}
}