Skip to content
This repository has been archived by the owner on Jul 2, 2022. It is now read-only.

Commit

Permalink
Improvements for Source Code
Browse files Browse the repository at this point in the history
  • Loading branch information
thedillonb committed Feb 7, 2016
1 parent b2960c7 commit e2b1c2a
Show file tree
Hide file tree
Showing 123 changed files with 7,605 additions and 4,262 deletions.
2 changes: 1 addition & 1 deletion CodeHub.Core/Factories/ILoginFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface ILoginFactory

Task<GitHubSharp.Client> LoginAccount(GitHubAccount account);

Task<LoginData> Authenticate(string domain, string user, string pass, string twoFactor, bool enterprise, GitHubAccount existingAccount);
Task<LoginData> CreateLoginData(string domain, string user, string pass, string twoFactor, bool enterprise, GitHubAccount existingAccount);
}

public class LoginData
Expand Down
19 changes: 7 additions & 12 deletions CodeHub.Core/Factories/LoginFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ public async Task<Client> LoginAccount(GitHubAccount account)
account.Username = userInfo.Login;
account.AvatarUrl = userInfo.AvatarUrl;
client.Username = userInfo.Login;
_accounts.Update(account);

if (_accounts.Exists(account))
_accounts.Update(account);
else
_accounts.Insert(account);
return client;
}

public async Task<LoginData> Authenticate(string domain, string user, string pass, string twoFactor, bool enterprise, GitHubAccount account)
public async Task<LoginData> CreateLoginData(string domain, string user, string pass, string twoFactor, bool enterprise, GitHubAccount account)
{
//Fill these variables in during the proceeding try/catch
var apiUrl = domain;
Expand All @@ -77,11 +81,7 @@ public async Task<LoginData> Authenticate(string domain, string user, string pas
if (apiUrl != null && !Uri.IsWellFormedUriString(apiUrl, UriKind.Absolute))
throw new ArgumentException("Domain is invalid");

//Does this user exist?
bool exists = account != null;
if (!exists)
account = new GitHubAccount { Username = user };

account = account ?? new GitHubAccount { Username = user };
account.Domain = apiUrl;
account.IsEnterprise = enterprise;
var client = twoFactor == null ? Client.Basic(user, pass, apiUrl) : Client.BasicTwoFactorAuthentication(user, pass, twoFactor, apiUrl);
Expand All @@ -96,11 +96,6 @@ public async Task<LoginData> Authenticate(string domain, string user, string pas
account.OAuth = auth.Data.Token;
}

if (exists)
_accounts.Update(account);
else
_accounts.Insert(account);

return new LoginData { Client = client, Account = account };
}
catch (StatusCodeException ex)
Expand Down
4 changes: 2 additions & 2 deletions CodeHub.Core/ViewModels/Accounts/AccountsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected async override void SelectAccount(IAccount account)
//Hack for now
if (isEnterprise)
{
ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id });
ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { AttemptedAccountId = account.Id });
}
else
{
Expand All @@ -58,7 +58,7 @@ protected async override void SelectAccount(IAccount account)
DisplayAlert("The credentials for the selected account are incorrect. " + e.Message);

if (isEnterprise)
ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = githubAccount.Id });
ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { AttemptedAccountId = githubAccount.Id });
else
ShowViewModel<LoginViewModel>(LoginViewModel.NavObject.CreateDontRemember(githubAccount));
}
Expand Down
16 changes: 3 additions & 13 deletions CodeHub.Core/ViewModels/Accounts/AddAccountViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class AddAccountViewModel : BaseViewModel
private string _domain;
private bool _isLoggingIn;

public bool IsEnterprise { get; private set; }

