Skip to content

Commit

Permalink
处理dialog对象的维护管理类
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Mar 27, 2018
1 parent 6ffcf0e commit 34257a3
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 173 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ dependencies {
compile project(path: ':dialog')
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
compile 'com.github.tianzhijiexian:logger:1.1.9'
compile 'com.jakewharton.timber:timber:4.5.1'
compile 'com.github.hss01248:Mylog:1.0.1'
compile 'com.alibaba:fastjson:1.1.54.android'
}
23 changes: 11 additions & 12 deletions app/src/main/java/com/hss01248/dialogutildemo/BaseApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.hss01248.dialog.ActivityStackManager;
import com.hss01248.dialog.DialogsMaintainer;
import com.hss01248.dialog.StyledDialog;
import com.orhanobut.logger.LogPrintStyle;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.Settings;
import com.orhanobut.logger.Jsonfy;
import com.orhanobut.logger.XLogUtil;
import com.squareup.leakcanary.LeakCanary;

/**
Expand All @@ -29,14 +29,12 @@ public void onCreate() {
}

private void initlog() {
Logger.initialize(
new Settings()
.setStyle(new LogPrintStyle())
.isShowMethodLink(true)
.isShowThreadInfo(false)
.setMethodOffset(0)
.setLogPriority(BuildConfig.DEBUG ? Log.VERBOSE : Log.ASSERT)
);
XLogUtil.init(true, "dialog", new Jsonfy() {
@Override
public String toJson(Object o) {
return JSON.toJSONString(o);
}
});
}

private void registCallback() {
Expand All @@ -57,6 +55,7 @@ public void onActivityResumed(Activity activity) {

@Override
public void onActivityPaused(Activity activity) {
DialogsMaintainer.onPause(activity);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void onClick(View view) {
testBadToken();
break;
case R.id.btn_dismiss:
StyledDialog.dismissLoading();
StyledDialog.dismissLoading(this);
break;
case R.id.btn_common_progress:
final Dialog[] dialog00 = new Dialog[1];
Expand Down
164 changes: 109 additions & 55 deletions dialog/src/main/java/com/hss01248/dialog/DialogsMaintainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,123 +3,177 @@
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

/**
* Created by hss on 2018/3/24.
*/

