Merge pull request #1315 from ogayot/no-remove-overlay

ubuntu-pro: don't assume an overlay is present in show_* functions
This commit is contained in:
Olivier Gayot 2022-06-22 16:51:55 +02:00 committed by GitHub
commit e5ac3e32e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 19 deletions

View File

@ -16,7 +16,7 @@
import asyncio
import logging
from typing import Optional
from typing import Callable, List, Optional
from subiquitycore.async_helpers import schedule_task
@ -24,6 +24,7 @@ from subiquity.client.controller import SubiquityTuiController
from subiquity.common.types import (
UbuntuProInfo,
UbuntuProCheckTokenStatus as TokenStatus,
UbuntuProService,
)
from subiquity.ui.views.ubuntu_pro import UbuntuProView
@ -61,22 +62,17 @@ class UbuntuProController(SubiquityTuiController):
if "token" in self.answers:
self.done(self.answers["token"])
def check_token(self, token: str):
def check_token(self, token: str,
on_success: Callable[[List[UbuntuProService]], None],
on_failure: Callable[[TokenStatus], None],
) -> None:
""" Asynchronously check the token passed as an argument. """
async def inner() -> None:
answer = await self.endpoint.check_token.GET(token)
if answer.status == TokenStatus.INVALID_TOKEN:
if isinstance(self.ui.body, UbuntuProView):
self.ui.body.show_invalid_token()
elif answer.status == TokenStatus.EXPIRED_TOKEN:
if isinstance(self.ui.body, UbuntuProView):
self.ui.body.show_expired_token()
elif answer.status == TokenStatus.UNKNOWN_ERROR:
if isinstance(self.ui.body, UbuntuProView):
self.ui.body.show_unknown_error()
if answer.status == TokenStatus.VALID_TOKEN:
on_success(answer.services)
else:
if isinstance(self.ui.body, UbuntuProView):
self.ui.body.show_activable_services(answer.services)
on_failure(answer.status)
self._check_task = schedule_task(inner())

View File

@ -25,7 +25,10 @@ from urwid import (
Widget,
)
from subiquity.common.types import UbuntuProService
from subiquity.common.types import (
UbuntuProCheckTokenStatus,
UbuntuProService,
)
from subiquitycore.view import BaseView
from subiquitycore.ui.buttons import (
back_btn,
@ -146,11 +149,27 @@ class UbuntuProView(BaseView):
asynchronously check if the token is valid. """
token: str = form.token.value
if token:
def on_success(services: List[UbuntuProService]) -> None:
self.remove_overlay()
self.show_activable_services(services)
def on_failure(status: UbuntuProCheckTokenStatus) -> None:
self.remove_overlay()
if status == UbuntuProCheckTokenStatus.INVALID_TOKEN:
self.show_invalid_token()
elif status == UbuntuProCheckTokenStatus.EXPIRED_TOKEN:
self.show_expired_token()
elif status == UbuntuProCheckTokenStatus.UNKNOWN_ERROR:
self.show_unknown_error()
checking_token_overlay = CheckingUAToken(self)
self.show_overlay(checking_token_overlay,
width=checking_token_overlay.width,
min_width=None)
self.controller.check_token(token)
self.controller.check_token(token,
on_success=on_success,
on_failure=on_failure)
else:
self.controller.done(token)
@ -161,7 +180,6 @@ class UbuntuProView(BaseView):
def show_invalid_token(self) -> None:
""" Display an overlay that indicates that the user-supplied token is
invalid. """
self.remove_overlay()
self.show_stretchy_overlay(
SomethingFailed(self,
"Invalid token.",
@ -172,7 +190,6 @@ class UbuntuProView(BaseView):
def show_expired_token(self) -> None:
""" Display an overlay that indicates that the user-supplied token has
expired. """
self.remove_overlay()
self.show_stretchy_overlay(
SomethingFailed(self,
"Token expired.",
@ -185,7 +202,6 @@ class UbuntuProView(BaseView):
network connection, temporary service unavailability, API issue ...
The user is prompted to continue anyway or go back.
"""
self.remove_overlay()
self.show_stretchy_overlay(ContinueAnywayWidget(self))
def show_activable_services(self,
@ -193,7 +209,6 @@ class UbuntuProView(BaseView):
""" Display an overlay with the list of services that can be enabled
via Ubuntu Pro subscription. After the user confirms, we will
quit the current view and move on. """
self.remove_overlay()
self.show_stretchy_overlay(ShowServicesWidget(self, services))