mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
TikTokLive client sevices customization
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.live.builder;
|
||||
|
||||
import io.github.jwdeveloper.dependance.implementation.DependanceContainerBuilder;
|
||||
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
|
||||
import io.github.jwdeveloper.tiktok.listener.TikTokEventListener;
|
||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||
@@ -60,15 +61,26 @@ public interface LiveClientBuilder extends EventsBuilder<LiveClientBuilder> {
|
||||
*/
|
||||
LiveClientBuilder addListener(TikTokEventListener listener);
|
||||
|
||||
|
||||
/**
|
||||
* Allows you to use own implementation of internal TikTokLive dependencies,
|
||||
* when the default implementation does not meet your needs
|
||||
*
|
||||
*
|
||||
* @param onCustomizeDependencies access to dependency container
|
||||
* @return
|
||||
*/
|
||||
LiveClientBuilder customize(Consumer<DependanceContainerBuilder> onCustomizeDependencies);
|
||||
|
||||
/**
|
||||
* Builds new instance of the LiveClient
|
||||
* @return LiveClient object
|
||||
*/
|
||||
LiveClient build();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return LiveClient object and connects to TikTok live
|
||||
* Builds new instance of the LiveClient and connects to live
|
||||
* @return LiveClient object
|
||||
*/
|
||||
LiveClient buildAndConnect();
|
||||
|
||||
|
||||
@@ -11,12 +11,7 @@
|
||||
|
||||
<artifactId>Client</artifactId>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -30,12 +25,7 @@
|
||||
<version>${project.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.jwdeveloper.DepenDance</groupId>
|
||||
<artifactId>DepenDance-Full</artifactId>
|
||||
<version>0.0.18-Release</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.protobuf</groupId>
|
||||
<artifactId>protobuf-java</artifactId>
|
||||
|
||||
@@ -24,6 +24,7 @@ package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import io.github.jwdeveloper.dependance.Dependance;
|
||||
import io.github.jwdeveloper.dependance.api.DependanceContainer;
|
||||
import io.github.jwdeveloper.dependance.implementation.DependanceContainerBuilder;
|
||||
import io.github.jwdeveloper.tiktok.mappers.MessagesMapperFactory;
|
||||
import io.github.jwdeveloper.tiktok.common.LoggerFactory;
|
||||
import io.github.jwdeveloper.tiktok.data.events.*;
|
||||
@@ -63,6 +64,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
||||
protected final TikTokLiveEventHandler eventHandler;
|
||||
protected final List<TikTokEventListener> listeners;
|
||||
protected final List<Consumer<TikTokMapper>> onCustomMappings;
|
||||
protected final List<Consumer<DependanceContainerBuilder>> onCustomDependencies;
|
||||
|
||||
public TikTokLiveClientBuilder(String userName) {
|
||||
this.clientSettings = LiveClientSettings.createDefault();
|
||||
@@ -70,6 +72,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
||||
this.eventHandler = new TikTokLiveEventHandler();
|
||||
this.listeners = new ArrayList<>();
|
||||
this.onCustomMappings = new ArrayList<>();
|
||||
this.onCustomDependencies = new ArrayList<>();
|
||||
}
|
||||
|
||||
public LiveClientBuilder onMapping(Consumer<TikTokMapper> consumer) {
|
||||
@@ -88,6 +91,12 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveClientBuilder customize(Consumer<DependanceContainerBuilder> onCustomizeDependencies) {
|
||||
this.onCustomDependencies.add(onCustomizeDependencies);
|
||||
return this;
|
||||
}
|
||||
|
||||
protected void validate() {
|
||||
if (clientSettings.getClientLanguage() == null || clientSettings.getClientLanguage().isEmpty())
|
||||
clientSettings.setClientLanguage("en");
|
||||
@@ -175,6 +184,8 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
||||
//client
|
||||
dependance.registerSingleton(LiveClient.class, TikTokLiveClient.class);
|
||||
|
||||
onCustomDependencies.forEach(action -> action.accept(dependance));
|
||||
|
||||
var container = dependance.build();
|
||||
return container.find(LiveClient.class);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.gift.TikTokGiftEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.models.gifts.Gift;
|
||||
import io.github.jwdeveloper.tiktok.live.GiftsManager;
|
||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* When the default implementation does not meet your needs,
|
||||
* you can override it using `customize` method
|
||||
*/
|
||||
public class CusomizationExample {
|
||||
public static void main(String[] args) {
|
||||
|
||||
var client = TikTokLive.newClient("john")
|
||||
.configure(liveClientSettings ->
|
||||
{
|
||||
liveClientSettings.setFetchGifts(false);
|
||||
liveClientSettings.setOffline(true);
|
||||
})
|
||||
.onError((liveClient, event) ->
|
||||
{
|
||||
event.getException().printStackTrace();
|
||||
})
|
||||
.customize(container ->
|
||||
{
|
||||
//overriding default implementation of GiftsManager, with own one
|
||||
container.registerSingleton(TikTokLiveEventHandler.class, CustomEventsHandler.class);
|
||||
}).build();
|
||||
|
||||
client.connect();
|
||||
client.publishEvent(TikTokGiftEvent.of("rose", 1, 12));
|
||||
client.publishEvent(TikTokGiftEvent.of("stone", 2, 12));
|
||||
}
|
||||
|
||||
public static class CustomEventsHandler extends TikTokLiveEventHandler {
|
||||
|
||||
@Override
|
||||
public void publish(LiveClient tikTokLiveClient, TikTokEvent tikTokEvent) {
|
||||
System.out.println("Hello from custom events handler: " + tikTokEvent.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,13 @@ import io.github.jwdeveloper.descrabble.api.elements.Element;
|
||||
import io.github.jwdeveloper.descrabble.api.elements.ElementFactory;
|
||||
|
||||
public class EventsDecorator implements DescriptionDecorator {
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void decorate(Element root, ElementFactory factory) {
|
||||
public void decorate(Element root, ElementFactory factory)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
13
pom.xml
13
pom.xml
@@ -23,7 +23,12 @@
|
||||
<maven.compiler.target>16</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -74,6 +79,12 @@
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.jwdeveloper.DepenDance</groupId>
|
||||
<artifactId>DepenDance-Full</artifactId>
|
||||
<version>0.0.18-Release</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
||||
Reference in New Issue
Block a user