Skip to content

Commit 2836264

Browse files
author
Ink
committed
+ NotificationsActivity
1 parent b8ab3d8 commit 2836264

File tree

4 files changed

+120
-29
lines changed

4 files changed

+120
-29
lines changed

glass-ee/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@
7575
<category android:name="android.intent.category.DEFAULT" />
7676
</intent-filter>
7777
</activity>
78+
<activity
79+
android:name=".ui.NotificationsActivity"
80+
android:exported="false"
81+
android:theme="@style/MenuTheme" />
82+
7883
<service
7984
android:name=".core.HostService"
8085
android:enabled="true"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.damn.anotherglass.glass.ee.host.ui
2+
3+
import android.graphics.Color
4+
import android.graphics.drawable.ColorDrawable
5+
import android.os.Bundle
6+
import android.view.LayoutInflater
7+
import android.view.View
8+
import android.view.ViewGroup
9+
import androidx.lifecycle.LiveData
10+
import androidx.viewpager.widget.PagerAdapter
11+
import com.damn.anotherglass.glass.ee.host.core.NotificationController
12+
import com.damn.anotherglass.glass.ee.host.databinding.LayoutNotificationsStackBinding
13+
import com.damn.anotherglass.glass.ee.host.databinding.ViewPagerLayoutBinding
14+
import com.damn.anotherglass.glass.ee.host.ui.extensions.LayoutNotificationsStackBindingEx.bindData
15+
import com.damn.anotherglass.shared.notifications.NotificationData
16+
17+
class NotificationsActivity : BaseActivity() {
18+
19+
override fun onCreate(savedInstanceState: Bundle?) {
20+
super.onCreate(savedInstanceState)
21+
22+
ViewPagerLayoutBinding.inflate(layoutInflater).apply {
23+
setContentView(root)
24+
root.background = ColorDrawable(Color.BLACK)
25+
viewPager.adapter = NotificationsPagerAdapter(
26+
NotificationController.instance.getNotifications(),
27+
this@NotificationsActivity,
28+
layoutInflater
29+
)
30+
pageIndicator.setupWithViewPager(viewPager, true)
31+
}
32+
}
33+
34+
class NotificationsPagerAdapter(
35+
notificationsLiveData: LiveData<List<NotificationData>>,
36+
notificationsActivity: NotificationsActivity,
37+
private val layoutInflater: LayoutInflater
38+
) : PagerAdapter() {
39+
40+
private var notifications: List<NotificationData> = notificationsLiveData.value!!
41+
set(value) {
42+
field = value
43+
notifyDataSetChanged()
44+
}
45+
46+
init {
47+
notificationsLiveData.observe(notificationsActivity) {
48+
if (it.isEmpty()) {
49+
notificationsActivity.finish() // if no notifications, finish activity to avoid unnecessary resource consumption
50+
} else {
51+
notifications = it
52+
}
53+
}
54+
}
55+
56+
override fun instantiateItem(container: ViewGroup, position: Int): Any =
57+
LayoutNotificationsStackBinding.inflate(layoutInflater).apply {
58+
container.addView(root)
59+
val data = notifications[position]
60+
bindData(data, layoutInflater.context)
61+
}.root
62+
63+
override fun destroyItem(container: ViewGroup, position: Int, obj: Any) =
64+
container.removeView(obj as View)
65+
66+
override fun getCount(): Int = notifications.size
67+
68+
override fun isViewFromObject(view: View, obj: Any): Boolean = view == obj
69+
}
70+
}

glass-ee/src/main/java/com/damn/anotherglass/glass/ee/host/ui/cards/NotificationsCard.kt

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package com.damn.anotherglass.glass.ee.host.ui.cards
22

33
import android.content.Context
4-
import android.graphics.BitmapFactory
4+
import android.content.Intent
55
import android.os.Bundle
6-
import android.text.format.DateUtils
7-
import android.util.Log
86
import android.view.LayoutInflater
97
import android.view.View
108
import android.view.ViewGroup
119
import com.damn.anotherglass.glass.ee.host.core.NotificationController
1210
import com.damn.anotherglass.glass.ee.host.databinding.LayoutNotificationsStackBinding
13-
import com.damn.anotherglass.shared.notifications.NotificationData
11+
import com.damn.anotherglass.glass.ee.host.ui.NotificationsActivity
12+
import com.damn.anotherglass.glass.ee.host.ui.extensions.LayoutNotificationsStackBindingEx.bindData
1413

1514
class NotificationsCard : BaseFragment() {
1615

@@ -42,36 +41,15 @@ class NotificationsCard : BaseFragment() {
4241
}
4342
}
4443

45-
private fun LayoutNotificationsStackBinding.bindData(
46-
last: NotificationData,
47-
context: Context
48-
) {
49-
title.text = last.title
50-
text.text = last.text
51-
footer.text = last.tickerText
52-
timestamp.text = DateUtils.formatDateTime(
53-
context,
54-
last.postedTime,
55-
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_SHOW_TIME or DateUtils.FORMAT_ABBREV_ALL
56-
)
57-
if (last.icon == null) {
58-
imgIcon.setImageResource(android.R.drawable.ic_dialog_info)
59-
} else {
60-
try {
61-
val bitmap = BitmapFactory.decodeByteArray(last.icon, 0, last.icon.size)
62-
imgIcon.setImageBitmap(bitmap)
63-
} catch (e: Exception) {
64-
Log.e(TAG, "Failed to decode icon: " + e.message, e)
65-
imgIcon.setImageResource(android.R.drawable.ic_dialog_info)
66-
}
67-
}
68-
}
69-
7044
override fun onDestroyView() {
7145
super.onDestroyView()
7246
binding = null
7347
}
7448

49+
override fun onSingleTapUp() {
50+
requireActivity().startActivity(Intent(requireActivity(), NotificationsActivity::class.java))
51+
}
52+
7553
companion object {
7654
private const val TAG = "NotificationsCard"
7755

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.damn.anotherglass.glass.ee.host.ui.extensions
2+
3+
import android.content.Context
4+
import android.graphics.BitmapFactory
5+
import android.text.format.DateUtils
6+
import android.util.Log
7+
import com.damn.anotherglass.glass.ee.host.databinding.LayoutNotificationsStackBinding
8+
import com.damn.anotherglass.shared.notifications.NotificationData
9+
10+
object LayoutNotificationsStackBindingEx {
11+
12+
private const val TAG = "LayoutNotificationsStackBindingEx"
13+
14+
fun LayoutNotificationsStackBinding.bindData(
15+
last: NotificationData,
16+
context: Context
17+
) {
18+
title.text = last.title
19+
text.text = last.text
20+
footer.text = last.tickerText
21+
timestamp.text = DateUtils.formatDateTime(
22+
context,
23+
last.postedTime,
24+
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_SHOW_TIME or DateUtils.FORMAT_ABBREV_ALL
25+
)
26+
if (last.icon == null) {
27+
imgIcon.setImageResource(android.R.drawable.ic_dialog_info)
28+
} else {
29+
try {
30+
val bitmap = BitmapFactory.decodeByteArray(last.icon, 0, last.icon.size)
31+
imgIcon.setImageBitmap(bitmap)
32+
} catch (e: Exception) {
33+
Log.e(TAG, "Failed to decode icon: " + e.message, e)
34+
imgIcon.setImageResource(android.R.drawable.ic_dialog_info)
35+
}
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)