public bool IsLoggingIn
{
get { return _isLoggingIn; }
Expand Down Expand Up @@ -66,13 +64,7 @@ public void Init(NavObject navObject)
if (_attemptedAccount != null)
{
Username = _attemptedAccount.Username;
IsEnterprise = _attemptedAccount.Domain != null;
if (IsEnterprise)
Domain = _attemptedAccount.Domain;
}
else
{
IsEnterprise = navObject.IsEnterprise;
Domain = _attemptedAccount.Domain;
}
}

Expand All @@ -85,7 +77,7 @@ private bool CanLogin()

private async Task Login()
{
var apiUrl = IsEnterprise ? Domain : null;
var apiUrl = Domain;
if (apiUrl != null)
{
if (!apiUrl.StartsWith("http://") && !apiUrl.StartsWith("https://"))
Expand All @@ -99,8 +91,7 @@ private async Task Login()
try
{
IsLoggingIn = true;
Console.WriteLine(apiUrl);
var loginData = await _loginFactory.Authenticate(apiUrl, Username, Password, TwoFactor, IsEnterprise, _attemptedAccount);
var loginData = await _loginFactory.CreateLoginData(apiUrl, Username, Password, TwoFactor, true, _attemptedAccount);
var client = await _loginFactory.LoginAccount(loginData.Account);
_application.ActivateUser(loginData.Account, client);
}
Expand All @@ -123,7 +114,6 @@ private async Task Login()

public class NavObject
{
public bool IsEnterprise { get; set; }
public int AttemptedAccountId { get; set; }

public NavObject()
Expand Down
5 changes: 0 additions & 5 deletions CodeHub.Core/ViewModels/Accounts/LoginViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public string LoginUrl

public string WebDomain { get; set; }

public ICommand GoToOldLoginWaysCommand
{
get { return new MvxCommand(() => ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = IsEnterprise })); }
}

public ICommand GoBackCommand
{
get { return new MvxCommand(() => ChangePresentation(new MvxClosePresentationHint(this))); }
Expand Down
2 changes: 1 addition & 1 deletion CodeHub.Core/ViewModels/Accounts/NewAccountViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public ICommand GoToDotComLoginCommand

public ICommand GoToEnterpriseLoginCommand
{
get { return new MvxCommand(() => this.ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = true })); }
get { return new MvxCommand(() => this.ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject())); }
}
}
}
13 changes: 1 addition & 12 deletions CodeHub.Core/ViewModels/App/StartupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ protected async override void Startup()
if (account.DontRemember)
{
ShowViewModel<Accounts.AccountsViewModel>();

//Hack for now
if (isEnterprise)
{
ShowViewModel<Accounts.AddAccountViewModel>(new Accounts.AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id });
}
else
{
ShowViewModel<Accounts.LoginViewModel>(Accounts.LoginViewModel.NavObject.CreateDontRemember(account));
}

return;
}

Expand All @@ -71,7 +60,7 @@ protected async override void Startup()

ShowViewModel<Accounts.AccountsViewModel>();
if (isEnterprise)
ShowViewModel<Accounts.AddAccountViewModel>(new Accounts.AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id });
ShowViewModel<Accounts.AddAccountViewModel>(new Accounts.AddAccountViewModel.NavObject { AttemptedAccountId = account.Id });
else
ShowViewModel<Accounts.LoginViewModel>(Accounts.LoginViewModel.NavObject.CreateDontRemember(account));
}
Expand Down
13 changes: 3 additions & 10 deletions CodeHub.Core/ViewModels/FileSourceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ protected set
_filePath = value;
RaisePropertyChanged(() => FilePath);
}
}
}

public bool IsMarkdown { get; protected set; }

public string ContentPath
{
Expand Down Expand Up @@ -61,15 +63,6 @@ public ICommand ShareCommand
}
}

protected string CreateContentFile()
{
var html = System.IO.File.ReadAllText("SourceBrowser/index.html");
var filled = html.Replace("{CODE_PATH}", "file://" + FilePath + "#" + System.Environment.TickCount);
var filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "source.html");
System.IO.File.WriteAllText(filepath, filled, System.Text.Encoding.UTF8);
return filepath;
}

