Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS:Convert ObjC code to C#, How to know the time app has been idle

I am new to iOS development, I am using monotouch for developing the iOS apps, i want to know the time since the app was idle, i got the ObjC code but am unable to convert it into c#. Here is the code:

- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event];

    // Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
    NSSet *allTouches = [event allTouches];
    if ([allTouches count] > 0) {
        // allTouches count only ever seems to be 1, so anyObject works here.
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
            [self resetIdleTimer];
    }
}

- (void)resetIdleTimer {
    if (idleTimer) {
        [idleTimer invalidate];
        [idleTimer release];
    }

    idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO] retain];
}

 - (void)idleTimerExceeded {
    NSLog(@"idle time exceeded");
}

Can anyone help me in converting this to c#.

like image 804
Saurabh Sashank Avatar asked Feb 14 '23 17:02

Saurabh Sashank


1 Answers

There's certainly better way to achieve this, but let's do a almost line to line translation fo this Obj-C code to Xamarin.iOS C#:

sendEvent is a method of UIApplication. It's very uncommon to subclass it, see Subclassing notes on UIApplication class reference

Once you subclassed it, you have to instruct the runtime to use it, that's done in the Main method, normally found in Main.cs. Here's what the modified Main.cs looks like now.

public class Application
{
    // This is the main entry point of the application.
    static void Main (string[] args)
    {
        UIApplication.Main (args, "MyApplication", "AppDelegate");
    }
}

[Register ("MyApplication")]
public class MyApplication : UIApplication
{

}

Notice the Register attribute on the class, used as second argument of UIApplication.Main.

Now, let's translate your code for real:

[Register ("MyApplication")]
public class MyApplication : UIApplication
{
    public override void SendEvent (UIEvent uievent)
    {
        base.SendEvent (uievent);
        var allTouches = uievent.AllTouches;
        if (allTouches.Count > 0) {
            var phase = ((UITouch)allTouches.AnyObject).Phase;
            if (phase == UITouchPhase.Began || phase == UITouchPhase.Ended)
                ResetIdleTimer ();
        }
    }

    NSTimer idleTimer;
    void ResetIdleTimer ()
    {
        if (idleTimer != null) {
            idleTimer.Invalidate ();
            idleTimer.Release ();
        }

        idleTimer = NSTimer.CreateScheduledTimer (TimeSpan.FromHours (1), TimerExceeded);
    }

    void TimerExceeded ()
    {
        Debug.WriteLine ("idle time exceeded");
    }
}

I replaced maxIdleTime by TimeSpan.FromHours (1). Otherwise, you'll have the same behaviour as the Obj-C one, including bugs if any (it looks ok though).

like image 144
Stephane Delcroix Avatar answered Feb 17 '23 07:02

Stephane Delcroix