12
12
13
13
namespace AndroidXamarinChat
14
14
{
15
- public class ChatCommandHandler
16
- {
17
- private Activity parentActivity ;
18
- private MessageListViewAdapter messageAdapter ;
19
- public Dictionary < string , List < ChatMessage > > FullHistory { get ; set ; }
20
-
21
- public string CurrentChannel { get ; set ; }
22
-
23
- public ChatCommandHandler ( Activity parentActivity , MessageListViewAdapter messageAdapter , string initialChannel )
24
- {
25
- this . parentActivity = parentActivity ;
26
- this . messageAdapter = messageAdapter ;
27
- this . FullHistory = new Dictionary < string , List < ChatMessage > > ( ) ;
28
- this . CurrentChannel = initialChannel ;
29
- }
30
-
31
- public void AppendMessage ( ChatMessage chatMessage )
32
- {
33
- if ( ! FullHistory . ContainsKey ( chatMessage . Channel ) ) {
34
- FullHistory . Add ( chatMessage . Channel , new List < ChatMessage > ( ) ) ;
35
- }
36
- FullHistory [ chatMessage . Channel ] . Add ( chatMessage ) ;
37
- if ( chatMessage . Channel == this . CurrentChannel ) {
38
- Application . SynchronizationContext . Post ( _ =>
39
- {
15
+ public class ChatCommandHandler
16
+ {
17
+ private Activity parentActivity ;
18
+ private MessageListViewAdapter messageAdapter ;
19
+ public Dictionary < string , List < ChatMessage > > FullHistory { get ; set ; }
20
+
21
+ public string CurrentChannel { get ; set ; }
22
+
23
+ public ChatCommandHandler ( Activity parentActivity , MessageListViewAdapter messageAdapter , string initialChannel )
24
+ {
25
+ this . parentActivity = parentActivity ;
26
+ this . messageAdapter = messageAdapter ;
27
+ this . FullHistory = new Dictionary < string , List < ChatMessage > > ( ) ;
28
+ this . CurrentChannel = initialChannel ;
29
+ }
30
+
31
+ public void AppendMessage ( ChatMessage chatMessage )
32
+ {
33
+ if ( ! FullHistory . ContainsKey ( chatMessage . Channel ) )
34
+ {
35
+ FullHistory . Add ( chatMessage . Channel , new List < ChatMessage > ( ) ) ;
36
+ }
37
+ FullHistory [ chatMessage . Channel ] . Add ( chatMessage ) ;
38
+ if ( chatMessage . Channel == this . CurrentChannel )
39
+ {
40
+ Application . SynchronizationContext . Post ( _ =>
41
+ {
40
42
messageAdapter . Add ( chatMessage ) ;
41
43
messageAdapter . NotifyDataSetChanged ( ) ;
42
- } , null ) ;
43
- }
44
- }
45
-
46
- public void ChangeChannel ( string channel )
47
- {
48
- this . CurrentChannel = channel ;
49
- Application . SynchronizationContext . Post ( _ =>
50
- {
44
+ } , null ) ;
45
+ }
46
+ }
47
+
48
+ public void ChangeChannel ( string channel )
49
+ {
50
+ this . CurrentChannel = channel ;
51
+ Application . SynchronizationContext . Post ( _ =>
52
+ {
51
53
messageAdapter . Clear ( ) ;
52
54
if ( FullHistory . ContainsKey ( channel ) )
53
55
{
54
- FullHistory [ channel ] . ForEach ( msg => {
56
+ FullHistory [ channel ] . ForEach ( msg =>
57
+ {
55
58
messageAdapter . Add ( msg ) ;
56
59
} ) ;
57
60
}
58
- } , null ) ;
59
- }
61
+ } , null ) ;
62
+ }
60
63
61
- public void SyncAdapter ( )
62
- {
63
- ChangeChannel ( this . CurrentChannel ) ;
64
- }
64
+ public void SyncAdapter ( )
65
+ {
66
+ ChangeChannel ( this . CurrentChannel ) ;
67
+ }
65
68
66
- public void ShowVideo ( string videoUrl )
67
- {
68
- parentActivity . StartActivity ( new Intent ( Intent . ActionView , Android . Net . Uri . Parse ( videoUrl ) ) ) ;
69
- }
69
+ public void ShowVideo ( string videoUrl )
70
+ {
71
+ parentActivity . StartActivity ( new Intent ( Intent . ActionView , Android . Net . Uri . Parse ( videoUrl ) ) ) ;
72
+ }
70
73
71
- public void Announce ( string message )
72
- {
74
+ public void Announce ( string message )
75
+ {
73
76
Notification . Builder builder = new Notification . Builder ( parentActivity )
74
77
. SetLocalOnly ( true )
75
78
. SetAutoCancel ( true )
@@ -86,74 +89,77 @@ public void Announce(string message)
86
89
87
90
// Publish the notification:
88
91
const int notificationId = 0 ;
89
- if ( notificationManager != null )
90
- {
91
- notificationManager . Notify ( notificationId , notification ) ;
92
-
93
- Vibrator vibrator = ( Vibrator ) parentActivity . GetSystemService ( Context . VibratorService ) ;
94
- vibrator . Vibrate ( 1000 ) ;
95
- CancelNotification ( notificationManager ) . ConfigureAwait ( false ) ;
96
- }
97
- }
98
-
99
- private async Task CancelNotification ( NotificationManager notificationManager )
100
- {
101
- await Task . Delay ( 5000 ) ;
92
+ if ( notificationManager != null )
93
+ {
94
+ notificationManager . Notify ( notificationId , notification ) ;
95
+
96
+ Vibrator vibrator = ( Vibrator ) parentActivity . GetSystemService ( Context . VibratorService ) ;
97
+ vibrator . Vibrate ( 1000 ) ;
98
+ CancelNotification ( notificationManager ) . ConfigureAwait ( false ) ;
99
+ }
100
+ }
101
+
102
+ private async Task CancelNotification ( NotificationManager notificationManager )
103
+ {
104
+ await Task . Delay ( 5000 ) ;
102
105
notificationManager . CancelAll ( ) ;
103
- }
104
-
105
- public void ChangeBackground ( string message )
106
- {
107
- var url = message . StartsWith ( "url(" ) ? message . Substring ( 4 , message . Length - 5 ) : message ;
108
- url . GetImageBitmap ( ) . ContinueWith ( t =>
109
- {
110
- t . Wait ( ) ;
106
+ }
107
+
108
+ public void ChangeBackground ( string message )
109
+ {
110
+ var url = message . StartsWith ( "url(" ) ? message . Substring ( 4 , message . Length - 5 ) : message ;
111
+ url . GetImageBitmap ( ) . ContinueWith ( t =>
112
+ {
113
+ t . Wait ( ) ;
111
114
var bitmap = t . Result ;
112
115
var chatBackground = parentActivity . FindViewById < ImageView > ( Resource . Id . chat_background ) ;
113
116
Application . SynchronizationContext . Post ( _ =>
114
117
{
115
118
chatBackground . SetImageBitmap ( bitmap ) ;
116
- } , null ) ;
117
- } ) ;
119
+ } , null ) ;
120
+ } ) ;
118
121
}
119
122
120
- public void ChangeBackgroundColor ( string message )
121
- {
123
+ public void ChangeBackgroundColor ( string message )
124
+ {
122
125
// Inject alpha values
123
- string color = message . Replace ( "#" , "#AA" ) ;
124
- var chatLayout = parentActivity . FindViewById < ListView > ( Resource . Id . messageHistory ) ;
125
- var editText = parentActivity . FindViewById < EditText > ( Resource . Id . message ) ;
126
- var sendButton = parentActivity . FindViewById < Button > ( Resource . Id . sendMessageButton ) ;
126
+ string color = message . Replace ( "#" , "#AA" ) ;
127
+ var chatLayout = parentActivity . FindViewById < ListView > ( Resource . Id . messageHistory ) ;
128
+ var editText = parentActivity . FindViewById < EditText > ( Resource . Id . message ) ;
129
+ var sendButton = parentActivity . FindViewById < Button > ( Resource . Id . sendMessageButton ) ;
127
130
Application . SynchronizationContext . Post ( _ =>
128
131
{
129
132
chatLayout . SetBackgroundColor ( Color . ParseColor ( color ) ) ;
130
133
editText . SetBackgroundColor ( Color . ParseColor ( color ) ) ;
131
134
sendButton . SetBackgroundColor ( Color . ParseColor ( color ) ) ;
132
135
} , null ) ;
133
- }
134
- }
135
-
136
- public class MessageResolver : IResolver
137
- {
138
- ChatCommandHandler messageHandler ;
139
- public MessageResolver ( ChatCommandHandler messageHandler )
140
- {
141
- this . messageHandler = messageHandler ;
142
- }
143
-
144
- public T TryResolve < T > ( )
145
- {
146
- if ( typeof ( T ) == typeof ( ChatReceiver ) ) {
147
- return ( T ) ( new ChatReceiver ( this . messageHandler ) as object ) ;
148
- }
149
- if ( typeof ( T ) == typeof ( TvReciever ) ) {
150
- return ( T ) ( new TvReciever ( this . messageHandler ) as object ) ;
151
- }
152
- if ( typeof ( T ) == typeof ( CssReceiver ) ) {
153
- return ( T ) ( new CssReceiver ( this . messageHandler ) as object ) ;
154
- }
155
-
156
- return typeof ( T ) . CreateInstance < T > ( ) ;
157
- }
158
- }
136
+ }
137
+ }
138
+
139
+ public class MessageResolver : IResolver
140
+ {
141
+ ChatCommandHandler messageHandler ;
142
+ public MessageResolver ( ChatCommandHandler messageHandler )
143
+ {
144
+ this . messageHandler = messageHandler ;
145
+ }
146
+
147
+ public T TryResolve < T > ( )
148
+ {
149
+ if ( typeof ( T ) == typeof ( ChatReceiver ) )
150
+ {
151
+ return ( T ) ( new ChatReceiver ( this . messageHandler ) as object ) ;
152
+ }
153
+ if ( typeof ( T ) == typeof ( TvReciever ) )
154
+ {
155
+ return ( T ) ( new TvReciever ( this . messageHandler ) as object ) ;
156
+ }
157
+ if ( typeof ( T ) == typeof ( CssReceiver ) )
158
+ {
159
+ return ( T ) ( new CssReceiver ( this . messageHandler ) as object ) ;
160
+ }
161
+
162
+ return typeof ( T ) . CreateInstance < T > ( ) ;
163
+ }
164
+ }
159
165
}
0 commit comments