Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ItemPagerActivity.kt « ocreader « schaal « email « java « main « src « app - github.com/schaal/ocreader.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b317e7c9fd4b341d2b30e359e34f0589d5822c26 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (C) 2015-2016 Daniel Schaal <daniel@schaal.email>
 *
 * This file is part of OCReader.
 *
 * OCReader 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.
 *
 * OCReader 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 OCReader.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package email.schaal.ocreader

import android.animation.AnimatorSet
import android.animation.ArgbEvaluator
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.res.ColorStateList
import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import androidx.preference.PreferenceManager
import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.Keep
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.use
import androidx.core.graphics.ColorUtils
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import email.schaal.ocreader.ItemPageFragment.Companion.newInstance
import email.schaal.ocreader.database.PagerViewModel
import email.schaal.ocreader.database.model.Item
import email.schaal.ocreader.databinding.ActivityItemPagerBinding
import email.schaal.ocreader.util.FaviconLoader
import email.schaal.ocreader.util.FaviconLoader.FeedColorsListener
import email.schaal.ocreader.util.FeedColors
import java.util.*

class ItemPagerActivity : AppCompatActivity() {
    @ColorInt private var defaultToolbarColor = 0
    @ColorInt private var defaultAccent = 0

    private lateinit var binding: ActivityItemPagerBinding
    private lateinit var item: Item
    private lateinit var items: List<Item>

    private val pagerViewModel:PagerViewModel by viewModels()

