how to implement a different thread inside the service in XAMARIN - c#

What I am trying to do is to run the activity while having a background process when a button is pressed. (Because my real problem is when I am having a very long process, my front-end is being stopped for a while)
What I tried to do is to make a service and bind to it (which I don't know if I made it right since I thought it will work independently). But still, when I am trying to access the method from the service, my foreground is still being stopped for a while.
HOW CAN I ACHIEVE TO MAKE MY FOREGROUND STILL RUNNING WHILE WAITING FOR THE RESPONSE OF WEB SERVICE IN THE BACKGROUND
(I ALREADY MADE IT WITH PROGRESS BAR TO MAKE THE USER AWARE IT IS STILL LOADING)
Here is my service, serviceConnection and binder code: // it is still useless, i'm just trying to test this
[Service]
public class MyService : Service
{
static readonly string TAG = "X:" + typeof(MyService).Name;
int i;
#region Override OnCreate()
public override void OnCreate()
{
base.OnCreate();
Toast.MakeText(this, "MyService started", ToastLength.Short).Show();
}
#endregion
#region Override OnDestroy
public override void OnDestroy()
{
base.OnDestroy();
StopSelf();
Log.Debug(TAG, "MyService destroyed at {0}.", DateTime.Now);
}
#endregion
#region Override OnBind
public override IBinder OnBind(Intent intent)
{
return new MyBinder(this);
}
#endregion
public string GetTimestamp(string value)
{
// this is just a testing
if (value == string.Empty)
{
for (i = 0; i < 9999999; i++)
{
if (i == 100000)
Log.Debug(TAG, "100000 zxc");
if (i == 500000)
Log.Debug(TAG, "500000 zxc");
}
return "empty";
}
else
return i.ToString() + " from service";
}
}
public class MyBinder : Binder
{
MyService service;
public MyBinder(MyService service)
{
Toast.MakeText(service.ApplicationContext, "my binder", ToastLength.Short).Show();
this.service = service;
}
public string GetFormattedTimestamp(string value)
{
return service?.GetTimestamp(value);
}
}
public class MyServiceConnection : Java.Lang.Object, IServiceConnection
{
static readonly string TAG = typeof(MyServiceConnection).FullName;
public bool IsConnected { get; private set; }
public MyBinder Binder { get; private set; }
public MyServiceConnection()
{
IsConnected = false;
Binder = null;
}
public void OnServiceConnected(ComponentName name, IBinder binder)
{
Binder = binder as MyBinder;
IsConnected = Binder != null;
Log.Debug(TAG, "OnServiceConnected zxc");
}
public void OnServiceDisconnected(ComponentName name)
{
Log.Debug(TAG, "OnServiceDisconnected zxc");
IsConnected = false;
Binder = null;
}
}

Related

Is there a way to be notified of whenever the volume setting is changed in Xamarin C#? [duplicate]

I'm programming a small widget that needs to be updated whenever the user changes the ringer volume or the vibrate settings.
Capturing android.media.VIBRATE_SETTING_CHANGED works just fine for the vibrate settings, but I haven't found any way of getting notified when the ringer volume changes and although I could try to capture when the user presses the volume up/volume down physical keys, there are many other options for changing the volume without using these keys.
Do you know if there's any broadcast action defined for this or any way to create one or to solve the problem without it?
There is no broadcast action, but I did find you can hook up a content observer to get notified when the settings change, volume of streams being some of those settings. Register for the android.provider.Settings.System.CONTENT_URI to be notified of all settings changes:
mSettingsContentObserver = new SettingsContentObserver( new Handler() );
this.getApplicationContext().getContentResolver().registerContentObserver(
android.provider.Settings.System.CONTENT_URI, true,
mSettingsContentObserver );
The content observer might look something like this:
public class SettingsContentObserver extends ContentObserver {
public SettingsContentObserver(Handler handler) {
super(handler);
}
#Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.v(LOG_TAG, "Settings change detected");
updateStuff();
}
}
And be sure to unregister the content observer at some point.
Nathan's code works but gives two notifications for each change system settings. To avoid that, use the following
public class SettingsContentObserver extends ContentObserver {
int previousVolume;
Context context;
public SettingsContentObserver(Context c, Handler handler) {
super(handler);
context=c;
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
previousVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
}
#Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
int delta=previousVolume-currentVolume;
if(delta>0)
{
Logger.d("Decreased");
previousVolume=currentVolume;
}
else if(delta<0)
{
Logger.d("Increased");
previousVolume=currentVolume;
}
}
}
Then in your service onCreate register it with:
mSettingsContentObserver = new SettingsContentObserver(this,new Handler());
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver );
Then unregister in onDestroy:
getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);
Yes, you can register a receiver for a volume change(this is kind of a hack, but works), I managed to do it this way (does not involve a ContentObserver):
In manifest xml file:
<receiver android:name="com.example.myproject.receivers.MyReceiver" >
<intent-filter>
<action android:name="android.media.VOLUME_CHANGED_ACTION" />
</intent-filter>
</receiver>
BroadcastReceiver:
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")) {
Log.d("Music Stream", "has changed");
}
}
}
hope it helps!
Based into Nathan's, adi's and swooby's code I created a full working example with some minor improvements.
Looking to the AudioFragment class we can see how easy is to listen for volume changes with our custom ContentObserver.
public class AudioFragment extends Fragment implements OnAudioVolumeChangedListener {
private AudioVolumeObserver mAudioVolumeObserver;
#Override
public void onResume() {
super.onResume();
// initialize audio observer
if (mAudioVolumeObserver == null) {
mAudioVolumeObserver = new AudioVolumeObserver(getActivity());
}
/*
* register audio observer to identify the volume changes
* of audio streams for music playback.
*
* It is also possible to listen for changes in other audio stream types:
* STREAM_RING: phone ring, STREAM_ALARM: alarms, STREAM_SYSTEM: system sounds, etc.
*/
mAudioVolumeObserver.register(AudioManager.STREAM_MUSIC, this);
}
#Override
public void onPause() {
super.onPause();
// release audio observer
if (mAudioVolumeObserver != null) {
mAudioVolumeObserver.unregister();
}
}
#Override
public void onAudioVolumeChanged(int currentVolume, int maxVolume) {
Log.d("Audio", "Volume: " + currentVolume + "/" + maxVolume);
Log.d("Audio", "Volume: " + (int) ((float) currentVolume / maxVolume) * 100 + "%");
}
}
public class AudioVolumeContentObserver extends ContentObserver {
private final OnAudioVolumeChangedListener mListener;
private final AudioManager mAudioManager;
private final int mAudioStreamType;
private int mLastVolume;
public AudioVolumeContentObserver(
#NonNull Handler handler,
#NonNull AudioManager audioManager,
int audioStreamType,
#NonNull OnAudioVolumeChangedListener listener) {
super(handler);
mAudioManager = audioManager;
mAudioStreamType = audioStreamType;
mListener = listener;
mLastVolume = audioManager.getStreamVolume(mAudioStreamType);
}
/**
* Depending on the handler this method may be executed on the UI thread
*/
#Override
public void onChange(boolean selfChange, Uri uri) {
if (mAudioManager != null && mListener != null) {
int maxVolume = mAudioManager.getStreamMaxVolume(mAudioStreamType);
int currentVolume = mAudioManager.getStreamVolume(mAudioStreamType);
if (currentVolume != mLastVolume) {
mLastVolume = currentVolume;
mListener.onAudioVolumeChanged(currentVolume, maxVolume);
}
}
}
#Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
}
public class AudioVolumeObserver {
private final Context mContext;
private final AudioManager mAudioManager;
private AudioVolumeContentObserver mAudioVolumeContentObserver;
public AudioVolumeObserver(#NonNull Context context) {
mContext = context;
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
public void register(int audioStreamType,
#NonNull OnAudioVolumeChangedListener listener) {
Handler handler = new Handler();
// with this handler AudioVolumeContentObserver#onChange()
// will be executed in the main thread
// To execute in another thread you can use a Looper
// +info: https://stackoverflow.com/a/35261443/904907
mAudioVolumeContentObserver = new AudioVolumeContentObserver(
handler,
mAudioManager,
audioStreamType,
listener);
mContext.getContentResolver().registerContentObserver(
android.provider.Settings.System.CONTENT_URI,
true,
mAudioVolumeContentObserver);
}
public void unregister() {
if (mAudioVolumeContentObserver != null) {
mContext.getContentResolver().unregisterContentObserver(mAudioVolumeContentObserver);
mAudioVolumeContentObserver = null;
}
}
}
public interface OnAudioVolumeChangedListener {
void onAudioVolumeChanged(int currentVolume, int maxVolume);
}
Hope it's still useful for someone! :)
Nathan's and adi's code works, but can be cleaned up and self-contained to:
public class AudioStreamVolumeObserver
{
public interface OnAudioStreamVolumeChangedListener
{
void onAudioStreamVolumeChanged(int audioStreamType, int volume);
}
private static class AudioStreamVolumeContentObserver
extends ContentObserver
{
private final AudioManager mAudioManager;
private final int mAudioStreamType;
private final OnAudioStreamVolumeChangedListener mListener;
private int mLastVolume;
public AudioStreamVolumeContentObserver(
#NonNull
Handler handler,
#NonNull
AudioManager audioManager, int audioStreamType,
#NonNull
OnAudioStreamVolumeChangedListener listener)
{
super(handler);
mAudioManager = audioManager;
mAudioStreamType = audioStreamType;
mListener = listener;
mLastVolume = mAudioManager.getStreamVolume(mAudioStreamType);
}
#Override
public void onChange(boolean selfChange)
{
int currentVolume = mAudioManager.getStreamVolume(mAudioStreamType);
if (currentVolume != mLastVolume)
{
mLastVolume = currentVolume;
mListener.onAudioStreamVolumeChanged(mAudioStreamType, currentVolume);
}
}
}
private final Context mContext;
private AudioStreamVolumeContentObserver mAudioStreamVolumeContentObserver;
public AudioStreamVolumeObserver(
#NonNull
Context context)
{
mContext = context;
}
public void start(int audioStreamType,
#NonNull
OnAudioStreamVolumeChangedListener listener)
{
stop();
Handler handler = new Handler();
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
mAudioStreamVolumeContentObserver = new AudioStreamVolumeContentObserver(handler, audioManager, audioStreamType, listener);
mContext.getContentResolver()
.registerContentObserver(System.CONTENT_URI, true, mAudioStreamVolumeContentObserver);
}
public void stop()
{
if (mAudioStreamVolumeContentObserver == null)
{
return;
}
mContext.getContentResolver()
.unregisterContentObserver(mAudioStreamVolumeContentObserver);
mAudioStreamVolumeContentObserver = null;
}
}
If its only ringer mode change you can use Brodcast receiver with "android.media.RINGER_MODE_CHANGED" as the action. It will easy to implement
Hi i tried the code above and it did not work for me. But when i tried to add this line
getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);
and put
mSettingsContentObserver = new SettingsContentObserver(this,new Handler());
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver );
It works now. My concern is how to hide the volume dialog onchange. See this image.
private const val EXTRA_VOLUME_STREAM_TYPE = "android.media.EXTRA_VOLUME_STREAM_TYPE"
private const val VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION"
val filter = IntentFilter(VOLUME_CHANGED_ACTION)
filter.addAction(RINGER_MODE_CHANGED_ACTION)
val receiver = object : BroadcastReceiver() {
override fun onReceive(context1: Context, intent: Intent) {
val stream = intent.getIntExtra(EXTRA_VOLUME_STREAM_TYPE, UNKNOWN)
val mode = intent.getIntExtra(EXTRA_RINGER_MODE, UNKNOWN)
val volumeLevel = audioManager.getStreamVolume(stream)
}
}
100% working way in all cases
public class SettingsContentObserver extends ContentObserver {
SettingsContentObserver(Handler handler) {
super(handler);
}
#Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
volumeDialogContract.updateMediaVolume(getMediaVolume());
}
int getMediaVolume() {
return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
}
void unRegisterVolumeChangeListener() {
volumeDialogContract.getAppContext().getApplicationContext().getContentResolver().
unregisterContentObserver(settingsContentObserver);
}
void registerVolumeChangeListener() {
settingsContentObserver = new VolumeDialogPresenter.SettingsContentObserver(new Handler());
volumeDialogContract.getAppContext().getApplicationContext().getContentResolver().registerContentObserver(
android.provider.Settings.System.CONTENT_URI, true,
settingsContentObserver);
}

