55using System . Text . Json . Nodes ;
66using GitHubExtension . Client ;
77using GitHubExtension . DataManager ;
8- using GitHubExtension . Helpers ;
98using GitHubExtension . Widgets . Enums ;
109using Microsoft . Windows . Widgets . Providers ;
1110
@@ -15,6 +14,8 @@ public abstract class GitHubRepositoryWidget : GitHubWidget
1514{
1615 protected string RepositoryUrl { get ; set ; } = string . Empty ;
1716
17+ private string ? _message ;
18+
1819 public GitHubRepositoryWidget ( )
1920 : base ( )
2021 {
@@ -66,14 +67,14 @@ public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs)
6667
6768 switch ( verb )
6869 {
69- case WidgetAction . CheckUrl :
70- HandleCheckUrl ( actionInvokedArgs ) ;
71- break ;
72-
7370 case WidgetAction . Save :
74- UpdateTitle ( JsonNode . Parse ( actionInvokedArgs . Data ) ) ;
75- base . OnActionInvoked ( actionInvokedArgs ) ;
76- CorrectUrl ( ) ;
71+ if ( HandleCheckUrl ( actionInvokedArgs ) )
72+ {
73+ UpdateTitle ( JsonNode . Parse ( actionInvokedArgs . Data ) ) ;
74+ base . OnActionInvoked ( actionInvokedArgs ) ;
75+ CorrectUrl ( ) ;
76+ }
77+
7778 break ;
7879
7980 default :
@@ -142,10 +143,11 @@ private void UpdateTitle(JsonNode? dataObj)
142143 }
143144
144145 GetTitleFromDataObject ( dataObj ) ;
145- if ( string . IsNullOrEmpty ( WidgetTitle ) )
146- {
147- WidgetTitle = GetRepositoryFromUrl ( RepositoryUrl ) . FullName ;
148- }
146+ }
147+
148+ protected string GetActualTitle ( )
149+ {
150+ return string . IsNullOrEmpty ( WidgetTitle ) ? GetRepositoryFromUrl ( RepositoryUrl ) . FullName : WidgetTitle ;
149151 }
150152
151153 protected override void ResetWidgetInfoFromState ( )
@@ -207,7 +209,7 @@ public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs c
207209 base . OnCustomizationRequested ( customizationRequestedArgs ) ;
208210 }
209211
210- private void HandleCheckUrl ( WidgetActionInvokedArgs args )
212+ private bool HandleCheckUrl ( WidgetActionInvokedArgs args )
211213 {
212214 // Set loading page while we fetch data from GitHub.
213215 Page = WidgetPageState . Loading ;
@@ -223,7 +225,19 @@ private void HandleCheckUrl(WidgetActionInvokedArgs args)
223225 RepositoryUrl = dataObject [ "url" ] ? . GetValue < string > ( ) ?? string . Empty ;
224226 UpdateTitle ( dataObject ) ;
225227
226- ConfigurationData = data ;
228+ var isGoodToSave = true ;
229+
230+ try
231+ {
232+ GetRepositoryFromUrl ( RepositoryUrl ) ;
233+ ConfigurationData = data ;
234+ _message = null ;
235+ }
236+ catch ( Exception ex )
237+ {
238+ _message = ex . Message ;
239+ isGoodToSave = false ;
240+ }
227241
228242 var updateRequestOptions = new WidgetUpdateRequestOptions ( Id )
229243 {
@@ -233,71 +247,29 @@ private void HandleCheckUrl(WidgetActionInvokedArgs args)
233247 } ;
234248
235249 WidgetManager . GetDefault ( ) . UpdateWidget ( updateRequestOptions ) ;
250+
251+ // Already shown error message while updating above,
252+ // can reset it to null here.
253+ _message = null ;
254+ return isGoodToSave ;
236255 }
256+
257+ UpdateWidget ( ) ;
258+
259+ return false ;
237260 }
238261
239262 public string GetConfiguration ( string dataUrl )
240263 {
241264 var configurationData = new JsonObject
242265 {
243- { "submitIcon" , IconLoader . GetIconAsBase64 ( "arrow.png" ) } ,
244266 { "widgetTitle" , WidgetTitle } ,
267+ { "url" , dataUrl } ,
268+ { "savedRepositoryUrl" , SavedConfigurationData } ,
269+ { "errorMessage" , _message } ,
245270 } ;
246271
247- if ( dataUrl == string . Empty )
248- {
249- configurationData . Add ( "hasConfiguration" , false ) ;
250- var repositoryData = new JsonObject
251- {
252- { "url" , string . Empty } ,
253- } ;
254-
255- configurationData . Add ( "configuration" , repositoryData ) ;
256- configurationData . Add ( "savedRepositoryUrl" , SavedConfigurationData ) ;
257- configurationData . Add ( "saveEnabled" , false ) ;
258-
259- return configurationData . ToString ( ) ;
260- }
261- else
262- {
263- try
264- {
265- var repository = GetRepositoryFromUrl ( dataUrl ) ;
266- var repositoryData = new JsonObject
267- {
268- { "name" , repository . FullName } ,
269- { "label" , repository . Name } ,
270- { "owner" , repository . Owner . Login } ,
271- { "milestone" , string . Empty } ,
272- { "project" , repository . Description } ,
273- { "url" , RepositoryUrl } ,
274- { "query" , GetUnescapedIssueQuery ( ) } ,
275- } ;
276-
277- configurationData . Add ( "hasConfiguration" , true ) ;
278- configurationData . Add ( "configuration" , repositoryData ) ;
279- configurationData . Add ( "savedRepositoryUrl" , SavedConfigurationData ) ;
280- configurationData . Add ( "saveEnabled" , true ) ;
281- }
282- catch ( Exception ex )
283- {
284- Log . Error ( ex , $ "Failed getting configuration information for input url: { dataUrl } ") ;
285- configurationData . Add ( "hasConfiguration" , false ) ;
286-
287- var repositoryData = new JsonObject
288- {
289- { "url" , RepositoryUrl } ,
290- } ;
291-
292- configurationData . Add ( "errorMessage" , ex . Message ) ;
293- configurationData . Add ( "configuration" , repositoryData ) ;
294- configurationData . Add ( "saveEnabled" , false ) ;
295-
296- return configurationData . ToString ( ) ;
297- }
298-
299- return configurationData . ToJsonString ( ) ;
300- }
272+ return configurationData . ToJsonString ( ) ;
301273 }
302274
303275 public override string GetData ( WidgetPageState page )
0 commit comments