lundi 30 novembre 2015

Proof of Concept: Trying to update PubCenter Ad ID and Ad Unit Id without having to resubmit your application

Proof of Concept: What if I wanted to create my own Ad manager that would allow me to be able to change the PubCenter Ad Id and Unit Id without having to resubmit the app.

 Here is the main idea behind this POC:
  • The different steps, first we will create a json file that will hold our default information.
  • Next when the application first launch’s it will read this file and store the information to memory.
  • Afterward we will call an endpoint with an updated version of the XML file and will copy the information of this json into that one if and only if the updated version has a higher version number.
  • Next when the UserControl containing the PubCenter ( or something else) ad is called it will read in memory the different values and pass them to the PubCenter element.
Lets get Coding!!

But first you need some place to store the file that will hold your information, you can either use your own server or dropbox, because I wanted to go quickly for this POC I used dropbox: https://www.dropbox.com/s/ruoe5dknprvn68d/ExampleAds.txt?dl=1

Now we can really start coding

  • You will need to create a file called DefaultAds.txt at the root of your app files, with the following content:

 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
{
  "AdSettings": {
    "Created" : "05102015",
    "Version" : "1",
    "AdElement": [
      {
        "Type": "small",
        "Kind": "Image",
        "AppID": "fb7f2816-4607-4508-86ee-e5c2058b627e",
        "AdID": "220045"
      },
      {
        "Type": "small",
        "Kind": "Image",
        "AppID": "fb7f2816-4607-4508-86ee-e5c2058b627e",
        "AdID": "220048"
      },
      {
        "Type": "medium",
        "Kind": "Post",
       "AppID": "fb7f2816-4607-4508-86ee-e5c2058b627e",
        "AdID": "220046"
      },
      {
        "Type": "medium",
        "Kind": "Post",
        "AppID": "fb7f2816-4607-4508-86ee-e5c2058b627e",
        "AdID": "220047"
      },
      {
        "Type": "medium",
        "Kind": "Main",
          "AppID": "fb7f2816-4607-4508-86ee-e5c2058b627e",
        "AdID": "220049"
      }
    ]
  }
}
  • Then we are going to create a class named AdPubcenterService in which we will add a method called ReadFileFromStorageFile which will luck as follows:

 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
