Rename implementation and improve README
This commit is contained in:
parent
0fd22c61f4
commit
f38fe387a6
51
README.md
51
README.md
@ -89,29 +89,56 @@ dependencies {
|
||||
## Usage
|
||||
Simple initialization of a native TDLib client
|
||||
```java
|
||||
package it.tdlight.example;
|
||||
|
||||
|
||||
import it.tdlight.tdlight.Client;
|
||||
import it.tdlight.common.TelegramClient;
|
||||
import it.tdlight.tdlight.ClientManager;
|
||||
import it.tdlight.common.Init;
|
||||
import it.tdlight.common.Log;
|
||||
import it.tdlight.common.TDLibException;
|
||||
|
||||
import it.tdlight.jni.TdApi;
|
||||
|
||||
public class Example {
|
||||
public static void main(String[] args) {
|
||||
// Initialize TDLight native libraries
|
||||
Init.start();
|
||||
|
||||
// Set TDLib log level to 1
|
||||
Log.setVerbosityLevel(1);
|
||||
// Create a client
|
||||
TelegramClient client = ClientManager.create(Example::onUpdate, Example::onUpdateError, Example::onError);
|
||||
|
||||
// Uncomment this line to print TDLib logs to a file
|
||||
// Log.setFilePath("logs" + File.separatorChar + "tdlib.log");
|
||||
|
||||
Client client = new Client();
|
||||
|
||||
// Initialize the TDLib client
|
||||
client.initializeClient();
|
||||
// Here you can use the client.
|
||||
|
||||
// Now you can use the client
|
||||
// Documentation of tdlib methods can be found here:
|
||||
// https://tdlight-team.github.io/tdlight-docs
|
||||
|
||||
// A similar example on how to use tdlib can be found here:
|
||||
// https://github.com/tdlib/td/blob/master/example/java/org/drinkless/tdlib/example/Example.java
|
||||
}
|
||||
|
||||
private static void onUpdate(TdApi.Object object) {
|
||||
TdApi.Update update = (TdApi.Update) object;
|
||||
System.out.println("Received update: " + update);
|
||||
}
|
||||
|
||||
private static void onUpdateError(Throwable exception) {
|
||||
if (exception instanceof TDLibException) {
|
||||
String errorMessage = ((TDLibException) exception).getErrorMessage();
|
||||
System.out.println("Received an error from updates: " + errorMessage);
|
||||
} else {
|
||||
System.out.println("Received an error from updates:");
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void onError(Throwable exception) {
|
||||
if (exception instanceof TDLibException) {
|
||||
String errorMessage = ((TDLibException) exception).getErrorMessage();
|
||||
System.out.println("Received an error: " + errorMessage);
|
||||
} else {
|
||||
System.out.println("Received an error:");
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -1,38 +0,0 @@
|
||||
package it.tdlight.common;
|
||||
|
||||
public abstract class CommonClient {
|
||||
|
||||
protected abstract String getImplementationName();
|
||||
|
||||
|
||||
private InternalClientManager getClientManager() {
|
||||
// ClientManager is singleton:
|
||||
return InternalClientManager.get(getImplementationName());
|
||||
}
|
||||
|
||||
public TelegramClient create(ResultHandler updateHandler,
|
||||
ExceptionHandler updateExceptionHandler,
|
||||
ExceptionHandler defaultExceptionHandler) {
|
||||
InternalClient client = new InternalClient(getClientManager(),
|
||||
updateHandler,
|
||||
updateExceptionHandler,
|
||||
defaultExceptionHandler
|
||||
);
|
||||
return create(client);
|
||||
}
|
||||
|
||||
public TelegramClient create(UpdatesHandler updatesHandler,
|
||||
ExceptionHandler updateExceptionHandler,
|
||||
ExceptionHandler defaultExceptionHandler) {
|
||||
InternalClient client = new InternalClient(getClientManager(),
|
||||
updatesHandler,
|
||||
updateExceptionHandler,
|
||||
defaultExceptionHandler
|
||||
);
|
||||
return create(client);
|
||||
}
|
||||
|
||||
private TelegramClient create(InternalClient internalClient) {
|
||||
return internalClient;
|
||||
}
|
||||
}
|
37
src/main/java/it/tdlight/common/CommonClientManager.java
Normal file
37
src/main/java/it/tdlight/common/CommonClientManager.java
Normal file
@ -0,0 +1,37 @@
|
||||
package it.tdlight.common;
|
||||
|
||||
public abstract class CommonClientManager {
|
||||
|
||||
private static InternalClientManager getClientManager(String implementationName) {
|
||||
// ClientManager is singleton:
|
||||
return InternalClientManager.get(implementationName);
|
||||
}
|
||||
|
||||
protected static TelegramClient create(String implementationName,
|
||||
ResultHandler updateHandler,
|
||||
ExceptionHandler updateExceptionHandler,
|
||||
ExceptionHandler defaultExceptionHandler) {
|
||||
InternalClient client = new InternalClient(getClientManager(implementationName),
|
||||
updateHandler,
|
||||
updateExceptionHandler,
|
||||
defaultExceptionHandler
|
||||
);
|
||||
return create(client);
|
||||
}
|
||||
|
||||
protected static TelegramClient create(String implementationName,
|
||||
UpdatesHandler updatesHandler,
|
||||
ExceptionHandler updateExceptionHandler,
|
||||
ExceptionHandler defaultExceptionHandler) {
|
||||
InternalClient client = new InternalClient(getClientManager(implementationName),
|
||||
updatesHandler,
|
||||
updateExceptionHandler,
|
||||
defaultExceptionHandler
|
||||
);
|
||||
return create(client);
|
||||
}
|
||||
|
||||
private static TelegramClient create(InternalClient internalClient) {
|
||||
return internalClient;
|
||||
}
|
||||
}
|
@ -16,6 +16,12 @@ public class InternalClientManager implements AutoCloseable {
|
||||
private final AtomicLong currentQueryId = new AtomicLong();
|
||||
|
||||
private InternalClientManager(String implementationName) {
|
||||
try {
|
||||
Init.start();
|
||||
} catch (Throwable ex) {
|
||||
ex.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
this.implementationName = implementationName;
|
||||
}
|
||||
|
||||
|
@ -1,14 +0,0 @@
|
||||
package it.tdlight.tdlib;
|
||||
|
||||
import it.tdlight.common.CommonClient;
|
||||
|
||||
/**
|
||||
* Interface for interaction with TDLib.
|
||||
*/
|
||||
public class Client extends CommonClient {
|
||||
|
||||
@Override
|
||||
protected String getImplementationName() {
|
||||
return "tdlib";
|
||||
}
|
||||
}
|
29
src/main/java/it/tdlight/tdlib/ClientManager.java
Normal file
29
src/main/java/it/tdlight/tdlib/ClientManager.java
Normal file
@ -0,0 +1,29 @@
|
||||
package it.tdlight.tdlib;
|
||||
|
||||
import it.tdlight.common.CommonClientManager;
|
||||
import it.tdlight.common.ExceptionHandler;
|
||||
import it.tdlight.common.ResultHandler;
|
||||
import it.tdlight.common.TelegramClient;
|
||||
import it.tdlight.common.UpdatesHandler;
|
||||
|
||||
/**
|
||||
* Interface for interaction with TDLib.
|
||||
*/
|
||||
public class ClientManager extends CommonClientManager {
|
||||
|
||||
private static final String implementationName = "tdlib";
|
||||
|
||||
public static TelegramClient create(
|
||||
ResultHandler updateHandler,
|
||||
ExceptionHandler updateExceptionHandler,
|
||||
ExceptionHandler defaultExceptionHandler) {
|
||||
return CommonClientManager.create(implementationName, updateHandler, updateExceptionHandler, defaultExceptionHandler);
|
||||
}
|
||||
|
||||
public static TelegramClient create(
|
||||
UpdatesHandler updatesHandler,
|
||||
ExceptionHandler updateExceptionHandler,
|
||||
ExceptionHandler defaultExceptionHandler) {
|
||||
return CommonClientManager.create(implementationName, updatesHandler, updateExceptionHandler, defaultExceptionHandler);
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package it.tdlight.tdlight;
|
||||
|
||||
import it.tdlight.common.CommonClient;
|
||||
|
||||
/**
|
||||
* Interface for interaction with TDLight.
|
||||
*/
|
||||
public class Client extends CommonClient {
|
||||
|
||||
@Override
|
||||
protected String getImplementationName() {
|
||||
return "tdlight";
|
||||
}
|
||||
}
|
29
src/main/java/it/tdlight/tdlight/ClientManager.java
Normal file
29
src/main/java/it/tdlight/tdlight/ClientManager.java
Normal file
@ -0,0 +1,29 @@
|
||||
package it.tdlight.tdlight;
|
||||
|
||||
import it.tdlight.common.CommonClientManager;
|
||||
import it.tdlight.common.ExceptionHandler;
|
||||
import it.tdlight.common.ResultHandler;
|
||||
import it.tdlight.common.TelegramClient;
|
||||
import it.tdlight.common.UpdatesHandler;
|
||||
|
||||
/**
|
||||
* Interface for interaction with TDLight.
|
||||
*/
|
||||
public class ClientManager extends CommonClientManager {
|
||||
|
||||
private static final String implementationName = "tdlight";
|
||||
|
||||
public static TelegramClient create(
|
||||
ResultHandler updateHandler,
|
||||
ExceptionHandler updateExceptionHandler,
|
||||
ExceptionHandler defaultExceptionHandler) {
|
||||
return CommonClientManager.create(implementationName, updateHandler, updateExceptionHandler, defaultExceptionHandler);
|
||||
}
|
||||
|
||||
public static TelegramClient create(
|
||||
UpdatesHandler updatesHandler,
|
||||
ExceptionHandler updateExceptionHandler,
|
||||
ExceptionHandler defaultExceptionHandler) {
|
||||
return CommonClientManager.create(implementationName, updatesHandler, updateExceptionHandler, defaultExceptionHandler);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user