How to output logs with Serilog to a textbox in Winform?

Help!
I can not figure it out how to implement Serilog to output my logs in real time into a textbox from Winforms.
I have an application written in .Net C# that was written a long time ago and had the logging framework log4net. I had different appenders and one was created in my code :
public class ExAppender : AppenderSkeleton{
private IExAppender control = null;
public void AttachControl(IExAppender obj)
{ this.control = obj;}
protected override void Append(LoggingEvent loggingEvent)
{
try
{
string message = RenderLoggingEvent(loggingEvent);
if (this.control != null)
{
this.control.LogMessage(message, loggingEvent.Level.Name);
}
}catch{// ignore}
}
And after that I had another class defined ExLogger:
public static class ExLogger
{ private static readonly ILog LoggerObj = null;
public static bool AttachControl(IExAppender obj)
{
IAppender[] appenders = LoggerObj.Logger.Repository.GetAppenders();
(appender as ExAppender).AttachControl(obj);
return true;
}
return false;}
I defined my serilog loggers in app.config, I want to read them from there because i have multiple loggers, I think that I need to use public class ExAppender : ILogEventSink, I replaced the old code to be suitable for Serilog, it writes to files, to eventLog, console etc, BUT I could not found a way to attach a windows to the logger and to write there. After my modification I obtaind something like this:
public class ExAppender : ILogEventSink
{
public ExAppender control = null;
public ConcurrentQueue<string> Events { get; } = new ConcurrentQueue<string>();
public void AttachControl(IExAppender obj)
{
this.control = obj;
}
public void Emit(LogEvent logEvent)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
var renderSpace = new ExAppender();
Events.Enqueue(renderSpace.ToString());
try
{ string message = logEvent.RenderMessage();
if (this.control != null)
{
this.control.LogMessage(message, logEvent.Level.ToString());
}
}catch { }
}
And for the ExLogger class:
public static bool AttachControl( IExAppender obj)
{try
{
ILogger test = new LoggerConfiguration()
.ReadFrom.AppSettings(settingPrefix: "ExAppender")
.WriteTo.ExAppender(restrictedToMinimumLevel: LogEventLevel.Information)
.CreateLogger();
return true;
}catch
{
return false;
}}
Can someone guide me? Does someone has an example or maybe explain what am I missing?
Maybe I am a bit too late to help you, but this is how I implemented it:
Custom logger sink, which has EventHandler:
public class TbsLoggerSink : ILogEventSink
{
public event EventHandler NewLogHandler;
public TbsLoggerSink() { }
public void Emit(LogEvent logEvent)
{
#if DEBUG
Console.WriteLine($"{logEvent.Timestamp}] {logEvent.MessageTemplate}");
#endif
NewLogHandler?.Invoke(typeof(TbsCore.Helpers.TbsLoggerSink), new LogEventArgs() { Log = logEvent });
}
}
public class LogEventArgs : EventArgs
{
public LogEvent Log { get; set; }
}
When creating the Serilog logger, add your custom sink. I use static sink/logger so I can access it from anywhere.
public static TbsLoggerSink LoggerSink = new TbsLoggerSink();
public static readonly Serilog.Core.Logger Log = new LoggerConfiguration()
.WriteTo.Sink(LoggerSink)
.CreateLogger();
Than in your view/form, where you have TextBox/RichTextBox (in my case this.logTextBox), add event handler:
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
Utils.LoggerSink.NewLogHandler += LogHandler;
}
private void LogHandler(object sender, EventArgs e)
{
var log = ((LogEventArgs)e).Log;
this.logTextBox.Text = $"{log.Timestamp.DateTime.ToString("HH:mm:ss")}: {log.MessageTemplate}\n{this.logTextBox.Text}";
}
}

