Skip to content

Commit ff1dcc6

Browse files
authored
Merge pull request #128 from jackBonadies/loginUpdate
Event handler on Login Started (since thats what matters to the user and determines an "in flight" login). Also fix bug where we can have 2 concurrent ContinueWith task chains.
2 parents 04b6069 + f27ee46 commit ff1dcc6

4 files changed

Lines changed: 53 additions & 12 deletions

File tree

‎Seeker/Login/LoginFragment.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public override void OnResume()
8787
MessagesBroadcastReceiver.MarkAsReadFromNotification += OnMarkAsReadUpdateBadge;
8888
UpdateUnreadBadge();
8989

90+
SessionService.LoginStarted += OnLoginStarted;
9091
SessionService.LoginCompleted += OnLoginCompleted;
9192
ServerMessageService.Changed += OnServerMessageChanged;
9293
RenderFromState();
@@ -99,6 +100,7 @@ public override void OnPause()
99100
SeekerState.SoulseekClient.StateChanged -= SoulseekClient_StateChanged;
100101
MessageController.MessageReceived -= OnMessageReceivedUpdateBadge;
101102
MessagesBroadcastReceiver.MarkAsReadFromNotification -= OnMarkAsReadUpdateBadge;
103+
SessionService.LoginStarted -= OnLoginStarted;
102104
SessionService.LoginCompleted -= OnLoginCompleted;
103105
ServerMessageService.Changed -= OnServerMessageChanged;
104106
}
@@ -127,6 +129,14 @@ private void UpdateServerMessage()
127129
}
128130
}
129131

132+
private void OnLoginStarted(object sender, EventArgs e)
133+
{
134+
this.Activity?.RunOnUiThread(() =>
135+
{
136+
UpdateConnectionStatus(SeekerState.SoulseekClient.State);
137+
});
138+
}
139+
130140
private void OnLoginCompleted(object sender, LoginCompletedEventArgs e)
131141
{
132142
this.Activity?.RunOnUiThread(() =>
@@ -347,7 +357,8 @@ public void UpdateConnectionStatus(SoulseekClientStates state)
347357
textColorResId = Resource.Color.statusConnectedText;
348358
chipBgColorResId = Resource.Color.statusConnectedChipBg;
349359
}
350-
else if (state.HasFlag(SoulseekClientStates.Connecting) || state.HasFlag(SoulseekClientStates.LoggingIn))
360+
else if (state.HasFlag(SoulseekClientStates.Connecting) || state.HasFlag(SoulseekClientStates.LoggingIn)
361+
|| SessionService.InFlightLoginOrigin != null) // i.e. resolving DNS or any pre-connect step
351362
{
352363
textResId = Resource.String.status_connecting;
353364
dotColorResId = Resource.Color.statusConnectingDot;

‎Seeker/Properties/AndroidManifest.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="143" android:versionName="3.11.1" package="com.companyname.andriodapp1" android:installLocation="auto">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="144" android:versionName="3.11.2" package="com.companyname.andriodapp1" android:installLocation="auto">
33
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="false" android:enableOnBackInvokedCallback="true" android:theme="@style/AppTheme" android:localeConfig="@xml/locales_config">
44
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true">
55
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />

‎Seeker/SeekerApplication.cs‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public enum DnsLookupResult
131131
public static Task<IPAddress> ResolveServerAddressAsync()
132132
{
133133
#if MOCK
134-
return Task.Delay(250).ContinueWith(task => IPAddress.Loopback);
134+
return Task.Delay(1000).ContinueWith(task => IPAddress.Loopback);
135135
#else
136136
return ResolveAddressAsync(ServerHost);
137137
#endif
@@ -432,7 +432,7 @@ private void SoulseekClient_Disconnected(object sender, SoulseekClientDisconnect
432432
Logger.Debug("disconnected");
433433
lock (OurCurrentLoginTaskSyncObject)
434434
{
435-
OurCurrentLoginTask = null;
435+
CurrentLoginTask = null;
436436
}
437437
}
438438

@@ -723,7 +723,7 @@ private void SoulseekClient_LoggedIn(object sender, EventArgs e)
723723
{
724724
lock (OurCurrentLoginTaskSyncObject)
725725
{
726-
OurCurrentLoginTask = null;
726+
CurrentLoginTask = null;
727727
}
728728
ChatroomController.JoinAutoJoinRoomsAndPreviousJoined();
729729
//ChatroomController.ConnectionLapse.Add(new Tuple<bool, DateTime>(true, DateTime.UtcNow)); //just testing obv remove later...
@@ -821,12 +821,23 @@ public static void SetUpLoginContinueWith(Task t)
821821
}
822822
}
823823

824-
public static Task OurCurrentLoginTask = null;
824+
// the full outer login task (including DNS resolution) or the task we chained on after
825+
public static Task CurrentLoginTask = null;
826+
// the inner SoulseekClient.ConnectAsync task
827+
public static Task CurrentInnerConnectTask = null;
825828
public static object OurCurrentLoginTaskSyncObject = new object();
826829
public static Task ConnectAndPerformPostConnectTasks(IPAddress server, string username, string password)
827830
{
828831
Task t = SeekerState.SoulseekClient.ConnectAsync(server.ToString(), ServerPort, username, password);
829-
OurCurrentLoginTask = t;
832+
lock (OurCurrentLoginTaskSyncObject)
833+
{
834+
CurrentInnerConnectTask = t;
835+
// only overwrite the loginTask if it is null or completed
836+
if (CurrentLoginTask == null || CurrentLoginTask.IsCompleted)
837+
{
838+
CurrentLoginTask = t;
839+
}
840+
}
830841
t.ContinueWith(PerformPostConnectTasks);
831842
return t;
832843
}

‎Seeker/Services/SessionService.cs‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ private static Task InFlightLogin
7979
}
8080
}
8181

