Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix about dialog #288

Merged
merged 1 commit into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions buzz/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,14 +666,23 @@ def get_asset_path(path: str):


class AboutDialog(QDialog):
def __init__(self, parent: Optional[QWidget] = None) -> None:
GITHUB_API_LATEST_RELEASE_URL = 'https://api.github.com/repos/chidiwilliams/buzz/releases/latest'
GITHUB_LATEST_RELEASE_URL = 'https://github.com/chidiwilliams/buzz/releases/latest'

def __init__(self, network_access_manager: Optional[QNetworkAccessManager]=None, parent: Optional[QWidget] = None) -> None:
super().__init__(parent)

self.setFixedSize(200, 250)

self.setWindowIcon(QIcon(BUZZ_ICON_PATH))
self.setWindowTitle(f'About {APP_NAME}')

if network_access_manager is None:
network_access_manager = QNetworkAccessManager()

self.network_access_manager = network_access_manager
self.network_access_manager.finished.connect(self.on_latest_release_reply)

layout = QVBoxLayout(self)

image_label = QLabel()
Expand All @@ -695,8 +704,8 @@ def __init__(self, parent: Optional[QWidget] = None) -> None:
version_label.setAlignment(Qt.AlignmentFlag(
Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignHCenter))

check_updates_button = QPushButton('Check for updates', self)
check_updates_button.clicked.connect(self.on_click_check_for_updates)
self.check_updates_button = QPushButton('Check for updates', self)
self.check_updates_button.clicked.connect(self.on_click_check_for_updates)

button_box = QDialogButtonBox(QDialogButtonBox.StandardButton(
QDialogButtonBox.StandardButton.Close), self)
Expand All @@ -706,22 +715,29 @@ def __init__(self, parent: Optional[QWidget] = None) -> None:
layout.addWidget(image_label)
layout.addWidget(buzz_label)
layout.addWidget(version_label)
layout.addWidget(check_updates_button)
layout.addWidget(self.check_updates_button)
layout.addWidget(button_box)

self.setLayout(layout)

def on_click_check_for_updates(self):
response = get(
'https://api.github.com/repos/chidiwilliams/buzz/releases/latest', timeout=15).json()
version_number = response.field('name')
if version_number == 'v' + VERSION:
dialog = QMessageBox(self)
dialog.setText("You're up to date!")
dialog.open()
else:
QDesktopServices.openUrl(
QUrl('https://github.com/chidiwilliams/buzz/releases/latest'))
url = QUrl(self.GITHUB_API_LATEST_RELEASE_URL)
self.network_access_manager.get(QNetworkRequest(url))
self.check_updates_button.setDisabled(True)

def on_latest_release_reply(self, reply: QNetworkReply):
if reply.error() == QNetworkReply.NetworkError.NoError:
response = json.loads(reply.readAll().data())
tag_name = response.get('name')
if self.is_version_lower(VERSION, tag_name[1:]):
QDesktopServices.openUrl(QUrl(self.GITHUB_LATEST_RELEASE_URL))
else:
QMessageBox.information(self, '', "You're up to date!")
self.check_updates_button.setEnabled(True)

@staticmethod
def is_version_lower(version_a: str, version_b: str):
return version_a.replace('.', '') < version_b.replace('.', '')


class TranscriptionTasksTableWidget(QTableWidget):
Expand Down Expand Up @@ -784,7 +800,8 @@ def upsert_task(self, task: FileTranscriptionTask):

def clear_task(self, task_id: int):
task_row_index = self.task_row_index(task_id)
self.removeRow(task_row_index)
if task_row_index is not None:
self.removeRow(task_row_index)

def task_row_index(self, task_id: int) -> int | None:
table_items_matching_task_id = [item for item in self.findItems(str(task_id), Qt.MatchFlag.MatchExactly) if
Expand Down Expand Up @@ -1267,7 +1284,7 @@ def on_import_action_triggered(self):
self.import_action_triggered.emit()

def on_about_action_triggered(self):
about_dialog = AboutDialog(self)
about_dialog = AboutDialog(parent=self)
about_dialog.open()


Expand Down
20 changes: 16 additions & 4 deletions tests/gui_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import sounddevice
from PyQt6.QtCore import QSize, Qt, QByteArray, QObject
from PyQt6.QtGui import QValidator, QKeyEvent
from PyQt6.QtWidgets import QPushButton, QToolBar, QTableWidget, QApplication
from PyQt6.QtWidgets import QPushButton, QToolBar, QTableWidget, QApplication, QMessageBox
from pytestqt.qtbot import QtBot

from buzz.__version__ import VERSION

from .mock_qt import MockNetworkAccessManager, MockNetworkReply
from buzz.cache import TasksCache
from buzz.gui import (AboutDialog, AdvancedSettingsDialog, AudioDevicesComboBox, DownloadModelProgressDialog,
Expand Down Expand Up @@ -206,9 +208,19 @@ def test_should_emit_triggered_event(self, qtbot: QtBot):


class TestAboutDialog:
def test_should_create(self):
dialog = AboutDialog()
assert dialog is not None
def test_should_check_for_updates(self, qtbot: QtBot):
reply = MockNetworkReply(data={'name': 'v' + VERSION})
manager = MockNetworkAccessManager(reply=reply)
dialog = AboutDialog(network_access_manager=manager)
qtbot.add_widget(dialog)

mock_message_box_information = Mock()
QMessageBox.information = mock_message_box_information

with qtbot.wait_signal(dialog.network_access_manager.finished):
dialog.check_updates_button.click()

mock_message_box_information.assert_called_with(dialog, '', "You're up to date!")


class TestAdvancedSettingsDialog:
Expand Down