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

Commit e2b1c2a

Browse files
committed
Improvements for Source Code
1 parent b2960c7 commit e2b1c2a

File tree

123 files changed

+7605
-4262
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+7605
-4262
lines changed

CodeHub.Core/Factories/ILoginFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface ILoginFactory
99

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

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

1515
public class LoginData

CodeHub.Core/Factories/LoginFactory.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ public async Task<Client> LoginAccount(GitHubAccount account)
5858
account.Username = userInfo.Login;
5959
account.AvatarUrl = userInfo.AvatarUrl;
6060
client.Username = userInfo.Login;
61-
_accounts.Update(account);
61+
62+
if (_accounts.Exists(account))
63+
_accounts.Update(account);
64+
else
65+
_accounts.Insert(account);
6266
return client;
6367
}
6468

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

80-
//Does this user exist?
81-
bool exists = account != null;
82-
if (!exists)
83-
account = new GitHubAccount { Username = user };
84-
84+
account = account ?? new GitHubAccount { Username = user };
8585
account.Domain = apiUrl;
8686
account.IsEnterprise = enterprise;
8787
var client = twoFactor == null ? Client.Basic(user, pass, apiUrl) : Client.BasicTwoFactorAuthentication(user, pass, twoFactor, apiUrl);
@@ -96,11 +96,6 @@ public async Task<LoginData> Authenticate(string domain, string user, string pas
9696
account.OAuth = auth.Data.Token;
9797
}
9898

99-
if (exists)
100-
_accounts.Update(account);
101-
else
102-
_accounts.Insert(account);
103-
10499
return new LoginData { Client = client, Account = account };
105100
}
106101
catch (StatusCodeException ex)

CodeHub.Core/ViewModels/Accounts/AccountsViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected async override void SelectAccount(IAccount account)
3737
//Hack for now
3838
if (isEnterprise)
3939
{
40-
ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id });
40+
ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { AttemptedAccountId = account.Id });
4141
}
4242
else
4343
{
@@ -58,7 +58,7 @@ protected async override void SelectAccount(IAccount account)
5858
DisplayAlert("The credentials for the selected account are incorrect. " + e.Message);
5959

6060
if (isEnterprise)
61-
ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = githubAccount.Id });
61+
ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { AttemptedAccountId = githubAccount.Id });
6262
else
6363
ShowViewModel<LoginViewModel>(LoginViewModel.NavObject.CreateDontRemember(githubAccount));
6464
}

CodeHub.Core/ViewModels/Accounts/AddAccountViewModel.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public class AddAccountViewModel : BaseViewModel
1919
private string _domain;
2020
private bool _isLoggingIn;
2121

22-
public bool IsEnterprise { get; private set; }
23-
2422
public bool IsLoggingIn
2523
{
2624
get { return _isLoggingIn; }
@@ -66,13 +64,7 @@ public void Init(NavObject navObject)
6664
if (_attemptedAccount != null)
6765
{
6866
Username = _attemptedAccount.Username;
69-
IsEnterprise = _attemptedAccount.Domain != null;
70-
if (IsEnterprise)
71-
Domain = _attemptedAccount.Domain;
72-
}
73-
else
74-
{
75-
IsEnterprise = navObject.IsEnterprise;
67+
Domain = _attemptedAccount.Domain;
7668
}
7769
}
7870

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

8678
private async Task Login()
8779
{
88-
var apiUrl = IsEnterprise ? Domain : null;
80+
var apiUrl = Domain;
8981
if (apiUrl != null)
9082
{
9183
if (!apiUrl.StartsWith("http://") && !apiUrl.StartsWith("https://"))
@@ -99,8 +91,7 @@ private async Task Login()
9991
try
10092
{
10193
IsLoggingIn = true;
102-
Console.WriteLine(apiUrl);
103-
var loginData = await _loginFactory.Authenticate(apiUrl, Username, Password, TwoFactor, IsEnterprise, _attemptedAccount);
94+
var loginData = await _loginFactory.CreateLoginData(apiUrl, Username, Password, TwoFactor, true, _attemptedAccount);
10495
var client = await _loginFactory.LoginAccount(loginData.Account);
10596
_application.ActivateUser(loginData.Account, client);
10697
}
@@ -123,7 +114,6 @@ private async Task Login()
123114

124115
public class NavObject
125116
{
126-
public bool IsEnterprise { get; set; }
127117
public int AttemptedAccountId { get; set; }
128118

129119
public NavObject()

CodeHub.Core/ViewModels/Accounts/LoginViewModel.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ public string LoginUrl
4949

5050
public string WebDomain { get; set; }
5151

52-
public ICommand GoToOldLoginWaysCommand
53-
{
54-
get { return new MvxCommand(() => ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = IsEnterprise })); }
55-
}
56-
5752
public ICommand GoBackCommand
5853
{
5954
get { return new MvxCommand(() => ChangePresentation(new MvxClosePresentationHint(this))); }

CodeHub.Core/ViewModels/Accounts/NewAccountViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public ICommand GoToDotComLoginCommand
1313

1414
public ICommand GoToEnterpriseLoginCommand
1515
{
16-
get { return new MvxCommand(() => this.ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject { IsEnterprise = true })); }
16+
get { return new MvxCommand(() => this.ShowViewModel<AddAccountViewModel>(new AddAccountViewModel.NavObject())); }
1717
}
1818
}
1919
}