    @SuppressLint("ResourceType")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_item_pager)
        setSupportActionBar(binding.bottomAppbar)

        try {
            pagerViewModel.updatePager()
            pagerViewModel.pager?.let {
                val preferences = PreferenceManager.getDefaultSharedPreferences(this)

                val order = Preferences.ORDER.getOrder(preferences)
                val sortField = Preferences.SORT_FIELD.getString(preferences)

                items = it.items?.sort(sortField ?: Item::id.name, order) ?: throw IllegalStateException("pager feed Items is null")
                binding.toolbarLayout.toolbar.title = it.name
            } ?: throw IllegalStateException("pager is null")

            val position = intent.getIntExtra(EXTRA_CURRENT_POSITION, 0)

            obtainStyledAttributes(intArrayOf(R.attr.colorPrimary, R.attr.colorAccent)).use {
                defaultToolbarColor = it.getColor(0, 0)
                defaultAccent = it.getColor(1, 0)
            }

            supportActionBar?.setDisplayHomeAsUpEnabled(true)

            val mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)
            binding.container.adapter = mSectionsPagerAdapter

            val pageChangeListener: OnPageChangeListener = MyOnPageChangeListener(mSectionsPagerAdapter)
            binding.container.addOnPageChangeListener(pageChangeListener)
            // The initial position is 0, so the pageChangeListener won't be called when setting the position to 0
            if (position == 0) pageChangeListener.onPageSelected(position)
            binding.container.setCurrentItem(position, false)

            binding.fabOpenInBrowser.setOnClickListener {
                if (item.url != null) {
                    startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(item.url)))
                }
            }
        } catch (e: IllegalStateException) {
            e.printStackTrace()
            setResult(Activity.RESULT_CANCELED)
            finish()
        }
    }

    private fun shareArticle() {
        if (item.url != null) {
            startActivity(Intent.createChooser(Intent(Intent.ACTION_SEND).apply {
                type = "text/plain"
                putExtra(Intent.EXTRA_TEXT, "${item.title} - ${item.url}")
            }, getString(R.string.share_article)))
        }
    }

    fun getItemForPosition(position: Int): Item {
        return items[position]
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_item_pager_bottom, menu)
        return true
    }

    override fun onPrepareOptionsMenu(menu: Menu): Boolean {
        menu.findItem(R.id.action_play_enclosure_media).isVisible = item.isPlayable()
        updateMenuItem(menu.findItem(R.id.menu_mark_read), !item.unread, R.drawable.ic_check_box, R.drawable.ic_check_box_outline_blank)
        updateMenuItem(menu.findItem(R.id.menu_mark_starred), item.starred, R.drawable.ic_star, R.drawable.ic_star_outline)
        return super.onPrepareOptionsMenu(menu)
    }

    private fun updateMenuItem(menuItem: MenuItem, value: Boolean, @DrawableRes checkedIcon: Int, @DrawableRes uncheckedIcon: Int) {
        menuItem.isChecked = value
        menuItem.setIcon(if (value) checkedIcon else uncheckedIcon)
    }

    fun updateResult() {
        val result = Intent()
        result.putExtra(EXTRA_CURRENT_POSITION, binding.container.currentItem)
        setResult(Activity.RESULT_OK, result)
    }

    override fun onBackPressed() {
        updateResult()
        super.onBackPressed()
    }

    override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {
        return when (menuItem.itemId) {
            android.R.id.home -> {
                updateResult()
                super.onOptionsItemSelected(menuItem)
            }
            R.id.action_play_enclosure_media -> {
                item.play(this)
                true
            }
            R.id.action_share_article -> {
                shareArticle()
                true
            }
            R.id.menu_mark_read -> {
                pagerViewModel.setItemUnread(!item.unread, item)
                updateMenuItem(menuItem, !item.unread, R.drawable.ic_check_box, R.drawable.ic_check_box_outline_blank)
                true
            }
            R.id.menu_mark_starred -> {
                pagerViewModel.setItemStarred(!item.starred, item)
                updateMenuItem(menuItem, item.starred, R.drawable.ic_star, R.drawable.ic_star_outline)
                true
            }
            else -> super.onOptionsItemSelected(menuItem)
        }
    }

    private inner class SectionsPagerAdapter internal constructor(fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
        private val fragments: WeakHashMap<Int, ItemPageFragment?>
        override fun getItem(position: Int): Fragment {
            val fragment: ItemPageFragment = fragments[position] ?: newInstance(getItemForPosition(position))

            if (fragments[position] == null)
                fragments[position] = fragment

            return fragment
        }

        override fun getCount(): Int {
            return items.size
        }

        init {
            fragments = WeakHashMap(count)
        }
    }

    private inner class StatusBarChanger {
        private val hsl = FloatArray(3)
        private fun changeLightness(backgroundColor: Int, lightnessChange: Float): Int {
            ColorUtils.colorToHSL(backgroundColor, hsl)
            hsl[2] *= lightnessChange
            return ColorUtils.HSLToColor(hsl)
        }

        @Keep
        fun setStatusBarColor(backgroundColor: Int) {
            val statusbarColor = changeLightness(backgroundColor, 0.7f)
            window.statusBarColor = statusbarColor

            binding.toolbarLayout.toolbar.setBackgroundColor(backgroundColor)
            binding.bottomAppbar.backgroundTint = ColorStateList.valueOf(backgroundColor)
        }
    }

    private inner class MyOnPageChangeListener internal constructor(private val mSectionsPagerAdapter: SectionsPagerAdapter) : OnPageChangeListener {
        private val DURATION = 250L

        private val currentNightMode: Int = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
        private val statusBarChanger: StatusBarChanger = StatusBarChanger()
        private var fabColorFrom = 0
        private var colorTo = defaultToolbarColor
        private var fabColorTo = defaultAccent
        private var colorFrom = 0
        private var progressFrom = 0f
        private var progressTo = 0f
        private val argbEvaluator = ArgbEvaluator()
        private var firstRun = true

        private val toListener: FeedColorsListener = object : FeedColorsListener {
            override fun onGenerated(feedColors: FeedColors) {
                colorTo = feedColors.getColor(FeedColors.Type.TEXT, defaultToolbarColor)
                fabColorTo = feedColors.getColor(FeedColors.Type.BACKGROUND, defaultAccent)
                if (firstRun) {
                    firstRun = false
                    if (currentNightMode == Configuration.UI_MODE_NIGHT_NO) statusBarChanger.setStatusBarColor(colorTo)
                    binding.fabOpenInBrowser.setBackgroundColor(fabColorTo)
                } else {
                    val fabAnimator = ObjectAnimator
                            .ofInt(binding.fabOpenInBrowser, "fabBackgroundColor", fabColorFrom, fabColorTo)
                    fabAnimator.setEvaluator(argbEvaluator)
                    val animatorSet = AnimatorSet()
                    val animatorSetBuilder = animatorSet.play(fabAnimator)
                    if (currentNightMode == Configuration.UI_MODE_NIGHT_NO) {
                        val statusBarAnimator = ObjectAnimator.ofInt(statusBarChanger, "statusBarColor", colorFrom, colorTo)
                        statusBarAnimator.setEvaluator(argbEvaluator)
                        animatorSetBuilder.with(statusBarAnimator)
                    }
                    animatorSet.duration = DURATION
                    animatorSet.start()
                }
            }

            override fun onStart() {
                colorFrom = colorTo
                fabColorFrom = fabColorTo
            }
        }

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
        override fun onPageSelected(position: Int) {
            invalidateOptionsMenu()
            item = getItemForPosition(position)
            pagerViewModel.setItemUnread(false, item)
            FaviconLoader.Builder(binding.fabOpenInBrowser)
                    .withGenerateFallbackImage(false)
                    .withPlaceholder(R.drawable.ic_open_in_browser)
                    .build()
                    .load(this@ItemPagerActivity, item.feed, toListener)
            progressFrom = progressTo
            progressTo = (position + 1).toFloat() / mSectionsPagerAdapter.count.toFloat()
            binding.bottomAppbar.performShow()
            binding.fabOpenInBrowser.show()
            ObjectAnimator
                    .ofFloat(binding.fabOpenInBrowser, "progress", progressFrom, progressTo)
                    .setDuration(DURATION)
                    .start()
        }

        override fun onPageScrollStateChanged(state: Int) {}
    }

    companion object {
        const val EXTRA_CURRENT_POSITION = "email.schaal.ocreader.extra.CURRENT_POSIION"
    }
}