11import 'package:flutter/material.dart' ;
2+ import 'package:gsy_github_app_flutter/common/repositories/repos_repository.dart' ;
23import 'package:gsy_github_app_flutter/model/event.dart' ;
34import 'package:gsy_github_app_flutter/model/push_event_commit.dart' ;
5+ import 'package:gsy_github_app_flutter/model/repo_commit.dart' ;
46import 'package:gsy_github_app_flutter/common/utils/common_utils.dart' ;
57import 'package:gsy_github_app_flutter/common/utils/navigator_utils.dart' ;
68
79/// 事件逻辑
810/// Created by guoshuyu
911/// Date: 2018-07-16
1012class EventUtils {
13+ static bool _isInvalidCompareBase (String ? sha) {
14+ if (sha == null || sha.isEmpty) {
15+ return true ;
16+ }
17+ return RegExp (r'^0+$' ).hasMatch (sha);
18+ }
19+
20+ static String _shortSha (String ? sha, [int length = 7 ]) {
21+ if (sha == null || sha.isEmpty) {
22+ return "" ;
23+ }
24+ if (sha.length <= length) {
25+ return sha;
26+ }
27+ return sha.substring (0 , length);
28+ }
29+
1130 ///事件描述与动作
1231 static ({String ? actionStr, String ? des}) getActionAndDes (Event event) {
1332 String ? actionStr;
@@ -96,26 +115,37 @@ class EventUtils {
96115 ref = ref.substring (ref.lastIndexOf ("/" ) + 1 );
97116 actionStr = "Push to $ref at ${event .repo !.name !}" ;
98117
99- des = '' ;
100- String descSpan = '' ;
101-
102- int count = event.payload? .commits? .length ?? 0 ;
118+ String descSpan = "" ;
119+ List <PushEventCommit > commits = event.payload? .commits ?? [];
120+ int count = commits.length;
103121 int maxLines = 4 ;
104122 int max = count > maxLines ? maxLines - 1 : count;
105123
106124 for (int i = 0 ; i < max; i++ ) {
107- PushEventCommit commit = event.payload ! . commits! [i];
125+ PushEventCommit commit = commits[i];
108126 if (i != 0 ) {
109127 descSpan += ("\n " );
110128 }
111- String sha = commit.sha! . substring ( 0 , 7 );
129+ String sha = _shortSha ( commit.sha);
112130 descSpan += sha;
113131 descSpan += " " ;
114- descSpan += commit.message! ;
132+ descSpan += ( commit.message ?? "Commit" ) ;
115133 }
116134 if (count > maxLines) {
117135 descSpan = "$descSpan \n ..." ;
118136 }
137+ if (descSpan.trim ().isNotEmpty) {
138+ des = descSpan;
139+ } else if (event.payload? .description != null &&
140+ event.payload! .description! .trim ().isNotEmpty) {
141+ des = event.payload! .description;
142+ } else if (event.payload? .head != null &&
143+ event.payload! .head! .trim ().isNotEmpty) {
144+ String head = _shortSha (event.payload! .head);
145+ des = "head: $head " ;
146+ } else {
147+ des = "" ;
148+ }
119149 } else {
120150 actionStr = "" ;
121151 }
@@ -133,7 +163,11 @@ class EventUtils {
133163 }
134164
135165 ///跳转
136- static ActionUtils (BuildContext context, Event event, currentRepository) {
166+ static Future <void > ActionUtils (
167+ BuildContext context,
168+ Event event,
169+ currentRepository,
170+ ) async {
137171 if (event.repo == null ) {
138172 NavigatorUtils .goPerson (context, event.actor! .login);
139173 return ;
@@ -153,32 +187,138 @@ class EventUtils {
153187 );
154188 break ;
155189 case 'PushEvent' :
156- if (event.payload! .commits == null ) {
190+ List <PushEventCommit > commits = event.payload? .commits ?? [];
191+ String ? beforeSha = event.payload? .before;
192+ String ? headSha = event.payload? .head;
193+ List <RepoCommit > compareCommits = [];
194+
195+ if (! _isInvalidCompareBase (beforeSha) &&
196+ headSha != null &&
197+ headSha.isNotEmpty &&
198+ beforeSha != headSha) {
199+ CommonUtils .showLoadingDialog (context);
200+ try {
201+ var compareRes = await ReposRepository .getReposCompareRequest (
202+ owner,
203+ repositoryName,
204+ beforeSha! ,
205+ headSha,
206+ );
207+ if (compareRes != null &&
208+ compareRes.result &&
209+ compareRes.data != null ) {
210+ compareCommits = compareRes.data.commits ?? [];
211+ }
212+ } finally {
213+ if (context.mounted) {
214+ Navigator .pop (context);
215+ }
216+ }
217+ }
218+ if (! context.mounted) {
219+ return ;
220+ }
221+
222+ if (compareCommits.length == 1 && compareCommits.first.sha != null ) {
223+ NavigatorUtils .goPushDetailPage (
224+ context,
225+ owner,
226+ repositoryName,
227+ compareCommits.first.sha,
228+ true ,
229+ );
230+ return ;
231+ }
232+
233+ if (compareCommits.length > 1 ) {
234+ StringList list = [];
235+ for (int i = 0 ; i < compareCommits.length; i++ ) {
236+ RepoCommit commit = compareCommits[i];
237+ String message =
238+ commit.commit? .message? .split ('\n ' ).first.trim () ?? "Commit" ;
239+ if (message.isEmpty) {
240+ message = "Commit" ;
241+ }
242+ list.add ("$message ${_shortSha (commit .sha , 4 )}" );
243+ }
244+ CommonUtils .showCommitOptionDialog (context, list, (index) {
245+ NavigatorUtils .goPushDetailPage (
246+ context,
247+ owner,
248+ repositoryName,
249+ compareCommits[index].sha,
250+ true ,
251+ );
252+ });
253+ return ;
254+ }
255+
256+ if (commits.isEmpty) {
257+ if (headSha != null && headSha.isNotEmpty) {
258+ NavigatorUtils .goPushDetailPage (
259+ context,
260+ owner,
261+ repositoryName,
262+ headSha,
263+ true ,
264+ );
265+ return ;
266+ }
157267 if (fullName.toLowerCase () == currentRepository.toLowerCase ()) {
158268 return ;
159269 }
160270 NavigatorUtils .goReposDetail (context, owner, repositoryName);
161- } else if (event.payload ! . commits! .length == 1 ) {
271+ } else if (commits.length == 1 && commits.first.sha != null ) {
162272 NavigatorUtils .goPushDetailPage (
163273 context,
164274 owner,
165275 repositoryName,
166- event.payload ! . commits! [ 0 ] .sha,
276+ commits.first .sha,
167277 true ,
168278 );
169279 } else {
170- StringList list = [];
171- for (int i = 0 ; i < event.payload! .commits! .length; i++ ) {
172- list.add (
173- "${event .payload !.commits ![i ].message !} ${event .payload !.commits ![i ].sha !.substring (0 , 4 )}" ,
280+ List <PushEventCommit > validCommits = commits
281+ .where ((item) => item.sha != null && item.sha! .isNotEmpty)
282+ .toList ();
283+ if (validCommits.isEmpty) {
284+ if (headSha != null && headSha.isNotEmpty) {
285+ NavigatorUtils .goPushDetailPage (
286+ context,
287+ owner,
288+ repositoryName,
289+ headSha,
290+ true ,
291+ );
292+ } else {
293+ NavigatorUtils .goReposDetail (context, owner, repositoryName);
294+ }
295+ return ;
296+ }
297+ if (validCommits.length == 1 ) {
298+ NavigatorUtils .goPushDetailPage (
299+ context,
300+ owner,
301+ repositoryName,
302+ validCommits.first.sha,
303+ true ,
174304 );
305+ return ;
306+ }
307+ StringList list = [];
308+ for (int i = 0 ; i < validCommits.length; i++ ) {
309+ PushEventCommit commit = validCommits[i];
310+ String shaShort = _shortSha (commit.sha, 4 );
311+ String message = (commit.message == null || commit.message! .isEmpty)
312+ ? "Commit"
313+ : commit.message! ;
314+ list.add ("$message $shaShort " );
175315 }
176316 CommonUtils .showCommitOptionDialog (context, list, (index) {
177317 NavigatorUtils .goPushDetailPage (
178318 context,
179319 owner,
180320 repositoryName,
181- event.payload ! .commits ! [index].sha,
321+ validCommits [index].sha,
182322 true ,
183323 );
184324 });
0 commit comments