Skip to content

Commit

Permalink
框架正式改名为 Toaster
Browse files Browse the repository at this point in the history
新增英文版本的文档及 Demo
  • Loading branch information
getActivity committed Feb 19, 2023
1 parent 2dc13f9 commit de577a2
Show file tree
Hide file tree
Showing 85 changed files with 1,106 additions and 210 deletions.
48 changes: 48 additions & 0 deletions .github/ISSUE_TEMPLATE/issue_en_template_bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
name: Submit Bug
about: Please tell me the problem with the framework and I will help you fix it!
labels: bug
assignees: getActivity

---


## [Warning: Please be sure to fill in according to the issue template. Don't take any chances. Once you find that the issue is not filled in carefully according to the template, you will close it directly.]

#### Description of the problem

* Framework Version [Required]: Please enter the framework version you are using

* Problem Description [Required]: Please enter your description of the problem

* Reproduction step [Required]: Please enter the steps to reproduce the problem (Note: Bug without reproduction step is not accepted at present)

* Whether the problem can be reproduced [Required]: Yes/No

* Phone information in question [Required]: Please fill in the phone brand and model in question

* Android version in question [Required]: Please fill in the Android version in question

* Source of problem information [Required]: Please fill in the source of the problem (for example: encountered by yourself / see on firebase crashlytics / user feedback, etc.)

#### Please respond

* Is it a specific phone or all phones will appear [Must answer]: Some/All (for example: google phones, certain Android versions will appear)

* Does the latest version of the framework have this problem [Must answer]: Yes/No (if you are using an older version, it is recommended to upgrade to see if the problem still exists)

* Have you consulted the framework documentation but have not resolved [Must answer]: Yes/No (the documentation will provide answers to the most common questions, and you can see if there is anything you want)

* Has anyone ever asked a similar question [Must answer]: Yes/No (see if anyone has ever asked a similar question, and refer to how others have solved it first)

* Whether the question can be reproduced through Demo [Must answer]: Yes/No (it is used to check whether it is caused by a problem with the writing of your own project code.)

* Does this issue also occur using the system-provided toast API [Must answer]: Yes/No (it is used to check whether there is a problem with the code of the framework.)

#### Other

* Provide the error stack (if it is a crash, it needs to be provided, be careful not to take the obfuscated code stack)

* Provide screenshots or videos (if available, it is recommended to fill in)

* Provide solutions (if any, it is recommended to fill in)
19 changes: 19 additions & 0 deletions .github/ISSUE_TEMPLATE/issue_en_template_question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
name: Ask questions
about: Ask questions and I'll answer them
labels: question
assignees: getActivity

---

## [Warning: Please be sure to fill in according to the issue template. Don't take any chances. Once you find that the issue is not filled in carefully according to the template, you will close it directly.]

#### Description of the problem

* Problem Description [Required]: Please describe your problem (Note: If it is determined to be a framework bug, please do not mention it here, otherwise it will not be accepted)

#### Please respond

* Have you consulted the framework documentation but have not resolved [Must answer]: Yes/No (the documentation will provide answers to the most common questions, and you can see if there is anything you want)

* Has anyone ever asked a similar question [Must answer]: Yes/No (see if anyone has ever asked a similar question, and refer to how others have solved it first)
19 changes: 19 additions & 0 deletions .github/ISSUE_TEMPLATE/issue_en_template_suggest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
name: Submit a suggestion
about: Please tell me the shortcomings of the framework, so that I can do better!
labels: help wanted
assignees: getActivity

---

## [Warning: Please be sure to fill in according to the issue template. Don't take any chances. Once you find that the issue is not filled in carefully according to the template, you will close it directly.]

#### Suggest collection

* Issue Has anyone asked a similar question before? [Must answer]: (Once there are repeated questions, I will not answer them again)

* Does the framework documentation mention this? [Must answer]: Yes/No (please read the documentation of the framework before making suggestions)

* What do you think the framework is lacking in? [Must answer]: (You can describe what makes you dissatisfied with the framework)