private async static Task ReadFileFromStorageFile()
        {
            var adSettings = new AdSettingsRoot();

            try
            {
                String ResourceReference = "ms-appx:///DefaultAds.txt";
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(ResourceReference, UriKind.Absolute));
                Stream src = await file.OpenStreamForReadAsync();

                using (StreamReader sr = new StreamReader(src))
                {
                    string text = sr.ReadToEnd();
                    adSettings = JsonConvert.DeserializeObject<AdSettingsRoot>(text);
                }

                //Updating data
                App.AdSettingsBase.AdElement_Small1 = adSettings.AdSettings.AdElement[0];
                App.AdSettingsBase.AdElement_Small2 = adSettings.AdSettings.AdElement[1];
                App.AdSettingsBase.AdElement_Medium_1 = adSettings.AdSettings.AdElement[2];
                App.AdSettingsBase.AdElement_Medium_2 = adSettings.AdSettings.AdElement[3];
                App.AdSettingsBase.AdElement_Medium_Main = adSettings.AdSettings.AdElement[4];

                App.AdSettingsBase.Version = adSettings.AdSettings.Version;
                App.AdSettingsBase.LastUpdated = Convert.ToDateTime(adSettings.AdSettings.Created);
            }
            catch (Exception e)
            {
                //Catch errors here
            }
        }


  • Now that we can read our file localy we also need to be able to ready our file that is store somewhere on on the web (on dropbox for me).  I will create a method call GetAdsFromHttpJsonFiles which will read our local file first, then make a call to our url and save the data for the url.



 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
        public static string PageUrl = "https://www.dropbox.com/s/ruoe5dknprvn68d/ExampleAds.txt?dl=1";

        public static async Task GetAdsFromHttpJsonFiles()
        {
            await ReadFileFromStorageFile();

            AdSettingsRoot adSettings = new AdSettingsRoot();
            bool error = false;

            try
            {
                var req = new HttpClient();
                var message = new HttpRequestMessage(HttpMethod.Get, PageUrl);

                var response = await req.SendAsync(message);
                string result = await response.Content.ReadAsStringAsync();
                adSettings = JsonConvert.DeserializeObject<AdSettingsRoot>(result);
            }
            catch (Exception e)
            {
                error = true;
            }

            ParseElementForAds(adSettings);
        }



  • The Method ParseElementForAds is what allow us to save information that we have store outside our application.  In this method:
 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
     private static void ParseElementForAds(AdSettingsRoot elementRoot)
        {
            bool updateFile = false;

            //Makign sure the file is not empty
            if (elementRoot.AdSettings != null)
            {
                //if Initialsing file is needed
                if (App.AdSettingsBase == null)
                {
                    App.AdSettingsBase = new AdSettingsBase();
                    updateFile = true;
                }

                //Checking the version
                int version = (int)(elementRoot.AdSettings.Version);

                //if new gotten elements are newer then the one i have update the settings file
                if (!updateFile && App.AdSettingsBase.Version < version)
                {
                    updateFile = true;
                }

                //making sure that we should update the file & that we have the correct count
                if (updateFile && elementRoot.AdSettings.AdElement.Count == 5)
                {
                    App.AdSettingsBase.Version = version;
                    App.AdSettingsBase.LastUpdated = DateTime.Now;

                    App.AdSettingsBase.AdElement_Small1 = elementRoot.AdSettings.AdElement[0];
                    App.AdSettingsBase.AdElement_Small2 = elementRoot.AdSettings.AdElement[1];
                    App.AdSettingsBase.AdElement_Medium_1 = elementRoot.AdSettings.AdElement[2];
                    App.AdSettingsBase.AdElement_Medium_2 = elementRoot.AdSettings.AdElement[3];
                    App.AdSettingsBase.AdElement_Medium_Main = elementRoot.AdSettings.AdElement[4];

#pragma warning disable 4014
                    SaveFileFromStorageFile(elementRoot);
#pragma warning restore 4014

                }
            }
        }
   
  • Next we will create a UserControl and add pubcenter XAML to it:
1
2
3
4
5
6
 
<ui:AdControl x:Name="AdMain"
                  Width="490"
                  Height="80"
                  Margin="0,0,0,0"
                  HorizontalAlignment="Left"
                  ErrorOccurred="AdControl_OnErrorOccurred" />

  • And when the user control is initialized we will pass our AdI d and AppId to the PubCenter Controller:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
  public PublicityUserControl()
        {
            this.InitializeComponent();

            try
            {
                if (App.AdSettingsBase != null)
                {
                    AdMain.AdUnitId = App.AdSettingsBase.AdElement_Medium_1.AdID; //"219859";
                    AdMain.ApplicationId = App.AdSettingsBase.AdElement_Medium_1.AppID; //"45e36de2-ed28-4ca7-9eea-779875b65700"; 
                }
            }
            catch (Exception e)
            {
              
            }
        }

        private void AdControl_OnErrorOccurred(object sender, AdErrorEventArgs e)
        {
            Debug.WriteLine(e.Error);
        }

And there you have it, you can now manage you Ad Id and App Id from a remote location without having to resubmit your application every time!

Here is the source code: https://onedrive.live.com/redir?resid=70A41C6835F56338!39329&authkey=!ANWpJkTH9Bpn5ho&ithint=file%2czip


lundi 23 novembre 2015

Why using Template 10 is a great idea in a Universal Application

Template10 is on Github

When you first install Visual Studio 2015 and want to start developing for windows 10 all you get an empty blank template...  which is not great.



At least in when we started developing for Windows 8.1 we had the option to start with different tempaltes:
  • Blank App
  • Hub App
  • Grid App
  • And more
As you can see here:



