-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
SnackbarServiceClient.lua
106 lines (82 loc) · 2.5 KB
/
SnackbarServiceClient.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
--[=[
Guarantees that only one snackbar is visible at once
@class SnackbarServiceClient
]=]
local require = require(script.Parent.loader).load(script)
local Snackbar = require("Snackbar")
local SnackbarScreenGuiProvider = require("SnackbarScreenGuiProvider")
local Maid = require("Maid")
local SnackbarOptionUtils = require("SnackbarOptionUtils")
local PromptQueue = require("PromptQueue")
local SnackbarServiceClient = {}
SnackbarServiceClient.ServiceName = "SnackbarServiceClient"
--[=[
Initializes the snackbar service. Should be done via [ServiceBag].
@param serviceBag ServiceBag
]=]
function SnackbarServiceClient:Init(serviceBag)
assert(not self._serviceBag, "Already initialized")
self._serviceBag = assert(serviceBag, "No serviceBag")
self._maid = Maid.new()
self._snackbarScreenGuiProvider = self._serviceBag:GetService(SnackbarScreenGuiProvider)
self._screenGui = self._maid:Add(self._snackbarScreenGuiProvider:Get("SNACKBAR"))
self._queue = self._maid:Add(PromptQueue.new())
end
--[=[
Sets the screenGui to use
@param screenGui ScreenGui
@return SnackbarServiceClient
]=]
function SnackbarServiceClient:SetScreenGui(screenGui)
self._screenGui = screenGui or error("No screenGui")
return self
end
--[=[
Makes a snackbar and shows it to the user in a queue.
```lua
local snackbarServiceClient = serviceBag:GetService(SnackbarServiceClient)
snackbarServiceClient:ShowSnackbar("Settings saved!", {
CallToAction = {
Text = "Undo";
OnClick = function()
print("Activated action")
end;
}
})
```
@param text string
@param options SnackbarOptions
]=]
function SnackbarServiceClient:ShowSnackbar(text, options)
assert(type(text) == "string", "text must be a string")
assert(SnackbarOptionUtils.isSnackbarOptions(options) or options == nil, "Bad snackbarOptions")
local snackbar = Snackbar.new(text, options)
snackbar.Gui.Parent = self._screenGui
self._queue:HideCurrent()
self._maid:GivePromise(self._queue:Queue(snackbar))
:Finally(function()
snackbar:Destroy()
end)
return snackbar
end
--[=[
Hides the current snackbar shown in the queue
@param doNotAnimate boolean
]=]
function SnackbarServiceClient:HideCurrent(doNotAnimate)
return self._queue:HideCurrent(doNotAnimate)
end
--[=[
Completely clears the queue
@param doNotAnimate boolean
]=]
function SnackbarServiceClient:ClearQueue(doNotAnimate)
self._queue:Clear(doNotAnimate)
end
--[=[
Cleans up the snackbar service!
]=]
function SnackbarServiceClient:Destroy()
self._maid:DoCleaning()
end
return SnackbarServiceClient