bauiv1lib.playlist.share
UI functionality for importing shared playlists.
1# Released under the MIT License. See LICENSE for details. 2# 3"""UI functionality for importing shared playlists.""" 4 5from __future__ import annotations 6 7import time 8from typing import TYPE_CHECKING 9 10from bauiv1lib.promocode import PromoCodeWindow 11import bauiv1 as bui 12 13if TYPE_CHECKING: 14 from typing import Any, Callable 15 16 17class SharePlaylistImportWindow(PromoCodeWindow): 18 """Window for importing a shared playlist.""" 19 20 def __init__( 21 self, 22 origin_widget: bui.Widget | None = None, 23 on_success_callback: Callable[[], Any] | None = None, 24 ): 25 PromoCodeWindow.__init__(self, modal=True, origin_widget=origin_widget) 26 self._on_success_callback = on_success_callback 27 28 def _on_import_response(self, response: dict[str, Any] | None) -> None: 29 if response is None: 30 bui.screenmessage(bui.Lstr(resource='errorText'), color=(1, 0, 0)) 31 bui.getsound('error').play() 32 return 33 34 if response['playlistType'] == 'Team Tournament': 35 playlist_type_name = bui.Lstr(resource='playModes.teamsText') 36 elif response['playlistType'] == 'Free-for-All': 37 playlist_type_name = bui.Lstr(resource='playModes.freeForAllText') 38 else: 39 playlist_type_name = bui.Lstr(value=response['playlistType']) 40 41 bui.screenmessage( 42 bui.Lstr( 43 resource='importPlaylistSuccessText', 44 subs=[ 45 ('${TYPE}', playlist_type_name), 46 ('${NAME}', response['playlistName']), 47 ], 48 ), 49 color=(0, 1, 0), 50 ) 51 bui.getsound('gunCocking').play() 52 if self._on_success_callback is not None: 53 self._on_success_callback() 54 bui.containerwidget( 55 edit=self._root_widget, transition=self._transition_out 56 ) 57 58 def _do_enter(self) -> None: 59 plus = bui.app.plus 60 assert plus is not None 61 62 plus.add_v1_account_transaction( 63 { 64 'type': 'IMPORT_PLAYLIST', 65 'expire_time': time.time() + 5, 66 'code': bui.textwidget(query=self._text_field), 67 }, 68 callback=bui.WeakCall(self._on_import_response), 69 ) 70 plus.run_v1_account_transactions() 71 bui.screenmessage(bui.Lstr(resource='importingText')) 72 73 74class SharePlaylistResultsWindow(bui.Window): 75 """Window for sharing playlists.""" 76 77 def __init__( 78 self, name: str, data: str, origin: tuple[float, float] = (0.0, 0.0) 79 ): 80 del origin # unused arg 81 self._width = 450 82 self._height = 300 83 assert bui.app.classic is not None 84 uiscale = bui.app.ui_v1.uiscale 85 super().__init__( 86 root_widget=bui.containerwidget( 87 size=(self._width, self._height), 88 color=(0.45, 0.63, 0.15), 89 transition='in_scale', 90 scale=( 91 1.8 92 if uiscale is bui.UIScale.SMALL 93 else 1.35 94 if uiscale is bui.UIScale.MEDIUM 95 else 1.0 96 ), 97 ) 98 ) 99 bui.getsound('cashRegister').play() 100 bui.getsound('swish').play() 101 102 self._cancel_button = bui.buttonwidget( 103 parent=self._root_widget, 104 scale=0.7, 105 position=(40, self._height - 40), 106 size=(50, 50), 107 label='', 108 on_activate_call=self.close, 109 autoselect=True, 110 color=(0.45, 0.63, 0.15), 111 icon=bui.gettexture('crossOut'), 112 iconscale=1.2, 113 ) 114 bui.containerwidget( 115 edit=self._root_widget, cancel_button=self._cancel_button 116 ) 117 118 bui.textwidget( 119 parent=self._root_widget, 120 position=(self._width * 0.5, self._height * 0.745), 121 size=(0, 0), 122 color=bui.app.ui_v1.infotextcolor, 123 scale=1.0, 124 flatness=1.0, 125 h_align='center', 126 v_align='center', 127 text=bui.Lstr( 128 resource='exportSuccessText', subs=[('${NAME}', name)] 129 ), 130 maxwidth=self._width * 0.85, 131 ) 132 133 bui.textwidget( 134 parent=self._root_widget, 135 position=(self._width * 0.5, self._height * 0.645), 136 size=(0, 0), 137 color=bui.app.ui_v1.infotextcolor, 138 scale=0.6, 139 flatness=1.0, 140 h_align='center', 141 v_align='center', 142 text=bui.Lstr(resource='importPlaylistCodeInstructionsText'), 143 maxwidth=self._width * 0.85, 144 ) 145 146 bui.textwidget( 147 parent=self._root_widget, 148 position=(self._width * 0.5, self._height * 0.4), 149 size=(0, 0), 150 color=(1.0, 3.0, 1.0), 151 scale=2.3, 152 h_align='center', 153 v_align='center', 154 text=data, 155 maxwidth=self._width * 0.85, 156 ) 157 158 def close(self) -> None: 159 """Close the window.""" 160 bui.containerwidget(edit=self._root_widget, transition='out_scale')