refactor: extname as string extension

This commit is contained in:
Rongjian Zhang 2020-01-31 14:54:46 +08:00
parent db92210659
commit 32d29f6f0b
3 changed files with 20 additions and 17 deletions

View File

@ -24,9 +24,9 @@ final objectRouter = RouterScreen('/:owner/:name/blob/:ref', (context, params) {
class ObjectScreen extends StatelessWidget {
final String owner;
final String name;
final String branch;
final String ref;
final String path;
ObjectScreen(this.owner, this.name, this.branch, {this.path});
ObjectScreen(this.owner, this.name, this.ref, {this.path});
@override
Widget build(BuildContext context) {
@ -35,8 +35,8 @@ class ObjectScreen extends StatelessWidget {
title: AppBarTitle(path == null ? 'Files' : path),
fetchData: () async {
final suffix = path == null ? '' : '/$path';
final res = await Provider.of<AuthModel>(context).getWithCredentials(
'/repos/$owner/$name/contents$suffix?ref=$branch');
final res = await Provider.of<AuthModel>(context)
.getWithCredentials('/repos/$owner/$name/contents$suffix?ref=$ref');
return res;
},
actionBuilder: (data, _) {
@ -60,10 +60,8 @@ class ObjectScreen extends StatelessWidget {
items: items.map((v) {
// if (item.type == 'commit') return null;
String url;
var ext = p.extension(v.name);
if (ext.startsWith('.')) ext = ext.substring(1);
if (['pdf', 'docx', 'doc', 'pptx', 'ppt', 'xlsx', 'xls']
.contains(ext)) {
.contains(v.name.ext)) {
// Let system browser handle these files
//
// TODO:
@ -71,7 +69,7 @@ class ObjectScreen extends StatelessWidget {
// https://github.com/flutter/flutter/issues/49162
url = v.downloadUrl;
} else {
url = '/$owner/$name/blob/$branch?path=${v.path.urlencode}';
url = '/$owner/$name/blob/$ref?path=${v.path.urlencode}';
}
return ObjectTreeItem(

View File

@ -1,4 +1,5 @@
import 'dart:convert';
import 'package:path/path.dart' as p;
extension MyString<T extends String> on String {
int get toInt => int.parse(this);
@ -6,4 +7,15 @@ extension MyString<T extends String> on String {
String get urldecode => Uri.decodeComponent(this);
String get dropLineBreak => this.replaceAll('\n', '');
String get base64ToUtf8 => utf8.decode(base64.decode(this));
/// Get extension by file name/path, returns `null` instead of empty string
///
/// 1.dart -> 'dart'
///
/// license -> null
String get ext {
final dotext = p.extension(this);
if (dotext.isEmpty) return null;
return dotext.substring(1);
}
}

View File

@ -1,5 +1,4 @@
import 'dart:convert';
import 'package:path/path.dart' as p;
import 'package:flutter/cupertino.dart';
import 'package:flutter_highlight/theme_map.dart';
import 'package:flutter_svg/flutter_svg.dart';
@ -24,19 +23,13 @@ class BlobView extends StatelessWidget {
this.networkUrl,
});
String get _extname {
var dotext = p.extension(name);
if (dotext.isEmpty) return '';
return dotext.substring(1);
}
String get _language => _extname.isEmpty ? 'plaintext' : _extname;
String get _language => name.ext ?? 'plaintext';
@override
Widget build(BuildContext context) {
final codeProvider = Provider.of<CodeModel>(context);
final theme = Provider.of<ThemeModel>(context);
switch (_extname) {
switch (name.ext) {
// TODO: All image types
case 'png':
case 'jpg':