C# - How to prevent a subclass from calling protected methods directly by itself?

My code:
abstract class StateMachine {
protected string State { get; private set; }
protected abstract void OnWorking();
protected abstract void OnPrepare();
protected abstract void OnCancel();
public bool Prepare() {
if(State != null) {
return false;
}
State = "Preparing";
OnPrepare();
State = "Prepared";
return true;
}
public bool Start() {
if(State != "Prepared") {
return false;
}
State = "Working";
OnWorking();
State = "Done";
return true;
}
public bool Cancel() {
if(State != "Working" || State == "Done") {
return false;
}
OnCancel();
State = "Canceled";
return true;
}
}
class Downloader : StateMachine {
protected override void OnPrepare() {
Console.WriteLine("I am preparing.");
Thread.Sleep(1000);
}
protected override void OnWorking() {
Console.WriteLine("I am working.");
Thread.Sleep(1000);
}
protected override void OnCancel() {
Console.WriteLine("Let's cancel the operation!");
}
}
class Program {
static void Main(string[] args) {
Downloader downloader = new Downloader();
Parallel.Invoke(() => {
downloader.Prepare();
downloader.Start();
}, () => {
// Cancel while working
Thread.Sleep(1500);
downloader.Cancel();
});
}
}
The output would be:
I am preparing.
I am working.
Let's cancel the operation!
Now I am building the StateMachine class and it works very well. It allows subclasses to not care about the current states at all, which is awesome because handling states of a process is a huge pain in the head.
The problem is though, nothing can't stop the subclass (Downloader) from calling those protected methods in the base class (StateMachine) by itself. For example, a subclass can have something like:
protected override void OnWorking(){
Console.WriteLine("I am working.");
Thread.Sleep(1000);
OnCancel();
OnPrepare();
}
Then the output would be:
I am preparing.
I am working.
Let's cancel the operation!
I am preparing.
Let's cancel the operation!
Which is not expected from the StateMachine's point of view.
So I am trying to prevent the subclass from calling protected methods. But I feel like I am doing a weird thing. I don't think C# OOP concepts would allow this behavior.
I don't mean to make these protected methods invisible from the subclass though. I'm more about throwing exceptions if subclasses do that. Maybe I need to add extra logic in the base class the handle this. But that might makes the code messy.
What would you do in this situation? I mean, what might be the elegant way to solve this?
You can define them as delegates and make them private, and set them with abstract helper methods as follow:
static void Main()
{
Downloader downloader = new Downloader();
Parallel.Invoke(() =>
{
downloader.Prepare();
downloader.Start();
}, () =>
{
// Cancel while working
Thread.Sleep(1500);
downloader.Cancel();
});
}
abstract class StateMachine
{
protected string State { get; private set; }
private Action OnWorking;
private Action OnPrepare;
private Action OnCancel;
// Helper methods to be implemented in subclass
protected abstract Action DefineWorkingAction();
protected abstract Action DefinePrepareAction();
protected abstract Action DefineCancelAction();
protected StateMachine()
{
this.OnWorking = DefineWorkingAction();
this.OnPrepare = DefinePrepareAction();
this.OnCancel = DefineCancelAction();
}
public bool Prepare()
{
if (State != null)
{
return false;
}
State = "Preparing";
OnPrepare();
State = "Prepared";
return true;
}
public bool Start()
{
if (State != "Prepared")
{
return false;
}
State = "Working";
OnWorking();
State = "Done";
return true;
}
public bool Cancel()
{
if (State != "Working" || State == "Done")
{
return false;
}
OnCancel();
State = "Canceled";
return true;
}
}
class Downloader : StateMachine
{
protected override Action DefineWorkingAction()
{
return () =>
{
Console.WriteLine("I am working.");
Thread.Sleep(1000);
};
}
protected override Action DefinePrepareAction()
{
return () =>
{
Console.WriteLine("I am preparing.");
Thread.Sleep(1000);
};
}
protected override Action DefineCancelAction()
{
return () =>
{
Console.WriteLine("Let's cancel the operation!");
};
}
}
Now subclasses cannot call them anymore.