protected static string CreatePlainContentFile(string data, string filename)
{
var filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);
Expand Down
38 changes: 24 additions & 14 deletions CodeHub.Core/ViewModels/Gists/GistFileViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using CodeHub.Core.ViewModels;
using System.Threading.Tasks;
using GitHubSharp.Models;
using CodeHub.Core.ViewModels;
Expand All @@ -11,7 +10,18 @@ public class GistFileViewModel : FileSourceViewModel
{
private string _id;
private string _filename;
private GistFileModel _fileModel;

public string FileName { get; private set; }

private GistFileModel _gist;
public GistFileModel Gist
{
get { return _gist; }
private set {
_gist = value;
RaisePropertyChanged();
}
}

public void Init(NavObject navObject)
{
Expand All @@ -22,33 +32,33 @@ public void Init(NavObject navObject)

//Create the temp file path
Title = fileName;
FileName = fileName;

_id = navObject.GistId;
_filename = navObject.Filename;

//Grab the data
_fileModel = GetService<IViewModelTxService>().Get() as GistFileModel;
Gist = GetService<IViewModelTxService>().Get() as GistFileModel;
}

protected override async Task Load(bool forceCacheInvalidation)
{
if (_fileModel == null)

if (Gist == null)
{
var data = await this.GetApplication().Client.ExecuteAsync(this.GetApplication().Client.Gists[_id].Get());
_fileModel = data.Data.Files[_filename];
Gist = data.Data.Files[_filename];
}

//Check to make sure...
if (_fileModel == null || _fileModel.Content == null)
{
if (Gist == null || Gist.Content == null)
throw new Exception("Unable to retreive gist!");
}

var content = _fileModel.Content;
var filePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetFileName(_fileModel.Filename));
System.IO.File.WriteAllText(filePath, content, System.Text.Encoding.UTF8);
FilePath = filePath;
ContentPath = CreateContentFile();
IsMarkdown = string.Equals(Gist?.Language, "Markdown");
Gist = Gist;

var filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), FileName);
System.IO.File.WriteAllText(filepath, Gist.Content, System.Text.Encoding.UTF8);
ContentPath = FilePath = filepath;
}

public class NavObject
Expand Down
10 changes: 8 additions & 2 deletions CodeHub.Core/ViewModels/Source/SourceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
using CodeHub.Core.Messages;
using MvvmCross.Plugins.Messenger;
using MvvmCross.Core.ViewModels;
using System.Linq;

namespace CodeHub.Core.ViewModels.Source
{
public class SourceViewModel : FileSourceViewModel
{
private readonly MvxSubscriptionToken _editToken;
private static readonly string[] MarkdownExtensions = { ".markdown", ".mdown", ".mkdn", ".md", ".mkd", ".mdwn", ".mdtxt", ".mdtext", ".text" };

private string _path;
private string _name;
Expand All @@ -27,7 +29,8 @@ public class SourceViewModel : FileSourceViewModel

protected override async Task Load(bool forceCacheInvalidation)
{
var filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetFileName(_name));
var fileName = System.IO.Path.GetFileName(_name);
var filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), fileName);
string mime = string.Empty;

using (var stream = new System.IO.FileStream(filepath, System.IO.FileMode.Create, System.IO.FileAccess.Write))
Expand All @@ -44,7 +47,7 @@ protected override async Task Load(bool forceCacheInvalidation)
var isText = mime.Contains("charset");
if (isText)
{
ContentPath = CreateContentFile();
ContentPath = FilePath;
}
}

Expand Down Expand Up @@ -84,6 +87,9 @@ public void Init(NavObject navObject)

//Create the temp file path
Title = fileName;

var extension = System.IO.Path.GetExtension(_path);
IsMarkdown = MarkdownExtensions.Contains(extension);
}

public class NavObject
Expand Down
Loading

0 comments on commit e2b1c2a

Please sign in to comment.