CodeHub.Core/ViewModels/App/StartupViewModel.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,6 @@ protected async override void Startup()
3838
if (account.DontRemember)
3939
{
4040
ShowViewModel<Accounts.AccountsViewModel>();
41-
42-
//Hack for now
43-
if (isEnterprise)
44-
{
45-
ShowViewModel<Accounts.AddAccountViewModel>(new Accounts.AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id });
46-
}
47-
else
48-
{
49-
ShowViewModel<Accounts.LoginViewModel>(Accounts.LoginViewModel.NavObject.CreateDontRemember(account));
50-
}
51-
5241
return;
5342
}
5443

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

7261
ShowViewModel<Accounts.AccountsViewModel>();
7362
if (isEnterprise)
74-
ShowViewModel<Accounts.AddAccountViewModel>(new Accounts.AddAccountViewModel.NavObject { IsEnterprise = true, AttemptedAccountId = account.Id });
63+
ShowViewModel<Accounts.AddAccountViewModel>(new Accounts.AddAccountViewModel.NavObject { AttemptedAccountId = account.Id });
7564
else
7665
ShowViewModel<Accounts.LoginViewModel>(Accounts.LoginViewModel.NavObject.CreateDontRemember(account));
7766
}

CodeHub.Core/ViewModels/FileSourceViewModel.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ protected set
2424
_filePath = value;
2525
RaisePropertyChanged(() => FilePath);
2626
}
27-
}
27+
}
28+
29+
public bool IsMarkdown { get; protected set; }
2830

2931
public string ContentPath
3032
{
@@ -61,15 +63,6 @@ public ICommand ShareCommand
6163
}
6264
}
6365

64-
protected string CreateContentFile()
65-
{
66-
var html = System.IO.File.ReadAllText("SourceBrowser/index.html");
67-
var filled = html.Replace("{CODE_PATH}", "file://" + FilePath + "#" + System.Environment.TickCount);
68-
var filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "source.html");
69-
System.IO.File.WriteAllText(filepath, filled, System.Text.Encoding.UTF8);
70-
return filepath;
71-
}
72-
7366
protected static string CreatePlainContentFile(string data, string filename)
7467
{
7568
var filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename);

CodeHub.Core/ViewModels/Gists/GistFileViewModel.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using CodeHub.Core.ViewModels;
32
using System.Threading.Tasks;
43
using GitHubSharp.Models;
54
using CodeHub.Core.ViewModels;
@@ -11,7 +10,18 @@ public class GistFileViewModel : FileSourceViewModel
1110
{
1211
private string _id;
1312
private string _filename;
14-
private GistFileModel _fileModel;
13+
14+
public string FileName { get; private set; }
15+
16+
private GistFileModel _gist;
17+
public GistFileModel Gist
18+
{
19+
get { return _gist; }
20+
private set {
21+
_gist = value;
22+
RaisePropertyChanged();
23+
}
24+
}
1525

1626
public void Init(NavObject navObject)
1727
{
@@ -22,33 +32,33 @@ public void Init(NavObject navObject)
2232

2333
//Create the temp file path
2434
Title = fileName;
35+
FileName = fileName;
2536

2637
_id = navObject.GistId;
2738
_filename = navObject.Filename;
2839

2940
//Grab the data
30-
_fileModel = GetService<IViewModelTxService>().Get() as GistFileModel;
41+
Gist = GetService<IViewModelTxService>().Get() as GistFileModel;
3142
}
3243

3344
protected override async Task Load(bool forceCacheInvalidation)
3445
{
35-
if (_fileModel == null)
46+
47+
if (Gist == null)
3648
{
3749
var data = await this.GetApplication().Client.ExecuteAsync(this.GetApplication().Client.Gists[_id].Get());
38-
_fileModel = data.Data.Files[_filename];
50+
Gist = data.Data.Files[_filename];
3951
}
4052

41-
//Check to make sure...
42-
if (_fileModel == null || _fileModel.Content == null)
43-
{
53+
if (Gist == null || Gist.Content == null)
4454
throw new Exception("Unable to retreive gist!");
45-
}
4655

47-
var content = _fileModel.Content;
48-
var filePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetFileName(_fileModel.Filename));
49-
System.IO.File.WriteAllText(filePath, content, System.Text.Encoding.UTF8);
50-
FilePath = filePath;
51-
ContentPath = CreateContentFile();
56+
IsMarkdown = string.Equals(Gist?.Language, "Markdown");
57+
Gist = Gist;
58+
59+
var filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), FileName);
60+
System.IO.File.WriteAllText(filepath, Gist.Content, System.Text.Encoding.UTF8);
61+
ContentPath = FilePath = filepath;
5262
}
5363

5464
public class NavObject

CodeHub.Core/ViewModels/Source/SourceViewModel.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
using CodeHub.Core.Messages;
66
using MvvmCross.Plugins.Messenger;
77
using MvvmCross.Core.ViewModels;
8+
using System.Linq;
89

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

1517
private string _path;
1618
private string _name;
@@ -27,7 +29,8 @@ public class SourceViewModel : FileSourceViewModel
2729

2830
protected override async Task Load(bool forceCacheInvalidation)
2931
{
30-
var filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetFileName(_name));
32+
var fileName = System.IO.Path.GetFileName(_name);
33+
var filepath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), fileName);
3134
string mime = string.Empty;
3235

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

@@ -84,6 +87,9 @@ public void Init(NavObject navObject)
8487

8588
//Create the temp file path
8689
Title = fileName;
90+
91+
var extension = System.IO.Path.GetExtension(_path);
92+
IsMarkdown = MarkdownExtensions.Contains(extension);
8793
}
8894

8995
public class NavObject

0 commit comments

Comments
 (0)