Skip to content

Commit

Permalink
feat: add diff view
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Feb 29, 2020
1 parent 4ea30d0 commit aa52263
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 11 deletions.
14 changes: 3 additions & 11 deletions lib/screens/gl_commit.dart
@@ -1,9 +1,9 @@
import 'package:flutter/material.dart';
import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/gitlab.dart';
import 'package:git_touch/models/code.dart';
import 'package:git_touch/scaffolds/refresh_stateful.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/diff_view.dart';
import 'package:provider/provider.dart';

class GlCommitScreen extends StatelessWidget {
Expand All @@ -20,22 +20,14 @@ class GlCommitScreen extends StatelessWidget {

@override
Widget build(BuildContext context) {
final code = Provider.of<CodeModel>(context);

return RefreshStatefulScaffold<List<GitlabDiff>>(
title: AppBarTitle('Commits'),
fetchData: () => _query(context),
bodyBuilder: (items, _) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
for (var item in items)
Text(
item.diff,
style: TextStyle(
fontFamily: code.fontFamilyUsed,
fontSize: 14,
),
)
for (var item in items) DiffView(item.diff),
],
);
},
Expand Down
84 changes: 84 additions & 0 deletions lib/widgets/diff_view.dart
@@ -0,0 +1,84 @@
import 'package:flutter/material.dart';
import 'package:git_touch/models/code.dart';
import 'package:git_touch/models/theme.dart';
import 'package:provider/provider.dart';

class DiffLine {
String type;
int lineNumber;
String content;
DiffLine({
@required this.type,
@required this.lineNumber,
@required this.content,
});
}

class DiffChunk {
String heading;
List<DiffLine> lines;
DiffChunk({@required this.heading, @required this.lines});
}

class DiffView extends StatelessWidget {
final String source;
DiffView(this.source);

@override
Widget build(BuildContext context) {
final code = Provider.of<CodeModel>(context);
final theme = Provider.of<ThemeModel>(context);

final lines = source.split('\n');
final chunks = <DiffChunk>[];
var offset = 0;
for (final line in lines) {
if (line.startsWith('@@')) {
chunks.add(DiffChunk(heading: line, lines: []));
offset = int.parse(line.substring(4).split(',')[0]);
} else {
chunks.last.lines.add(DiffLine(
type: line.isEmpty ? null : line[0],
lineNumber: offset,
content: line,
));
offset++;
}
}

return DefaultTextStyle(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (var c in chunks)
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
c.heading,
style: TextStyle(
color: theme.palette.tertiaryText,
backgroundColor: Color(0xfffafafa),
),
),
for (var l in c.lines)
Text(
l.content,
style: TextStyle(
backgroundColor: l.type == '-'
? Color(0x00fbe9eb)
: l.type == '+' ? Color(0xffecfdf0) : null,
),
),
],
),
],
),
style: TextStyle(
fontFamily: code.fontFamilyUsed,
fontSize: 14,
color: theme.palette.text,
),
);
}
}

0 comments on commit aa52263

Please sign in to comment.