82+
// for pre SoulseekClient.ConnectAsync tasks
83+
public static event EventHandler LoginStarted;
84+
8285
public static event EventHandler<LoginCompletedEventArgs> LoginCompleted;
8386

8487
/// <summary>
@@ -113,6 +116,15 @@ public static Task BeginLogin(LoginOrigin origin, string username, string passwo
113116

114117
// We set the handle after the connect task finishes
115118
soulseekClientConnectTask.ContinueWith(t => OnLoginTaskCompleted(t, handle), TaskScheduler.Default);
119+
120+
try
121+
{
122+
LoginStarted?.Invoke(null, EventArgs.Empty);
123+
}
124+
catch (Exception e)
125+
{
126+
Logger.FirebaseError("LoginStarted handler failed", e);
127+
}
116128
return handle.Task;
117129
}
118130

@@ -146,8 +158,12 @@ private static Task AdoptConnectInProgress()
146158
return Task.CompletedTask;
147159
}
148160

149-
Task adopted = SeekerApplication.OurCurrentLoginTask;
150-
if (adopted == null)
161+
Task adopted;
162+
lock (SeekerApplication.OurCurrentLoginTaskSyncObject)
163+
{
164+
adopted = SeekerApplication.CurrentInnerConnectTask;
165+
}
166+
if (adopted == null || adopted.IsCompleted)
151167
{
152168
return Task.FromException(new InvalidOperationException("BeginLogin: connect underway with no task to adopt"));
153169
}
@@ -462,15 +478,15 @@ public bool IfLoggingInTaskCurrentlyBeingPerformedContinueWithAction(Action<Task
462478
}
463479

464480
// both are fine to continue with on
465-
Task loginTask = SeekerApplication.OurCurrentLoginTask ?? InFlightLogin;
481+
Task loginTask = SeekerApplication.CurrentLoginTask ?? InFlightLogin;
466482
if (loginTask == null)
467483
{
468484
// this should never happen
469485
Logger.Firebase("RunWithReconnect: not logged in and no login in flight, state " + state);
470486
loginTask = Task.FromException(new InvalidOperationException("not logged in and no login in flight"));
471487
}
472488

473-
SeekerApplication.OurCurrentLoginTask = loginTask.ContinueWith(action, System.Threading.CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default);
489+
SeekerApplication.CurrentLoginTask = loginTask.ContinueWith(action, System.Threading.CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default);
474490
if (msg != null)
475491
{
476492
SeekerApplication.Toaster.ShowToast(msg, ToastLength.Short);
@@ -529,7 +545,10 @@ public void RunWithReconnect(Action<Task> continuationAction, string loggingInMs
529545
Task.FromException(new Exception("could not start reconnect")).ContinueWith(continuationAction);
530546
return;
531547
}
532-
SeekerApplication.OurCurrentLoginTask = t.ContinueWith(continuationAction);
548+
lock (SeekerApplication.OurCurrentLoginTaskSyncObject)
549+
{
550+
SeekerApplication.CurrentLoginTask = t.ContinueWith(continuationAction);
551+
}
533552
}
534553
else if (IfLoggingInTaskCurrentlyBeingPerformedContinueWithAction(continuationAction, loggingInMsg, contextForMsg))
535554
{

0 commit comments

Comments
 (0)