Skip to content

Commit 3387986

Browse files
authored
Merge pull request #58 from jwdeveloper/develop-1.4.0-Gifts-Update
Update gifts manager
2 parents a68eaba + 0fcac60 commit 3387986

File tree

90 files changed

+351
-189431
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+351
-189431
lines changed

API/src/main/java/io/github/jwdeveloper/tiktok/data/models/gifts/Gift.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
import java.util.*;
88

99
@Data
10-
@AllArgsConstructor
11-
public class Gift
12-
{
13-
@Getter private static final Set<Gift> gifts = new HashSet<>();
14-
public static final Gift UNDEFINED = new Gift(-1, "undefined", -1, "", null);
10+
public class Gift {
11+
public static final Gift UNDEFINED = new Gift(-1, "undefined", -1, "");
1512

1613
private final int id;
1714

@@ -23,14 +20,23 @@ public class Gift
2320

2421
private final JsonObject properties;
2522

26-
public Gift(int id, String name, int diamondCost, String pictureLink, JsonObject properties) {
23+
public Gift(int id, String name, int diamondCost, Picture pictureLink, JsonObject properties) {
2724
this.id = id;
2825
this.name = name;
2926
this.diamondCost = diamondCost;
30-
this.picture = new Picture(pictureLink);
27+
this.picture = pictureLink;
3128
this.properties = properties;
3229
}
3330

31+
32+
public Gift(int id, String name, int diamondCost, String pictureLink) {
33+
this(id, name, diamondCost, new Picture(pictureLink), new JsonObject());
34+
}
35+
36+
public Gift(int id, String name, int diamondCost, Picture picture) {
37+
this(id, name, diamondCost, picture, new JsonObject());
38+
}
39+
3440
public boolean hasDiamondCostRange(int minimalCost, int maximalCost) {
3541
return diamondCost >= minimalCost && diamondCost <= maximalCost;
3642
}

API/src/main/java/io/github/jwdeveloper/tiktok/data/requests/GiftsData.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
package io.github.jwdeveloper.tiktok.data.requests;
2424

25+
import io.github.jwdeveloper.tiktok.data.models.gifts.Gift;
2526
import lombok.AllArgsConstructor;
2627
import lombok.Data;
2728
import lombok.Getter;
@@ -41,16 +42,7 @@ public final class Request
4142
public static final class Response
4243
{
4344
private String json;
44-
private List<GiftModel> gifts;
45-
}
46-
47-
@Data
48-
public static class GiftModel
49-
{
50-
private int id;
51-
private String name;
52-
private int diamondCost;
53-
private String image;
45+
private List<Gift> gifts;
5446
}
5547

5648
}

API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/LiveClientSettings.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
@Data
3434
public class LiveClientSettings {
3535

36+
37+
/**
38+
* Determines if gifts data is downloaded before TikTokLive starts,
39+
* when `false` then client.giftManager() does not contain initial gifts
40+
*/
41+
private boolean fetchGifts;
42+
3643
/**
3744
* ISO-Language for Client
3845
*/

API/src/main/java/io/github/jwdeveloper/tiktok/http/LiveHttpClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public interface LiveHttpClient
3434
*/
3535
GiftsData.Response fetchGiftsData();
3636

37+
3738
/**
3839
* Returns information about user that is having a livestream
3940
* @param userName name of user

API/src/main/java/io/github/jwdeveloper/tiktok/live/GiftManager.java renamed to API/src/main/java/io/github/jwdeveloper/tiktok/live/GiftsManager.java

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,62 @@
2525
import com.google.gson.JsonObject;
2626
import io.github.jwdeveloper.tiktok.data.models.Picture;
2727
import io.github.jwdeveloper.tiktok.data.models.gifts.*;
28+
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
2829

30+
import java.util.Collections;
2931
import java.util.List;
32+
import java.util.Map;
33+
import java.util.function.Predicate;
3034

31-
public interface GiftManager {
35+
public interface GiftsManager {
3236

3337
/**
34-
* In case you can't find your gift in Gift enum. You can register gift
35-
* manually here to make it detected while TikTokGiftEvent
38+
* You can create and attach your own custom gift to manager
3639
*
37-
* @param id gift's id
38-
* @param name gift's name
39-
* @param diamondCost diamond cost
40-
* @return
40+
* @param gift
4141
*/
42-
default Gift registerGift(int id, String name, int diamondCost, Picture picture) {
43-
return registerGift(id, name, diamondCost, picture, null);
44-
}
45-
46-
Gift registerGift(int id, String name, int diamondCost, Picture picture, JsonObject properties);
42+
void attachGift(Gift gift);
4743

4844
/**
45+
* You can create and attach your own custom gift to manager
4946
*
50-
* @param giftId
51-
* @return
47+
* @param gifts
5248
*/
53-
Gift findById(int giftId);
49+
void attachGiftsList(List<Gift> gifts);
5450

5551
/**
52+
* finds gift by name
53+
* When gift not found return Gift.UNDEFINED;
5654
*
57-
* @param giftName
58-
* @return
55+
* @param name gift name
5956
*/
60-
Gift findByName(String giftName);
57+
Gift getByName(String name);
6158

6259
/**
60+
* finds gift by id
61+
* When gift not found return Gift.UNDEFINED;
6362
*
64-
* @return all gifts
63+
* @param giftId giftId
64+
*/
65+
Gift getById(int giftId);
66+
67+
68+
/**
69+
* finds gift by filter
70+
* When gift not found return Gift.UNDEFINED;
71+
*/
72+
Gift getByFilter(Predicate<Gift> filter);
73+
74+
List<Gift> getManyByFilter(Predicate<Gift> filter);
75+
76+
/**
77+
* @return list of all gifts
78+
*/
79+
List<Gift> toList();
80+
81+
82+
/**
83+
* @return list of all map of all gifts where Integer is gift Id
6584
*/
66-
List<Gift> getGifts();
85+
Map<Integer, Gift> toMap();
6786
}

API/src/main/java/io/github/jwdeveloper/tiktok/live/LiveClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public interface LiveClient {
6464
/**
6565
* Get information about gifts
6666
*/
67-
GiftManager getGiftManager();
67+
GiftsManager getGiftManager();
6868

6969
/**
7070
* Gets the current room info from TikTok API including streamer info, room status and statistics.

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLive.java

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@
2323
package io.github.jwdeveloper.tiktok;
2424

2525

26+
import io.github.jwdeveloper.tiktok.gifts.TikTokGiftsManager;
2627
import io.github.jwdeveloper.tiktok.http.LiveHttpClient;
28+
import io.github.jwdeveloper.tiktok.live.GiftsManager;
2729
import io.github.jwdeveloper.tiktok.live.builder.LiveClientBuilder;
2830

31+
import java.util.List;
2932
import java.util.concurrent.CompletableFuture;
3033

3134
public class TikTokLive {
3235

3336
/**
3437
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
38+
*
3539
* @param hostName profile name of Tiktok user could be found in profile link
3640
* @return LiveClientBuilder
3741
*/
@@ -41,42 +45,42 @@ public static LiveClientBuilder newClient(String hostName) {
4145

4246
/**
4347
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
48+
*
4449
* @param hostName profile name of Tiktok user could be found in profile link
4550
* @return true if live is Online, false if is offline
4651
*/
47-
public static boolean isLiveOnline(String hostName)
48-
{
52+
public static boolean isLiveOnline(String hostName) {
4953
return requests().fetchLiveUserData(hostName).isLiveOnline();
5054
}
5155

5256
/**
5357
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
58+
*
5459
* @param hostName profile name of Tiktok user could be found in profile link
5560
* @return true if live is Online, false if is offline
5661
*/
57-
public static CompletableFuture<Boolean> isLiveOnlineAsync(String hostName)
58-
{
59-
return CompletableFuture.supplyAsync(()-> isLiveOnline(hostName));
62+
public static CompletableFuture<Boolean> isLiveOnlineAsync(String hostName) {
63+
return CompletableFuture.supplyAsync(() -> isLiveOnline(hostName));
6064
}
6165

6266
/**
6367
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
68+
*
6469
* @param hostName profile name of Tiktok user could be found in profile link
6570
* @return true is hostName name is valid and exists, false if not
6671
*/
67-
public static boolean isHostNameValid(String hostName)
68-
{
72+
public static boolean isHostNameValid(String hostName) {
6973
return requests().fetchLiveUserData(hostName).isHostNameValid();
7074
}
7175

7276
/**
7377
* Example: https://www.tiktok.com/@dostawcavideo - hostName would be 'dostawcavideo'
78+
*
7479
* @param hostName profile name of Tiktok user could be found in profile link
7580
* @return true is hostName name is valid and exists, false if not
7681
*/
77-
public static CompletableFuture<Boolean> isHostNameValidAsync(String hostName)
78-
{
79-
return CompletableFuture.supplyAsync(()-> isHostNameValid(hostName));
82+
public static CompletableFuture<Boolean> isHostNameValidAsync(String hostName) {
83+
return CompletableFuture.supplyAsync(() -> isHostNameValid(hostName));
8084
}
8185

8286
/**
@@ -87,4 +91,29 @@ public static CompletableFuture<Boolean> isHostNameValidAsync(String hostName)
8791
public static LiveHttpClient requests() {
8892
return new TikTokLiveHttpClient();
8993
}
94+
95+
96+
//I don't like it, but it is reasonable for now
97+
private static GiftsManager giftsManager;
98+
99+
/**
100+
* Fetch gifts from endpoint and returns GiftManager
101+
*
102+
* @return GiftsManager
103+
*/
104+
public static GiftsManager gifts() {
105+
if (giftsManager != null) {
106+
return giftsManager;
107+
}
108+
synchronized (GiftsManager.class)
109+
{
110+
if (giftsManager == null)
111+
{
112+
return new TikTokGiftsManager(requests().fetchGiftsData().getGifts());
113+
}
114+
}
115+
return giftsManager;
116+
}
117+
118+
90119
}

0 commit comments

Comments
 (0)