feat: remove account

This commit is contained in:
Rongjian Zhang 2020-02-01 18:30:32 +08:00
parent 2795dedf83
commit 5aa7483c90
5 changed files with 53 additions and 4 deletions

View File

@ -56,6 +56,17 @@ class AuthModel with ChangeNotifier {
await prefs.setString(StorageKeys.accounts, json.encode(_accounts));
}
removeAccount(int index) async {
if (activeAccountIndex == index) {
activeAccountIndex = null;
}
_accounts.removeAt(index);
// Save
final prefs = await SharedPreferences.getInstance();
await prefs.setString(StorageKeys.accounts, json.encode(_accounts));
notifyListeners();
}
// https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#web-application-flow
Future<void> _onSchemeDetected(Uri uri) async {
await closeWebView();

View File

@ -408,6 +408,7 @@ class ThemeModel with ChangeNotifier {
actions: actionItems.asMap().entries.map((entry) {
return CupertinoActionSheetAction(
child: Text(entry.value.text),
isDestructiveAction: entry.value.isDestructiveAction,
onPressed: () {
Navigator.pop(context, entry.key);
},

View File

@ -5,6 +5,7 @@ import 'package:git_touch/models/theme.dart';
import 'package:git_touch/scaffolds/single.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/action_button.dart';
import 'package:git_touch/widgets/action_entry.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/text_field.dart';
import 'package:provider/provider.dart';
@ -37,6 +38,17 @@ class _LoginScreenState extends State<LoginScreen> {
onTap: () {
auth.setActiveAccountAndReload(index);
},
onLongPress: () {
theme.showActions(context, [
ActionItem(
text: 'Remove account',
isDestructiveAction: true,
onTap: (_) {
auth.removeAccount(index);
},
),
]);
},
child: Container(
padding: CommonStyle.padding,
decoration: BoxDecoration(
@ -50,9 +62,15 @@ class _LoginScreenState extends State<LoginScreen> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(account.login, style: TextStyle(fontSize: 20)),
Text(
account.login,
style: TextStyle(fontSize: 20, color: theme.palette.text),
),
Padding(padding: EdgeInsets.only(top: 6)),
Text(account.domain)
Text(
account.domain,
style: TextStyle(color: theme.palette.secondaryText),
)
],
),
),
@ -229,6 +247,16 @@ class _LoginScreenState extends State<LoginScreen> {
}
}
},
),
Container(
padding: CommonStyle.padding,
child: Text(
'Note: Long press to remove account',
style: TextStyle(
fontSize: 16,
color: theme.palette.secondaryText,
),
),
)
],
),

View File

@ -8,6 +8,7 @@ import 'package:share/share.dart';
class ActionItem {
String text;
String url;
bool isDestructiveAction;
void Function(BuildContext context) onTap;
IconData iconData;
@ -16,6 +17,7 @@ class ActionItem {
this.onTap,
this.url,
this.iconData,
this.isDestructiveAction = false,
});
static List<ActionItem> getUrlActions(String url) {

View File

@ -51,11 +51,13 @@ class Link extends StatelessWidget {
final Widget child;
final String url;
final Function onTap;
final Function onLongPress;
Link({
this.child,
@required this.child,
this.url,
this.onTap,
this.onLongPress,
});
@override
@ -64,7 +66,7 @@ class Link extends StatelessWidget {
switch (theme.theme) {
case AppThemeType.cupertino:
return CupertinoButton(
Widget w = CupertinoButton(
minSize: 0,
child: child,
padding: EdgeInsets.zero,
@ -73,6 +75,10 @@ class Link extends StatelessWidget {
if (url != null) theme.push(context, url);
},
);
if (onLongPress != null) {
w = GestureDetector(onLongPress: onLongPress, child: w);
}
return w;
default:
return InkWell(
child: child,
@ -80,6 +86,7 @@ class Link extends StatelessWidget {
if (onTap != null) onTap();
if (url != null) theme.push(context, url);
},
onLongPress: onLongPress,
);
}
}