Forum Xamarin.Forms

Need some feedback please!

JKnottJKnott Member ✭✭✭
edited February 7 in Xamarin.Forms

I am working on making a free version of my app. To do this I need to implement ads in the app, but want to make this as seamless as possible to my existing code.
I will just create a new build target for a _Free version. I can then use this code to build both versions of the app.

When I track the number of times the user viewed a reward video, I understand you can use FireBase to track these things, but I was considering tracking the number of times as a field in the database using cryptography for that record entry. This way the database will always know how many rewards the user gets.
I am trying to keep this app as off the cloud as possible.
Would this be a good approach? or is there a nifferent way I should be doing this?

I have started working on this implementation, but would like to hear people's feedback on if this sounds sound for how to go about it. I am using the MarcTron.AdMob library. This library is incredibly simple and easy to implement so if you're not familiar with it's API, that shouldn't matter.
My BaseViewModel implements the AdDataViewModel as a public element within it. This means any viewmodel I derrive from the base will automatically have access to creating ads on their pages.


<Stacklayout Orientation="Vertical">
    <Frame x:Name="myAdview" />
 ..... other page elements ....

Then in codebehind all I have to do is this.


public MyPageViewModel viewModel;

public void InitializationRoutine()

//viewmodel has been initialized in the page constructor
  viewModel.AdDataViewModel = new AdDataViewModel();
  myAdview.Content = viewModel.AdDataViewModel.AdViewElement; 

The viewmodel then creates / sets the data for this particular implementation of the ad view;
I am in the process of extending this viewmodel to support reward ads IDs and handlers independently of banner ads all in the same instance (basically user has option to view add for reward within the app)

namespace MyApp.ViewModels
    //EventParent overloads the SetProperty and OnPropertyChanged events for my getters and setters
    public class AdDataViewModel : EventParent
        //Lazy constructor, have class do everything
        public AdDataViewModel()
            Type = AdType.Banner;

        //set a key as a part of the constructor
        public AdDataViewModel(string key)
            Type = AdType.Banner;

        // set both key and type 
        public AdDataViewModel(string key, AdType type)
            Type = type;
            AdIDKey = key;


        //This is an enum of various ad types 
        public AdType Type;

        All of the get / set sets use this methodology, I've trimmed 
        them for space and readability
        private string adIDKey;
        public string AdIDKey
            get => adIDKey;

                SetProperty(ref adIDKey, value, nameof(AdIDKey));

        //This is the actual contentView for the ad
        private MTAdView adViewElement;
        public MTAdView AdViewElement 
        .... get set as before...

        private string adName;
        public string AdName
        .... get set as before...

        private bool personalizedSetting;
        public bool PersonalizedSetting
        .... get set as before...

        private int adHeight;
        private int adWidth;

        //If we already have a key the user can make this call skip calling
        // Alternately this can be called to change the AdIDKey
        private async void Initialize(bool NeedsKey = true)
            //Specify default values for height and width.
            adHeight = 80;
            adWidth = 120;

            if (NeedsKey)
                AdIDKey = await GetAdKey(Type);

            // This can be changed later, but is defaulted here.
            PersonalizedSetting = true;

            AdViewElement = new MTAdView();

        public void RefreshAdData()
          AdViewElement.AdsIdKey = AdIDKey;
          AdViewElement.PersonalizedAds = PersonalizedSetting;
          AdViewElement.HeightRequest = adHeight;
          AdViewElement.WidthRequest = adWidth;

        //This is where I need the most help. 
        //Is this a good way to go about getting the keys for my ads? I want to make 
        // debugging and free vs paid versions use as much common code as possible.
        private async Task<string> GetAdKey(AdType type)
            string szKeyType = string.Empty;

            switch (type)
                case AdType.Banner:
                    //Default to NOKEY, change it if we are in the appropriate build
                    szKeyType = "NOKEY";
                    //These are arbitrary, I can overload them to have getters and setters,
                    // or just leave them statically coded like this. 
                    adHeight = 80;
                    adWidth = 120;
                    AdIDKey = "ca-app-pub-3940256099942544/6300978111";
#elif _Free
                    //We're in the free version of the app, so we can get the key
                    szKeyType = "BannerAdKey";
                    //Commercial version of the app will still pass through the NOKEY so there will
                    // be no wasted overhead for getting the key from securestore.

                    This has case statements for all of the supported ad types

          //If the 'NOKEY'was set, this is a test build and the key has been set to the test key
          if (!szKeyType.Contains("NOKEY"))
              AdIDKey = await SecureStorage.GetAsync(szKeyType);

          //Return the appropriate key.
          return AdIDKey;

I have another question about making my free vs paid versions use common code bases. But I will post that a little later.

Any feedback would be GREATLY appreciated!

Sign In or Register to comment.