CategoryActivity.kt 8.01 KB
Newer Older
1
/*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 * Copyright (C) 2019-2021  E FOUNDATION
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17
 */

18
package foundation.e.apps.categories.category
Nihar Thakkar's avatar
Nihar Thakkar committed
19

20
import android.content.pm.PackageManager
narinder Rana's avatar
narinder Rana committed
21
import android.graphics.Color
Nihar Thakkar's avatar
Nihar Thakkar committed
22
23
import android.os.Bundle
import android.view.MenuItem
24
import android.view.View
25
import android.widget.LinearLayout
26
import android.widget.ProgressBar
27
import android.widget.RelativeLayout
28
import android.widget.TextView
Arnau Vàzquez's avatar
Arnau Vàzquez committed
29
30
31
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.Observer
32
import androidx.lifecycle.ViewModelProvider
Arnau Vàzquez's avatar
Arnau Vàzquez committed
33
34
35
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
36
37
38
39
40
41
42
43
44
45
import foundation.e.apps.R
import foundation.e.apps.application.model.Application
import foundation.e.apps.applicationmanager.ApplicationManager
import foundation.e.apps.applicationmanager.ApplicationManagerServiceConnection
import foundation.e.apps.applicationmanager.ApplicationManagerServiceConnectionCallback
import foundation.e.apps.categories.category.viewmodel.CategoryViewModel
import foundation.e.apps.categories.model.Category
import foundation.e.apps.common.ApplicationListAdapter
import foundation.e.apps.utils.Constants
import foundation.e.apps.utils.Constants.CATEGORY_KEY
46
import kotlinx.android.synthetic.main.activity_category.*
narinder Rana's avatar
narinder Rana committed
47
import kotlinx.android.synthetic.main.error_layout.*
Nihar Thakkar's avatar
Nihar Thakkar committed
48

49
class CategoryActivity : AppCompatActivity(), ApplicationManagerServiceConnectionCallback {
Nihar Thakkar's avatar
Nihar Thakkar committed
50
51

    private lateinit var category: Category
52
    private lateinit var categoryViewModel: CategoryViewModel
53
54
    private lateinit var recyclerView: RecyclerView
    private lateinit var progressBar: ProgressBar
55
56
    private val applicationManagerServiceConnection =
            ApplicationManagerServiceConnection(this)
Nihar Thakkar's avatar
Nihar Thakkar committed
57
    private var applicationList = ArrayList<Application>()
58
    private var isLoadingMoreApplications = false
narinder Rana's avatar
narinder Rana committed
59
    var accentColorOS=0;
Arnau Vàzquez's avatar
Arnau Vàzquez committed
60
61


Nihar Thakkar's avatar
Nihar Thakkar committed
62
63
64
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_category)
65
       getAccentColor()
Nihar Thakkar's avatar
Nihar Thakkar committed
66
67
68
69

        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
Arnau Vàzquez's avatar
Arnau Vàzquez committed
70
        val extras = intent.extras
71
        category = intent.getSerializableExtra(CATEGORY_KEY) as Category
72
        supportActionBar?.title = category.getTitle()
73

74
        categoryViewModel = ViewModelProvider(this).get(CategoryViewModel::class.java)
75
        recyclerView = findViewById(R.id.app_list)
76
        val loadMoreContainer = findViewById<RelativeLayout>(R.id.load_more_container)
77
        progressBar = findViewById(R.id.progress_bar)
78
79
80
        val errorContainer = findViewById<LinearLayout>(R.id.error_container)
        val errorDescription = findViewById<TextView>(R.id.error_description)

narinder Rana's avatar
narinder Rana committed
81
82
83
84
85
        //set accent color to Error button (Retry )
        findViewById<TextView>(R.id.error_resolve).setTextColor(Color.parseColor("#ffffff"))
        findViewById<TextView>(R.id.error_resolve).setBackgroundColor(accentColorOS)


86
87
        // Initialise UI elements
        recyclerView.visibility = View.GONE
88
89
        loadMoreContainer.visibility = View.GONE
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
Nihar Thakkar's avatar
Nihar Thakkar committed
90
91
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                if (!recyclerView.canScrollVertically(1)) {
92
93
                    loadMoreContainer.visibility = View.VISIBLE
                    recyclerView.scrollToPosition(applicationList.size - 1)
94
95
96
97
                    if (!isLoadingMoreApplications) {
                        isLoadingMoreApplications = true
                        categoryViewModel.loadApplications(this@CategoryActivity)
                    }
98
99
                } else {
                    loadMoreContainer.visibility = View.GONE
Nihar Thakkar's avatar
Nihar Thakkar committed
100
101
                }
            }