* What do you think would be better to improve? [Optional]: (You can provide your own ideas or practices for the author's reference)
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ assignees: getActivity

#### 问题描述

* 框架版本【必填】:XXX
* 框架版本【必填】:请输入你使用的框架版本

* 问题描述【必填】:XXX
* 问题描述【必填】:请输入你对这个问题的描述

* 复现步骤【必填】:XXX(注意:目前不受理没有复现步骤的 Bug 单)
* 复现步骤【必填】:请输入问题的复现步骤(注意:目前不受理没有复现步骤的 Bug 单)

* 是否必现【必填】:填是/否
* 是否必现【必填】:/否

* 出现问题的手机信息【必填】:请填写出现问题的品牌和机型

Expand All @@ -30,7 +30,7 @@ assignees: getActivity

* 框架最新的版本是否存在这个问题【必答】:是/否(如果用的是旧版本的话,建议升级看问题是否还存在)

* 是否已经查阅框架文档还未能解决的【必答】:是/否(文档会提供最常见的问题解答,可以看看是否有自己想要的)
* 是否已经查阅框架文档但还未能解决的【必答】:是/否(文档会提供最常见的问题解答,可以看看是否有自己想要的)

* issue 是否有人曾提过类似的问题【必答】:是/否(看看曾经有人提过类似的问题,先参考一下别人是怎么解决的)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ assignees: getActivity

#### 问题描述

* 请描述一下你的疑惑【必填】:XXX(注意:如果是框架 bug 请不要在这里提,否则一概不受理)
* 问题描述【必填】:请描述一下你的问题(注意:如果确定是框架 bug 请不要在这里提,否则一概不受理)

#### 请回答

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ assignees: getActivity

#### 建议收集

* issue 是否有人曾提过类似的问题?【必答】(一旦出现重复提问我将不会再次解答)
* issue 是否有人曾提过类似的问题?【必答】:(一旦出现重复提问我将不会再次解答)

* 框架文档是否有提及到此问题?【必答】:是/否(请先看完框架的文档后再来提建议)

* 你觉得框架有什么不足之处?【必答】(你可以描述框架有什么令你不满意的地方)
* 你觉得框架有什么不足之处?【必答】:(你可以描述框架有什么令你不满意的地方)

* 你觉得该怎么去完善会比较好?【非必答】(你可以提供一下自己的想法或者做法供作者参考)
* 你觉得该怎么去完善会比较好?【非必答】:(你可以提供一下自己的想法或者做法供作者参考)
179 changes: 179 additions & 0 deletions HelpDoc-en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#### Catalog

* [How to customize toast display animation](#how-to-customize-toast-display-animation)

* [How to customize toast display duration](#how-to-customize-toast-display-duration)

* [How to customize toast layout style](#how-to-customize-toast-layout-style)

* [How to switch to toast queue display strategy](#how-to-switch-to-toast-queue-display-strategy)

* [What should I do if the framework cannot meet the scene I am currently using](#what-should-i-do-if-the-framework-cannot-meet-the-scene-i-am-currently-using)

* [Why the framework prefers to use window manager to implement toast](#why-the-framework-prefers-to-use-window-manager-to-implement-toast)

#### How to customize toast display animation

* When toast is initialized, just modify the toast strategy

```java
Toaster.init(this, new ToastStrategy() {

@Override
public IToast createToast(IToastStyle<?> style) {
if (toast instanceof CustomToast) {
CustomToast customToast = ((CustomToast) toast);
// Set the toast animation effect
customToast.setAnimationsId(R.anim.xxx);
}
return toast;
}
});
```

* The disadvantage of this method is that it will only take effect when the application is in the foreground. This is because the toast in the foreground is implemented with a framework, which is essentially a WindowManager. The advantage is that it is very flexible and is not limited by the system toast mechanism. The disadvantage is that it cannot It is displayed in the background; while the toast in the background is implemented by the system, the advantage is that it can be displayed in the background, the disadvantage is that it is very limited and cannot be customized too deeply; and the framework uses two The advantages and disadvantages of the two methods are complementary.

#### How to customize toast display duration

* When toast is initialized, just modify the toast strategy

```java
Toaster.init(this, new ToastStrategy() {

@Override
public IToast createToast(IToastStyle<?> style) {
IToast toast = super.createToast(style);
if (toast instanceof CustomToast) {
CustomToast customToast = ((CustomToast) toast);
// Set the display duration of the short toast (default is 2000 milliseconds)
customToast.setShortDuration(1000);
// Set the display duration of the long Toast (default is 3500 milliseconds)
customToast.setLongDuration(5000);
}
return toast;
}
});
```

* The disadvantage of this method is that it will only take effect when the application is in the foreground. This is because the toast in the foreground is implemented with a framework, which is essentially a WindowManager. The advantage is that it is very flexible and is not limited by the system toast mechanism. The disadvantage is that it cannot It is displayed in the background; while the toast in the background is implemented by the system, the advantage is that it can be displayed in the background, the disadvantage is that it is very limited and cannot be customized too deeply; and the framework uses two The advantages and disadvantages of the two methods are complementary.

#### How to customize toast layout style

* If you want to set the global toast style, you can call it like this (choose any one)

```java
// Modify toast layout
Toaster.setView(int id);
```

```java
// Modified toast layout, toast shows center of gravity, toast shows position offset
Toaster.setStyle(IToastStyle<?> style);
```

* If you want to set a separate Toast display style for one occasion, you can do all of these (select either)

```java
// Modify toast layout
ToastParams params = new ToastParams();
params.text = "I am toast of custom layout (partial effect)";
params.style = new CustomViewToastStyle(R.layout.toast_custom_view);
Toaster.show(params);
```

```java
// Modify the toast layout, toast display center of gravity, and toast display position offset
ToastParams params = new ToastParams();
params.text = "I am toast of custom layout (partial effect)";
params.style = new CustomViewToastStyle(R.layout.toast_custom_view, Gravity.CENTER, 10, 20);
Toaster.show(params);
```

* At this point, you may have a doubt, why setting a new toast style can only pass in the layout id instead of the View object? Because every time the framework displays toast, it will create a new toast object and View object. If the View object is passed in, it will not be able to create it every time it is displayed. As for why the framework does not reuse this View object, it is because if After reusing this View object, the following exceptions may be triggered:

```text
java.lang.IllegalStateException: View android.widget.TextView{7ffea98 V.ED..... ......ID 0,0-396,153 #102000b android:id/message}
has already been added to the window manager.
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:371)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:131)
at android.widget.Toast$TN.handleShow(Toast.java:501)
at android.widget.Toast$TN$1.handleMessage(Toast.java:403)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
```

* This is because WindowManager succeeded when addingView, but failed when removingView, which caused the View object of the previous toast to be unable to be reused when the next toast is displayed. Although this situation is relatively rare, there are still people who have reported this to me. Problem, in order to solve this problem, I decided not to reuse the View object. For specific adjustments to this piece, you can check the release record: [Toaster/releases/tag/9.0](https://github.com/getActivity/Toaster/releases/tag/9.0)

#### How to switch to toast queue display strategy

* You only need to modify the initialization method of the toast framework and manually pass in the toast strategy class. Here, you can use the ToastStrategy class that has been encapsulated by the framework.

```java
// Initialize the toast framework
// Toaster.init(this);
Toaster.init(this, new ToastStrategy(ToastStrategy.SHOW_STRATEGY_TYPE_QUEUE));
```

* Note that the constructor needs to pass in `ToastStrategy.SHOW_STRATEGY_TYPE_QUEUE`. For an introduction to this field, see the code comments below

```java
public class ToastStrategy {

/**
* Instant display mode (default)
*
* In the case of multiple toast display requests, before displaying the next toast
* The previous toast will be canceled immediately to ensure that the currently displayed toast message is up to date
*/
public static final int SHOW_STRATEGY_TYPE_IMMEDIATELY = 0;

/**
* No message loss mode
*
* In the case of multiple toast display requests, wait for the previous toast to be displayed for 1 second or 1.5 seconds
* Then display the next toast, not according to the display duration of the toast, because the waiting time will be very long
* This can not only ensure that the user can see every toast message, but also ensure that the user will not wait too long
*/
public static final int SHOW_STRATEGY_TYPE_QUEUE = 1;
}
```

#### What should I do if the framework cannot meet the scene I am currently using

* The Toaster framework is intended to solve some toast requirements. If Toaster cannot meet your needs, you can consider using the [XToast](https://github.com/getActivity/XToast) floating window framework to achieve it.

#### Why the framework prefers to use window manager to implement toast

* There are too many pits in the system toast, the main problems are as follows:

* System toast will cause some memory leaks

* System toast cannot realize custom display animation and display duration control

* Android 7.1 version will block the main thread and cause BadTokenException

* Closing the permission of the notification bar below Android 10.0 will cause the problem that the system toast cannot be displayed

* Android 11 and above, cannot customize the toast style (layout, position center of gravity, position offset)

* Therefore, the framework prefers to use WindowManager instead of implementing toast display. The specific advantages and disadvantages are as follows:

* advantage

* There will be no memory leaks, and there will not be so many strange problems

* High degree of customization, support custom animation and custom display duration

* Break through Google's restrictions on toast in the new version of Android

* shortcoming

* WindowManager cannot pop up <br> in the background without floating window permission (frame solution: if it is displayed in the background, use the system's toast to display)

* The WindowManager will be bound to the Activity and will disappear with the Activity being destroyed <br> (framework solution: the display is delayed by 200ms, thus waiting for the latest Activity to be created before calling the display, so WindowManager is bound to the latest Activity and does not have the problem of disappearing with the old Activity when it finishes)

* Of course, it is not to say that using the system toast is not good. It must be good to use WindowManger. It depends on the specific usage scenario. I think the best way is: use WindowManager to display the application in the foreground, and use the system in the background. the best solution is to use WindowManager in the foreground state and system Toast in the background state.
Loading

0 comments on commit de577a2

Please sign in to comment.