How to skip/pause notifications sent to observers in c#

I implemented an observer pattern using events and delegates. The program is receiving and processing big amounts of data (around 3000 messages per second) but at some point, it starts sending messages with a delayed timestamp, which I am trying to fix. I have 3 main classes that do the job in my opinion:
public class MessageTracker : IObservable<MessageEventArgs>
{
private List<IObserver<MessageEventArgs>> observers;
public MessageTracker()
{
observers = new List<IObserver<MessageEventArgs>>();
}
private static readonly MessageTracker mInstance = new MessageTracker();
private static MessageTracker getInstance() => mInstance;
private class Unsubscriber : IDisposable
{
private List<IObserver<MessageEventArgs>> _observers;
private IObserver<MessageEventArgs> _observer;
public Unsubscriber(List<IObserver<MessageEventArgs>> observers, IObserver<MessageEventArgs> observer)
{
this._observers = observers;
this._observer = observer;
}
public void Dispose()
{
if (! (_observer == null)) _observers.Remove(_observer);
}
}
public IDisposable Subscribe(IObserver<MessageEventArgs> observer)
{
if (! observers.Contains(observer))
observers.Add(observer);
return new Unsubscriber(observers, observer);
}
public void MessageTrack(MessageEventArgs msg) {
observers.AsParallel().ForAll(observer =>
{
if (msg is null)
observer.OnError(new ArgumentException("MessageError."));
else
observer.OnNext(msg);
});
}
public void EndMessageTrans(){
foreach(var observer in observers.ToArray())
if (observers.Contains(observer))
observer.OnCompleted();
observers.Clear();
}
}
public class MessageReporter : IObserver<MessageEventArgs>
{
private IDisposable unsubscriber;
public MessageReporter()
{ }
public event EventHandler<MessageEventArgs> OnNextMessage;
public virtual void Subscribe(IObservable<MessageEventArgs> provider)
{
if (provider != null)
unsubscriber = provider.Subscribe(this);
}
public void OnCompleted()
{
this.Unsubscribe();
}
public void OnError(Exception error)
{
}
public void OnNext(MessageEventArgs value)
{
if (OnNextMessage != null)
{
OnNextMessage?.Invoke(this, value);
}
}
public virtual void Unsubscribe()
{
unsubscriber.Dispose();
}
}
public sealed class MessageDataWorker
{
private readonly bool mSubscribeAll;
private readonly IEnumerable<string> mMessages;
public MessageDataWorker(IEnumerable<string> messages)
{
mMessages = messages;
if ((mMessages?.Count() ?? 0) == 0)
mSubscribeAll = true;
}
public override void DoWork()
{
var messageReporter = new MessageReporter();
messageReporter.OnNextMessage += OnNewMessageReceived;
messageReporter.Subscribe(MessageTracker.GetInstance());
while (!mShouldStop.WaitOne(100)) ;
MessageReporter.Unsubscribe();
}
private void OnNewMessageReceived(object sender, MessageEventArgs e)
{
if (!mSubscribeAll && !mMessages.Contains(e.Message))
return;
string message = "Message|" +
$"{e.Time}|" +
$"{e.Text};
try
{
Console.WriteLine(message);
}
catch { }
}
}
What I am trying to achieve is skipping notifications or receiving data for X milliseconds after sending the last message and afterward send the newest received message. I tried sleeping the observers and the provider but it just increased the delay. I think I am missing something and any suggestion would be appreciated.
From what I can tell from your code you could write the three classes with this code:
var messageTrack = new Subject<MessageEventArgs>();
var query =
from e in messageTrack
where !mMessages.Contains(e.Message)
select $"Message|{e.Time}|{e.Text}";
query.Throttle(TimeSpan.FromMilliseconds(X)).Subscribe(Console.WriteLine);
You should never need to implement IObservable<> or IObserver<> yourself. It almost always ends in disaster.
The above code handles the throttling you wanted.