So why when we want to create a Windows Universal App in Visual Studio 2015 we only have a blank application template where is the love!!

Why did the Visual Studio Team do this to us?  did we do something wrong? I don't know... but I found a solution and this is where Jerry Nixon and the Github community comes in and try to give you a hand!

Template 10: https://github.com/Windows-XAML/Template10 is a Windows app project template that can help you speed up your development time!

Thanks to the great job of the community Template 10 allows us Universal application developers for Windows 10 to have a very nice starting base for your development and it also gives you conventions that you SHOULD follow =).

Here are the conventions that Template 10 uses:
  1. Views (XAML files) are located in an /Views folder (and ns)
  2. Only have one view-model for one view
  3. View-models are located in a /ViewModels folder (and ns)
  4. Use OnNavigatedTo in view-models, not pages
  5. Models are located in a /Models folder (and ns)
  6. Use the façade pattern with our models
  7. Navigate using a NavigationService
  8. Communicate with a Messenger
  9. Dependency Injection
  10. We use Template 10 ;-)
Help us make Template 10 more robust and help the Github community  by asking questions and contributing if you can!  I have recently done a pull request so that if you (like me) are migrating your existing application from a Windows Phone or Windows Store application that was using Mvvm Light, you can keep on using your current properties without having to add Mvvm Light to your project.   Because yes Template 10 does ship with a mini Mvvm Framework, that being said you should use your favorite Mvvm Framework.

I'll let you check out the Github repository: https://github.com/Windows-XAML/Template10
Or you can download the Visual Studio template here: https://visualstudiogallery.msdn.microsoft.com/60bb885a-44e9-4cbf-a380-270803b3f6e5



Thanks for reading.

lundi 9 novembre 2015

Customizing your Media Player Framework on Windows Phone (XAML, C#)

I have written and article on how to customize your Media Player Framework on my company engineering blog.

As you know here at Dailymotion we strive to deliver the best video player experience no matter what platform you are on (Windows, iOS, Android etc).

Thus, I have spent a lot of time customizing the Media Player for our Windows apps and needless to say that it has been a long journey for me because of the lack of any good tutorials and examples. Many times I used trial and error to try and figure out what worked and what did not.

You can find the rest of the article here

mercredi 4 novembre 2015

Detecting if an object is outside of the screen for Windows (8/8.1) Store Apps (C#/XAML)

How to detect if an object is intersecting with another object
This is a tutorial on how to implement a methode that allows us to see if you have 2 objects that are intersecting with one another. I mainly use this to see if an object is outside of the screen for the user and thus need to show him something else when that object is not visible anymore.
If you don't want to have to look at the code here is the important part, these two methodes IsObjectHidden and IntersectsWith.

  IsObjectHidden Methode

//the object of this methode is to create 2 rectangles, from our two objects and see they intersect or not
protected bool IsObjectHidden(FrameworkElement child, FrameworkElement scrollViewer, int Offset)
{
    //Getting the information related to the scrollViewer
    GeneralTransform childTransform = child.TransformToVisual(scrollViewer);
    //creating out first rectangle using the object that i wish to ttrack
    Rect childRectangle = childTransform.TransformBounds(new Rect(new Point(0, 0), child.RenderSize));
    //creating out second rectangle  using the scrollViewer
    Rect ownerRectangle = new Rect(new Point(0, 0), scrollViewer.RenderSize);
    //Testing
    bool isInterset = IntersectsWith(childRectangle, ownerRectangle, Offset);
    return !isInterset;
}

  IsObjectHidden

//the object of this methode is to create 2 rectangles, from our two objects and see they intersect or not
public static bool IntersectsWith(Rect rect, Rect other, int offset)
{
    // Test for separating axis on the two rectangles
    if (other.Bottom < rect.Top || other.Right < rect.Left
    || other.Top > rect.Bottom || other.Left > (rect.Right - offset))
    {
        return false;
    }
    return true;
}
I have only tested this on Windows Store apps, happy coding.