Arnau Vàzquez's avatar
Arnau Vàzquez committed
102

Nihar Thakkar's avatar
Nihar Thakkar committed
103
        })
104
        progressBar.visibility = View.VISIBLE
105
106
        errorContainer.visibility = View.GONE
        findViewById<TextView>(R.id.error_resolve).setOnClickListener {
107
            loadMoreContainer.visibility = View.GONE
108
109
110
            progressBar.visibility = View.VISIBLE
            categoryViewModel.loadApplications(this)
        }
111
112
113
114

        // Initialise recycler view
        recyclerView.setHasFixedSize(true)
        recyclerView.layoutManager = LinearLayoutManager(this)
115
        recyclerView.adapter = ApplicationListAdapter(this, applicationList, accentColorOS)
116
117

        // Bind to the list of applications in this activity's category
118
        categoryViewModel.getApplications().observe(this, Observer {
119
            if (it != null) {
Nihar Thakkar's avatar
Nihar Thakkar committed
120
121
                applicationList.clear()
                applicationList.addAll(it)
122
                progressBar.visibility = View.GONE
Arnau Vàzquez's avatar
Arnau Vàzquez committed
123
                recyclerView.adapter?.notifyDataSetChanged()
124
                recyclerView.visibility = View.VISIBLE
125
                loadMoreContainer.visibility = View.GONE
126
                isLoadingMoreApplications = false
127
            }
128
        })
129
130
131
132

        // Bind to the screen error
        categoryViewModel.getScreenError().observe(this, Observer {
            if (it != null) {
133
                if (!isLoadingMoreApplications) {
134
135
136
137
138
                    errorDescription.text = getString(it.description)
                    errorContainer.visibility = View.VISIBLE
                    progressBar.visibility = View.GONE
                    loadMoreContainer.visibility = View.GONE
                } else {
Nihar Thakkar's avatar
Nihar Thakkar committed
139
                    loadMoreContainer.visibility = View.GONE
140
141
                    isLoadingMoreApplications = false
                }
142
143
144
145
146
            } else {
                errorContainer.visibility = View.GONE
            }
        })

147
148
        applicationManagerServiceConnection.bindService(this)
    }
149

150
151
    override fun onServiceBind(applicationManager: ApplicationManager) {
        categoryViewModel.initialise(applicationManager, category.id)
152
153
154
        if (categoryViewModel.getApplications().value == null) {
            categoryViewModel.loadApplications(this)
        }
Nihar Thakkar's avatar
Nihar Thakkar committed
155
156
157
158
159
160
161
162
163
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            android.R.id.home ->
                finish()
        }
        return true
    }
164

165
166
167
168
169
170
171
172
173
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
                                            grantResults: IntArray) {
        if (requestCode == Constants.STORAGE_PERMISSION_REQUEST_CODE &&
                grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_DENIED) {
            Snackbar.make(container, R.string.error_storage_permission_denied,
                    Snackbar.LENGTH_LONG).show()
        }
    }

174
175
176
    override fun onResume() {
        super.onResume()
        if (::categoryViewModel.isInitialized) {
177
178
179
180
            categoryViewModel.getApplications().value?.let {
                it.forEach { application ->
                    application.checkForStateUpdate(this)
                }
181
182
183
184
            }
        }
    }

185
186
    override fun onDestroy() {
        super.onDestroy()
187
188
189
190
        categoryViewModel.getApplications().value?.let {
            it.forEach { application ->
                application.decrementUses()
            }
Nihar Thakkar's avatar
Nihar Thakkar committed
191
        }
192
        applicationManagerServiceConnection.unbindService(this)
193
    }
narinder Rana's avatar
narinder Rana committed
194
195
196
197
198
199
200

    /*
   * get Accent color from OS
   *
   *  */
    private fun getAccentColor() {

narinder Rana's avatar
narinder Rana committed
201
        accentColorOS=this.resources.getColor(R.color.colorAccent);
narinder Rana's avatar
narinder Rana committed
202
    }
Nihar Thakkar's avatar
Nihar Thakkar committed
203
}