@@ -2,6 +2,7 @@ package com.developers.mvpsample.ui.main
2
2
3
3
import android.support.v7.app.AppCompatActivity
4
4
import android.os.Bundle
5
+ import android.os.PersistableBundle
5
6
import android.support.design.widget.Snackbar
6
7
import android.support.v7.widget.LinearLayoutManager
7
8
import android.view.View
@@ -12,14 +13,25 @@ import com.developers.mvpsample.data.Result
12
13
import com.developers.mvpsample.di.component.DaggerActivityComponent
13
14
import com.developers.mvpsample.di.module.ActivityModule
14
15
import com.developers.mvpsample.ui.adapter.MovieAdapter
16
+ import com.google.gson.Gson
17
+ import com.google.gson.reflect.TypeToken
15
18
import kotlinx.android.synthetic.main.activity_main.*
19
+ import java.util.logging.Logger
16
20
import javax.inject.Inject
17
21
18
22
19
23
class MainActivity : AppCompatActivity (), MainView {
20
24
21
25
@Inject
22
26
lateinit var mainPresenter: MainMvpPresenter <MainView >
27
+ var resultJSON: String? = null
28
+ private val RESULT = " resultJson"
29
+ private val gson = Gson ()
30
+
31
+
32
+ companion object {
33
+ val log = Logger .getLogger(MainActivity ::class .java.name)
34
+ }
23
35
24
36
override fun onCreate (savedInstanceState : Bundle ? ) {
25
37
super .onCreate(savedInstanceState)
@@ -33,6 +45,32 @@ class MainActivity : AppCompatActivity(), MainView {
33
45
setListeners()
34
46
}
35
47
48
+ override fun onSaveInstanceState (outState : Bundle ? ) {
49
+ super .onSaveInstanceState(outState)
50
+ outState?.putString(RESULT , resultJSON)
51
+ }
52
+
53
+ override fun onRestoreInstanceState (savedInstanceState : Bundle ? ) {
54
+ super .onRestoreInstanceState(savedInstanceState)
55
+ val result = savedInstanceState?.getString(RESULT )
56
+ val movieList = gson.fromJson<List <Result >>(result, object : TypeToken <List <Result >>() {
57
+ }.type)
58
+ if (movieList != null ) {
59
+ setupRecyclerView(movieList)
60
+ resultJSON = gson.toJson(movieList)
61
+ }
62
+ }
63
+
64
+ private fun setupRecyclerView (movieResult : List <Result >) {
65
+ val linearLayoutManager = LinearLayoutManager (applicationContext)
66
+ linearLayoutManager.orientation = LinearLayoutManager .VERTICAL
67
+ val movieAdapter = MovieAdapter (movieResult, applicationContext)
68
+ with (movie_recycler_view) {
69
+ layoutManager = linearLayoutManager
70
+ adapter = movieAdapter
71
+ }
72
+ }
73
+
36
74
private fun setListeners () {
37
75
search_button.setOnClickListener({
38
76
mainPresenter.searchMovieQuery(query_edit_text.text.toString()
@@ -50,6 +88,7 @@ class MainActivity : AppCompatActivity(), MainView {
50
88
}
51
89
52
90
override fun showData (movieResult : List <Result >) {
91
+ resultJSON = gson.toJson(movieResult)
53
92
val linearLayoutManager = LinearLayoutManager (applicationContext)
54
93
linearLayoutManager.orientation = LinearLayoutManager .VERTICAL
55
94
val movieAdapter = MovieAdapter (movieResult, applicationContext)
0 commit comments