public class DialogsMaintainer {

private static HashMap<Activity,List<Dialog>> dialogsOfActivity = new HashMap<>();
private static List<Dialog> loadingDialogs = new ArrayList<>();
private static HashMap<Activity, Set<Dialog>> dialogsOfActivity = new HashMap<>();
private static HashMap<Activity, Set<Dialog>> loadingDialogs = new HashMap<>();


public static void addLoadingDialog(Context context, Dialog dialog) {

public static void addLoadingDialog(Dialog dialog){
if(dialog.getOwnerActivity() instanceof Activity){
loadingDialogs.add(dialog);
if (!(context instanceof Activity)) {
return;
}
Activity activity = (Activity) context;

Set<Dialog> dialogs = null;
if (loadingDialogs.containsKey(activity)) {
dialogs = loadingDialogs.get(activity);
}
if (dialogs == null) {
dialogs = new HashSet<>();
loadingDialogs.put(activity, dialogs);
}
dialogs.add(dialog);


}

public static void dismissLoading(Activity activity){
if(activity ==null){
public static void dismissLoading(Activity activity) {

if (activity == null) {
return;
}
ListIterator<Dialog> iterator = loadingDialogs.listIterator();
while (iterator.hasNext()){
Dialog dialog = iterator.next();
if(dialog.getOwnerActivity().equals(activity)){
dialog.dismiss();
iterator.remove();
removeWhenDismiss(dialog);
if (!loadingDialogs.containsKey(activity)) {
return;
}
Set<Dialog> dialogSet = loadingDialogs.get(activity);
for (Dialog dialog : dialogSet) {
dialog.dismiss();
//在callback内部自动会去移除在dialogsOfActivity的引用
}
loadingDialogs.remove(activity);

}

public static void dismissLoading(Dialog dialog) {
Iterator<Map.Entry<Activity, Set<Dialog>>> entryIterator = loadingDialogs.entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<Activity, Set<Dialog>> entry = entryIterator.next();
Set<Dialog> dialogInterfaces = entry.getValue();
if (dialogInterfaces == null || dialogInterfaces.isEmpty()) {
entryIterator.remove();
return;
}
Iterator<Dialog> iterator = dialogInterfaces.iterator();
while (iterator.hasNext()) {
Dialog dialog0 = iterator.next();
if (dialog.equals(dialog0)) {
iterator.remove();
}
}
if (dialogInterfaces == null || dialogInterfaces.isEmpty()) {
entryIterator.remove();
return;
}
}
}

public static void addWhenShow(Dialog dialog){
public static void addWhenShow(Context context, Dialog dialog) {

Activity activity = dialog.getOwnerActivity();
if(activity ==null){
if (!(context instanceof Activity)) {
return;
}
List<Dialog> dialogs = null;
if(dialogsOfActivity.containsKey(activity)){

Activity activity = (Activity) context;

Set<Dialog> dialogs = null;
if (dialogsOfActivity.containsKey(activity)) {
dialogs = dialogsOfActivity.get(activity);
}
if(dialogs == null){
dialogs = new ArrayList<>();
dialogsOfActivity.put(activity,dialogs);
if (dialogs == null) {
dialogs = new HashSet<>();
dialogsOfActivity.put(activity, dialogs);
}
dialogs.add(dialog);

}

public static void removeWhenDismiss(Dialog dialog){
Activity activity = dialog.getOwnerActivity();
if(activity ==null){
return;
}
if(!dialogsOfActivity.containsKey(activity)){
return;
}
List<Dialog> dialogInterfaces = dialogsOfActivity.get(activity);
if(dialogInterfaces==null || dialogInterfaces.isEmpty()){
return;
}
ListIterator<Dialog> iterator = dialogInterfaces.listIterator();
while (iterator.hasNext()){
Dialog dialog0 = iterator.next();
if(dialog.equals(dialog0)){
iterator.remove();
public static void removeWhenDismiss(Dialog dialog) {

try {
Iterator<Map.Entry<Activity, Set<Dialog>>> entryIterator = dialogsOfActivity.entrySet().iterator();
while (entryIterator.hasNext()) {
Map.Entry<Activity, Set<Dialog>> entry = entryIterator.next();
Set<Dialog> dialogInterfaces = entry.getValue();
if (dialogInterfaces == null || dialogInterfaces.isEmpty()) {
entryIterator.remove();
return;
}
Iterator<Dialog> iterator = dialogInterfaces.iterator();
while (iterator.hasNext()) {
Dialog dialog0 = iterator.next();
if (dialog.equals(dialog0)) {
iterator.remove();
}
}
if (dialogInterfaces == null || dialogInterfaces.isEmpty()) {
entryIterator.remove();
return;
}
}
} catch (Exception e) {
e.printStackTrace();
}


}

/**
* 在这里移除已经dismiss的dialog引用
*
* @param activity
*/
public static void onPause(Activity activity){
if(!dialogsOfActivity.containsKey(activity)){
public static void onPause(Activity activity) {
if (!dialogsOfActivity.containsKey(activity)) {
return;
}
List<Dialog> dialogInterfaces = dialogsOfActivity.get(activity);
if(dialogInterfaces==null || dialogInterfaces.isEmpty()){
Set<Dialog> dialogInterfaces = dialogsOfActivity.get(activity);
if (dialogInterfaces == null || dialogInterfaces.isEmpty()) {
dialogInterfaces.remove(activity);
return;
}
ListIterator<Dialog> iterator = dialogInterfaces.listIterator();
while (iterator.hasNext()){
Iterator<Dialog> iterator = dialogInterfaces.iterator();
while (iterator.hasNext()) {
Dialog dialog = iterator.next();
if(!dialog.isShowing()){
if (!dialog.isShowing()) {
iterator.remove();
}
}
if (dialogInterfaces == null || dialogInterfaces.isEmpty()) {
dialogInterfaces.remove(activity);
return;
}

}

public static void onDestory(Activity activity){
if(!dialogsOfActivity.containsKey(activity)){
public static void onDestory(Activity activity) {

if (!dialogsOfActivity.containsKey(activity)) {
return;
}
List<Dialog> dialogInterfaces = dialogsOfActivity.get(activity);
if(dialogInterfaces==null || dialogInterfaces.isEmpty()){
Set<Dialog> dialogInterfaces = dialogsOfActivity.get(activity);
if (dialogInterfaces == null || dialogInterfaces.isEmpty()) {
dialogInterfaces.remove(activity);
return;
}
for (Dialog dialog : dialogInterfaces) {
if(dialog!=null){
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
dialogInterfaces.remove(activity);

}
}
Loading

0 comments on commit 34257a3

Please sign in to comment.