Skip to content

RobertoGFilho/WorkHours

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Work Hours

Xamarin Forms Application with design pattern MVVM and CLEAN CODE principles. Available on Android, iOS and Windows platforms

Screenshot 2021-12-20 222701

IDE and Project

  • Microsoft Visual Studio 2019;
  • Xamarin Forms Project;

Libraries

  • Microsoft.EntityFrameworkCore.Sqlite : database abstraction layer;
  • Microsoft.EntityFrameworkCore.Tools : used for data migration;
  • Refractored.MvvmHelpers : used for data binding and synchronous and asynchronous commands;
  • Xamarin.Essentials : internet connection verification;
  • Shell : page browsing;
  • FluentValidation: validation when editing data

Languages

Multilingual App Toolkit extension was used to generate the translation files available in \Resources folder for languages:

  • English;
  • Brazilian portuguese;

<Label Text="{extensions:Translate pokemonTypes}" FontSize="Caption"/>

Models

Three main classes Payment, Work and WorkBalance

ModelsDiagram

Business

Layer between model and viewModel responsible for data validation and new business rules;

public class BaseBusiness<TModel> : ObservableObject where TModel : BaseModel, new()
{
    ...
    public IList<ValidationFailure> Erros { get; set; }
    public AbstractValidator<TModel> Validator { get; set; }
    ...
}

View Model

In this layer, inheritance and generics techniques combined were used, to define behavior patterns and maximum code reuse.

public abstract class BaseCollectionViewModel<TModel, TBusiness, TDataManager> : 
BaseDataViewModel<TModel, TBusiness, TDataManager> where TModel : BaseModel, new() where 
TBusiness : BaseBusiness<TModel>, new() where 
TDataManager : BaseManager<TModel>, new()
{ ... }

BaseViewModelsDiagram - Copy

View

Layer representing the page and injecting the viewModel.

public partial class BasePage<TViewModel> : ContentPage where TViewModel : ViewModels.BaseViewModel, new()
{ ... }

Database

The Sqlite and Entity Framework were used as data caching strategy.

Migrations

Whenever model structures are changed, adding or removing fields or new models, the migration will be performed at startup restructuring the database;

public class Database : DbContext
{
    ...
    public void Initialize()
    {
        //Database.EnsureDeleted();
        Database.Migrate();
    }
}

Set Amout Paid

private void SetAmounPaid(Work model)
    {
        var start = model.StartHour;
        var finish = (model.FinishHour.Hours == 0) ? new TimeSpan(24, 0, 0) : model.FinishHour;
        
        while (start < finish)
        {
            var payment = paymentsManager.GetPaymentFromTime(start.Add(new TimeSpan(0, 1, 0)), model.DayOfWeek);

            if (payment != null)
            {
                model.AmounPaid += payment.AmounPaid;
            }

            start = start.Add(new TimeSpan(1, 0, 0));
        }
    }

Image Font

Strategy used to use icons, in the action bar, from true type fonts.

  • icofont.ttf;
  • material.ttf;

Navigation

Injecting the "views" page navigation service through view model navigation

public partial class App : Application
{
    ...   
    public App(string dbPath)
    {
        ...
        DependencyService.Register<Interfaces.INavigation, Navigation>();
    }   
 }
 
 public class Navigation : Interfaces.INavigation
 {
    public async Task<TViewModel> GoToAsync<TViewModel>() where TViewModel : BaseViewModel
    {
        await Shell.Current.GoToAsync(typeof(TViewModel).Name);
        return Shell.Current.CurrentPage.BindingContext as TViewModel;
    }

    public Task GoToBackAsync()
    {
        return Shell.Current.GoToAsync("..");

    }
 }
 
public abstract class BaseViewModel : MvvmHelpers.BaseViewModel
{
    ...
    public Interfaces.INavigation Navigation => 
    DependencyService.Get<Interfaces.INavigation>(DependencyFetchTarget.GlobalInstance);
}

Conclusion

Focus on best programming practices in Xamarin Forms applications.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages