HomeFragment.kt 7.16 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.home
19

Arnau Vàzquez's avatar
Arnau Vàzquez committed
20
21
//import java.awt.font.ShapeGraphicAttribute.STROKE
//import java.awt.AlphaComposite.SRC_IN
narinder Rana's avatar
narinder Rana committed
22
import android.graphics.Color
23
24
25
26
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
27
import android.widget.LinearLayout
28
import android.widget.ProgressBar
29
import android.widget.TextView
Arnau Vàzquez's avatar
Arnau Vàzquez committed
30
31
import androidx.fragment.app.Fragment
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 androidx.viewpager.widget.ViewPager
36
37
38
39
40
41
import foundation.e.apps.R
import foundation.e.apps.application.model.Application
import foundation.e.apps.applicationmanager.ApplicationManager
import foundation.e.apps.categories.model.Category
import foundation.e.apps.common.SmallApplicationListAdapter
import foundation.e.apps.home.viewmodel.HomeViewModel
42

Arnau Vàzquez's avatar
Arnau Vàzquez committed
43

44
class HomeFragment : Fragment() {
45
46
    private lateinit var homeViewModel: HomeViewModel
    private lateinit var imageCarousel: ViewPager
Nihar Thakkar's avatar
Nihar Thakkar committed
47
    private lateinit var divider: View
48
    private lateinit var categoryList: LinearLayout
49
    private lateinit var progressBar: ProgressBar
50
    private var applicationManager: ApplicationManager? = null
narinder Rana's avatar
narinder Rana committed
51
    var accentColorOS=0;
52

narinder Rana's avatar
narinder Rana committed
53
    fun initialise(applicationManager: ApplicationManager, accentColorOS: Int) {
54
        this.applicationManager = applicationManager
narinder Rana's avatar
narinder Rana committed
55
        this.accentColorOS=accentColorOS;
56
    }
57

58
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
59
        if (applicationManager == null) {
Nihar Thakkar's avatar
Nihar Thakkar committed
60
            return null
Arnau Vàzquez's avatar
Arnau Vàzquez committed
61

Nihar Thakkar's avatar
Nihar Thakkar committed
62
63
        }

64
        val view = inflater.inflate(R.layout.fragment_home, container, false)
narinder Rana's avatar
narinder Rana committed
65
66
67
68
       /* if(accentColorOS!=0){

            view.findViewById<TextView>(R.id.tv_featured).setTextColor(accentColorOS);
        }*/
69

70
        homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
71
        imageCarousel = view.findViewById(R.id.image_carousel)
Nihar Thakkar's avatar
Nihar Thakkar committed
72
        divider = view.findViewById(R.id.divider)
73
        categoryList = view.findViewById(R.id.category_list)
74
        progressBar = view.findViewById(R.id.progress_bar)
75
76
        val errorContainer = view.findViewById<LinearLayout>(R.id.error_container)
        val errorDescription = view.findViewById<TextView>(R.id.error_description)
narinder Rana's avatar
narinder Rana committed
77
78
79
80
        //set accent color to Error button (Retry )

