BannerApplicationLoader.kt 2.33 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.model
19

Arnau Vàzquez's avatar
Arnau Vàzquez committed
20
import android.graphics.BitmapFactory
21
import android.os.AsyncTask
Arnau Vàzquez's avatar
Arnau Vàzquez committed
22
23
import foundation.e.apps.MainActivity.Companion.mActivity
import foundation.e.apps.R
24
25
import foundation.e.apps.application.model.Application
import foundation.e.apps.utils.ImagesLoader
26

27
class BannerApplicationLoader(private val apps: ArrayList<Application>, private val homeModel: HomeModel) : AsyncTask<Any, Any, ArrayList<BannerApplication>>() {
28

29
30
    override fun doInBackground(vararg params: Any): ArrayList<BannerApplication> {
        return loadBannerImages()
31
32
33
34
35
36
    }

    override fun onPostExecute(result: ArrayList<BannerApplication>) {
        homeModel.bannerApplications.value = result
    }

37
    private fun loadBannerImages(): ArrayList<BannerApplication> {
38
        val bannerApplications = ArrayList<BannerApplication>()
Arnau Vàzquez's avatar
Arnau Vàzquez committed
39
40
41
42
43
44
45
46
47
48
49
50

//      Show App logo if "Show only open Source apps"  or "Show pwa apps " condition is checked
        if(apps.isEmpty()){
            val icon = BitmapFactory.decodeResource(mActivity.getResources(),
                R.mipmap.ic_launcher2)
                bannerApplications.add(BannerApplication(null, icon))
        }
        else{
            apps.forEach { application ->
                if (application.basicData!!.imagesUri.isNotEmpty()) {
                    val image = Array(1) { application.basicData!!.imagesUri[0] }
                    ImagesLoader(image).loadImages().forEach {
51
                    bannerApplications.add(BannerApplication(application, it))
Arnau Vàzquez's avatar
Arnau Vàzquez committed
52
53
54
                    }
                } else {
                    application.decrementUses()
55
                }
jo's avatar
jo committed
56
            }
57
        }
58
        return bannerApplications
59
60
61
    }

}