forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AvatarGroupViewActivity.kt
174 lines (157 loc) · 8.8 KB
/
AvatarGroupViewActivity.kt
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
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
package com.microsoft.fluentuidemo.demos
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import com.microsoft.fluentui.persona.AvatarBorderStyle
import com.microsoft.fluentui.persona.AvatarGroupStyle
import com.microsoft.fluentui.persona.AvatarGroupView
import com.microsoft.fluentui.persona.AvatarSize
import com.microsoft.fluentui.popupmenu.PopupMenu
import com.microsoft.fluentui.popupmenu.PopupMenuItem
import com.microsoft.fluentui.snackbar.Snackbar
import com.microsoft.fluentuidemo.DemoActivity
import com.microsoft.fluentuidemo.R
import com.microsoft.fluentuidemo.databinding.ActivityAvatarGroupViewBinding
import com.microsoft.fluentuidemo.util.createAvatarList
import com.microsoft.fluentuidemo.util.createAvatarNameList
import com.microsoft.fluentuidemo.util.createImageAvatarList
import com.microsoft.fluentuidemo.util.createSmallAvatarList
class AvatarGroupViewActivity : DemoActivity() {
private var singleCheckedItemId: Int = -1
var borderStyle: AvatarBorderStyle = AvatarBorderStyle.RING
private lateinit var avatarGroupBinding: ActivityAvatarGroupViewBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
avatarGroupBinding = ActivityAvatarGroupViewBinding.inflate(
LayoutInflater.from(container.context),
container,
true
)
avatarGroupBinding.avatarFaceStackExampleXxlargePhoto.setAvatars(createAvatarList(this))
avatarGroupBinding.avatarFaceStackExampleXlargePhoto.setAvatars(createAvatarNameList(this))
avatarGroupBinding.avatarFaceStackExampleLargePhoto.setAvatars(createSmallAvatarList(this))
avatarGroupBinding.avatarFaceStackExampleMediumPhoto.setAvatars(createImageAvatarList(this))
createFaceStackFromCode(avatarGroupBinding.avatarFaceStackExampleSmallPhoto)
avatarGroupBinding.avatarFaceStackExampleXsmallPhoto.setAvatars(createAvatarList(this))
avatarGroupBinding.avatarFacePileExampleXxlargePhoto.setAvatars(createAvatarNameList(this))
avatarGroupBinding.avatarFacePileExampleXlargePhoto.setAvatars(createSmallAvatarList(this))
avatarGroupBinding.avatarFacePileExampleLargePhoto.setAvatars(createImageAvatarList(this))
createFacePileFromCode(avatarGroupBinding.avatarFacePileExampleMediumPhoto)
avatarGroupBinding.avatarFacePileExampleSmallPhoto.setAvatars(createAvatarList(this))
avatarGroupBinding.avatarFacePileExampleXsmallPhoto.setAvatars(createAvatarList(this))
avatarGroupBinding.avatarFaceStackExampleXsmallPhotoOverflow.setAvatars(
createAvatarList(
this
)
)
avatarGroupBinding.avatarFacePileExampleXsmallPhotoOverflow.setAvatars(createAvatarList(this))
setupMaxAvatarDisplayed(avatarGroupBinding.maxDisplayedAvatar)
avatarGroupBinding.overflowAvatarCount.setOnEditorActionListener { v, actionId, event ->
return@setOnEditorActionListener when (actionId) {
EditorInfo.IME_ACTION_DONE -> {
avatarGroupBinding.avatarFaceStackExampleXsmallPhotoOverflow.overflowAvatarCount =
Integer.parseInt(avatarGroupBinding.overflowAvatarCount.text.toString())
avatarGroupBinding.avatarFacePileExampleXsmallPhotoOverflow.overflowAvatarCount =
Integer.parseInt(avatarGroupBinding.overflowAvatarCount.text.toString())
val imm =
v.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(v.windowToken, 0)
true
}
else -> {
false
}
}
}
avatarGroupBinding.avatarBorderToggle.setOnClickListener {
toggleBorders()
}
}
private fun createFaceStackFromCode(avatarGroupView: AvatarGroupView) {
avatarGroupView.avatarGroupStyle = AvatarGroupStyle.STACK
avatarGroupView.avatarSize = AvatarSize.SMALL
avatarGroupView.avatarBorderStyle = AvatarBorderStyle.RING
avatarGroupView.setAvatars(createAvatarList(this))
avatarGroupView.listener = object : AvatarGroupView.Listener {
override fun onAvatarClicked(index: Int) {
Snackbar.make(
demoBinding.rootView,
String.format(getString(R.string.avatar_group_avatar_clicked), index),
Snackbar.LENGTH_SHORT
).show()
}
override fun onOverFlowClicked() {
Snackbar.make(
demoBinding.rootView,
getString(R.string.avatar_group_overflow_clicked),
Snackbar.LENGTH_SHORT
).show()
}
}
}
private fun createFacePileFromCode(avatarGroupView: AvatarGroupView) {
avatarGroupView.avatarGroupStyle = AvatarGroupStyle.PILE
avatarGroupView.avatarSize = AvatarSize.MEDIUM
avatarGroupView.avatarBorderStyle = AvatarBorderStyle.RING
avatarGroupView.setAvatars(createAvatarList(this))
}
private fun setupMaxAvatarDisplayed(anchorView: View) {
val popupMenuItems: ArrayList<PopupMenuItem> = ArrayList()
for (id in 1..4) {
popupMenuItems.add(PopupMenuItem(id, id.toString()))
avatarGroupBinding.maxDisplayedAvatar.text = id.toString()
}
val onPopupMenuItemClickListener = object : PopupMenuItem.OnClickListener {
override fun onPopupMenuItemClicked(popupMenuItem: PopupMenuItem) {
singleCheckedItemId = popupMenuItem.id
avatarGroupBinding.maxDisplayedAvatar.text = popupMenuItem.title
setMaxAvatarDisplayedForAllViews(popupMenuItem.id)
}
}
val popupMenu =
PopupMenu(this, anchorView, popupMenuItems, PopupMenu.ItemCheckableBehavior.SINGLE)
popupMenu.onItemClickListener = onPopupMenuItemClickListener
anchorView.setOnClickListener {
popupMenu.show()
}
}
private fun toggleBorders() {
borderStyle =
if (borderStyle == AvatarBorderStyle.RING) AvatarBorderStyle.NO_BORDER else AvatarBorderStyle.RING
avatarGroupBinding.avatarFaceStackExampleXxlargePhoto.avatarBorderStyle = borderStyle
avatarGroupBinding.avatarFaceStackExampleXlargePhoto.avatarBorderStyle = borderStyle
avatarGroupBinding.avatarFaceStackExampleLargePhoto.avatarBorderStyle = borderStyle
avatarGroupBinding.avatarFaceStackExampleMediumPhoto.avatarBorderStyle = borderStyle
avatarGroupBinding.avatarFaceStackExampleSmallPhoto.avatarBorderStyle = borderStyle
avatarGroupBinding.avatarFaceStackExampleXsmallPhoto.avatarBorderStyle = borderStyle
avatarGroupBinding.avatarFacePileExampleXxlargePhoto.avatarBorderStyle = borderStyle
avatarGroupBinding.avatarFacePileExampleXlargePhoto.avatarBorderStyle = borderStyle
avatarGroupBinding.avatarFacePileExampleLargePhoto.avatarBorderStyle = borderStyle
avatarGroupBinding.avatarFacePileExampleMediumPhoto.avatarBorderStyle = borderStyle
avatarGroupBinding.avatarFacePileExampleSmallPhoto.avatarBorderStyle = borderStyle
avatarGroupBinding.avatarFacePileExampleXsmallPhoto.avatarBorderStyle = borderStyle
}
private fun setMaxAvatarDisplayedForAllViews(id: Int) {
avatarGroupBinding.avatarFaceStackExampleXxlargePhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFaceStackExampleXlargePhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFaceStackExampleLargePhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFaceStackExampleMediumPhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFaceStackExampleSmallPhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFaceStackExampleXsmallPhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFacePileExampleXxlargePhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFacePileExampleXlargePhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFacePileExampleLargePhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFacePileExampleMediumPhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFacePileExampleSmallPhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFacePileExampleXsmallPhoto.maxDisplayedAvatars = id
avatarGroupBinding.avatarFaceStackExampleXsmallPhotoOverflow.maxDisplayedAvatars = id
avatarGroupBinding.avatarFacePileExampleXsmallPhotoOverflow.maxDisplayedAvatars = id
}
}