        view.findViewById<TextView>(R.id.error_resolve).setTextColor(Color.parseColor("#ffffff"))
        view.findViewById<TextView>(R.id.error_resolve).setBackgroundColor(accentColorOS)
81

82
        // Initialise UI elements
83
        homeViewModel.initialise(applicationManager!!)
84
        setCustomScroller()
85
        imageCarousel.visibility = View.INVISIBLE
Nihar Thakkar's avatar
Nihar Thakkar committed
86
        divider.visibility = View.INVISIBLE
87
88
        categoryList.visibility = View.GONE
        progressBar.visibility = View.VISIBLE
89
90
91
92
93
        errorContainer.visibility = View.GONE
        view.findViewById<TextView>(R.id.error_resolve).setOnClickListener {
            progressBar.visibility = View.VISIBLE
            homeViewModel.loadCategories(context!!)
        }
94

95
96
97
98
        // Bind image carousel adapter to banner images in view model
        homeViewModel.getBannerApplications().observe(this, Observer {
            if (homeViewModel.getBannerApplications().value!!.isNotEmpty()) {
                imageCarousel.adapter = ImageCarouselAdapter(activity!!, homeViewModel.getBannerApplications().value!!)
Arnau Vàzquez's avatar
Arnau Vàzquez committed
99
100
101
                imageCarousel.clipToPadding = false;
                imageCarousel.setPadding(170, 10, 170, 10);
                imageCarousel.pageMargin =50
102
103
104
105
106
                imageCarousel.setCurrentItem(0, false)
                imageCarousel.visibility = View.VISIBLE
                // Automatically switch between images for one round
                ImageCarouselSwitcher(homeViewModel.getBannerApplications().value!!.size, imageCarousel).start()
            }
107
108
        })

109
110
111
        // Bind categories adapter to categories in view model
        homeViewModel.getCategories().observe(this, Observer {
            if (homeViewModel.getCategories().value!!.isNotEmpty()) {
112
                showCategories(it!!)
113
                categoryList.visibility = View.VISIBLE
Nihar Thakkar's avatar
Nihar Thakkar committed
114
                divider.visibility = View.VISIBLE
115
116
                progressBar.visibility = View.GONE
            }
117
        })
118

119
120
121
        // Bind to the screen error
        homeViewModel.getScreenError().observe(this, Observer {
            if (it != null) {
122
                errorDescription.text = activity!!.getString(it.description)
123
124
125
126
127
128
129
                errorContainer.visibility = View.VISIBLE
                progressBar.visibility = View.GONE
            } else {
                errorContainer.visibility = View.GONE
            }
        })

130
131
132
133
        if (homeViewModel.getBannerApplications().value!!.isEmpty() ||
                homeViewModel.getCategories().value!!.isEmpty()) {
            homeViewModel.loadCategories(context!!)
        }
134
135
136
        return view
    }

137
    private fun setCustomScroller() {
jo's avatar
jo committed
138
139
140
        val scroller = ViewPager::class.java.getDeclaredField("mScroller")
        scroller.isAccessible = true
        scroller.set(imageCarousel, ImageCarouselScroller(context!!))
141
    }
Nihar Thakkar's avatar
Nihar Thakkar committed
142

143
    private fun showCategories(categories: LinkedHashMap<Category, ArrayList<Application>>) {
144
145
146
147
148
149
150
151
152
153
        categoryList.removeAllViews()
        categories.forEach {
            val homeCategory = HomeCategory(context!!, it.key)
            val applicationList = homeCategory.findViewById<RecyclerView>(R.id.application_list)
            applicationList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            applicationList.adapter = SmallApplicationListAdapter(activity!!, it.value)
            categoryList.addView(homeCategory)
        }
    }

154
155
156
157
158
    override fun onResume() {
        super.onResume()
        if (::homeViewModel.isInitialized) {
            homeViewModel.getCategories().value!!.values.forEach {
                it.forEach { application ->
159
                    application.checkForStateUpdate(context!!)
160
161
162
163
164
                }
            }
        }
    }

165
166
    fun decrementApplicationUses() {
        if (::homeViewModel.isInitialized) {
167
168
169
170
171
            homeViewModel.getCategories().value!!.forEach {
                it.value.forEach { application ->
                    application.decrementUses()
                }
            }
Nihar Thakkar's avatar
Nihar Thakkar committed
172
            homeViewModel.getBannerApplications().value!!.forEach {
Arnau Vàzquez's avatar
Arnau Vàzquez committed
173
                if(it.application!=null)
174
                    it.application.decrementUses()
Nihar Thakkar's avatar
Nihar Thakkar committed
175
            }
Nihar Thakkar's avatar
Nihar Thakkar committed
176
177
        }
    }
178
}