MVVMCross Messenger - Subscribing to messages in static class

I am just looking into implementing an MVVMCross Messenger solution that will enable me to upload information to Google Analytics when published from either the iOS application or the PCL.
The problem I have the is that the subscription delgates are not fired after I publish. Can you subscribe to MVVMCross Messenger subscriptions from a static class?
Subscriptions in static class
public static class GoogleAnalyticsWrapper //: IDisposable
{
private const string TrackingId = "xxxxxxxxxxx";
private static readonly IMvxMessenger messenger;
private static readonly MvxSubscriptionToken screenNameToken;
private static readonly MvxSubscriptionToken eventToken;
private static readonly MvxSubscriptionToken exceptionToken;
private static readonly MvxSubscriptionToken performanceToken;
private static readonly MvxSubscriptionToken publishToken;
private static bool disposed = false;
private static SafeHandle handle;
static GoogleAnalyticsWrapper()
{
Gai.SharedInstance.DispatchInterval = 60;
Gai.SharedInstance.TrackUncaughtExceptions = true;
Gai.SharedInstance.GetTracker(TrackingId);
messenger = new MvxMessengerHub();// Mvx.Resolve<IMvxMessenger>();
screenNameToken = messenger.Subscribe<GaScreenNameMessage>((m) => SetScreenName(m));
int count = messenger.CountSubscriptionsFor<GaScreenNameMessage>();
eventToken = messenger.Subscribe<GaEventMessage>(CreateEvent);
exceptionToken = messenger.Subscribe<GaExceptionMessage>(CreateException);
performanceToken = messenger.Subscribe<GaPerformanceTimingMessage>(CreatePerformanceMetric);
publishToken = messenger.Subscribe<GaPublishMessage>(PublishAll);
}
public static string Dummy { get; set; }
public static void SetScreenName(GaScreenNameMessage message)
{
System.Diagnostics.Debugger.Break();
Gai.SharedInstance.DefaultTracker.Set(GaiConstants.ScreenName, message.ScreenName);
Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateScreenView().Build());
}
public static void CreateEvent(GaEventMessage message)
=> Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateEvent(message.Category, message.Action, message.Label, message.Number).Build());
private static void CreateException(GaExceptionMessage message)
=> Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateException(message.ExceptionMessage, message.IsFatal).Build());
private static void CreatePerformanceMetric(GaPerformanceTimingMessage message)
=> Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateTiming(message.Category, message.Milliseconds, message.Name, message.Label).Build());
private static void PublishAll(GaPublishMessage message)
=> Gai.SharedInstance.Dispatch();
public static void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Dispose managed resources.
if (handle != null)
{
handle.Dispose();
}
}
// Dispose unmanaged managed resources.
disposed = true;
}
}
}
Publication
messengerService.Publish<GaEventMessage>(new GaEventMessage(this, "Event", "Publish Event", "Publish Event From First View Model", 123));
The problem is, that you are creating a new MvxMessengerHub in your static class, but (I guess) inject IMvxMessenger in your consuming classes, which is created by MvvMCross during the initialization lifecycle and so a different instance.
The easy solution would be to initialize it in your App.cs like
public class App : Cirrious.MvvmCross.ViewModels.MvxApplication
{
public override void Initialize()
{
// ...
var m = Cirrious.CrossCore.Mvx.Resolve<IMvxMessenger>();
GoogleAnalyticsWrapper.Initialize(m);
// ...
}
}
With a wrapper like this
public static class GoogleAnalyticsWrapper
{
static void Initialize(IMvxMessenger messenger)
{
Gai.SharedInstance.DispatchInterval = 60;
Gai.SharedInstance.TrackUncaughtExceptions = true;
Gai.SharedInstance.GetTracker(TrackingId);
screenNameToken = messenger.Subscribe<GaScreenNameMessage>((m) => SetScreenName(m));
int count = messenger.CountSubscriptionsFor<GaScreenNameMessage>();
eventToken = messenger.Subscribe<GaEventMessage>(CreateEvent);
exceptionToken = messenger.Subscribe<GaExceptionMessage>(CreateException);
performanceToken = messenger.Subscribe<GaPerformanceTimingMessage>(CreatePerformanceMetric);
publishToken = messenger.Subscribe<GaPublishMessage>(PublishAll);
}
// ...
}
Advanced Hint
But as far as I see, you don't even need messaging for this case, because it's one to one "communication". I think it would be nice, if you move the functionality of your GoogleAnalyticsWrapper into a well defined Service like:
interface ITrackingService
{
void SetScreenName(GaScreenNameMessage message);
void CreateEvent(GaEventMessage message);
void CreateException(GaExceptionMessage message);
void CreatePerformanceMetric(GaPerformanceTimingMessage message);
void PublishAll(GaPublishMessage message);
}
public class GoogleAnalyticsTrackingService : ITrackingService
{
private const string TrackingId = "xxxxxxxxxxx";
public GoogleAnalyticsTrackingService()
{
Gai.SharedInstance.DispatchInterval = 60;
Gai.SharedInstance.TrackUncaughtExceptions = true;
Gai.SharedInstance.GetTracker(TrackingId);
}
public void SetScreenName(GaScreenNameMessage message)
{
Gai.SharedInstance.DefaultTracker.Set(GaiConstants.ScreenName, message.ScreenName);
Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateScreenView().Build());
}
public void CreateEvent(GaEventMessage message)
{
Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateEvent(message.Category, message.Action, message.Label, message.Number).Build());
}
private void CreateException(GaExceptionMessage message)
{
Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateException(message.ExceptionMessage, message.IsFatal).Build());
}
private void CreatePerformanceMetric(GaPerformanceTimingMessage message)
{
Gai.SharedInstance.DefaultTracker.Send(DictionaryBuilder.CreateTiming(message.Category, message.Milliseconds, message.Name, message.Label).Build());
}
private void PublishAll(GaPublishMessage message)
{
Gai.SharedInstance.Dispatch();
}
}
That has to be registered in your App
Mvx.LazyConstructAndRegisterSingleton<ITrackingService, GoogleAnalyticsTrackingService>();
And can be consumed with constructor injection or manual resolves
class MyViewModel : MvxViewModel
{
public MyViewModel(ITrackingService tracking)
{
tracking.CreateEvent(new GaEventMessage(this, "Event", "Publish Event", "Publish Event From First View Model", 123));
}
}
// or
class MyViewModel : MvxViewModel
{
public MyViewModel()
{
var tracking = Mvx.Resolve<ITrackingService>();
tracking.CreateEvent(new GaEventMessage(this, "Event", "Publish Event", "Publish Event From First View Model", 123));
}
}
There is still one Problem: The interface has still a dependency to google analytics. But the dependency can be easily removed by using multiple parameters instead of a parameter object.
interface ITrackingService
{
void CreateEvent(string eventName, string title, string message, params object[] additionalParams);
// ...
}
// call:
tracking.CreateEvent("Event", "Publish Event", "Publish Event From First View Model", 123);
With this, you are able to unit test it and exchange the tracking service with litte effort, if your stakeholders decide to switch to adobe omniture or whatever.

Categories