瀏覽代碼

only require restart in case if macFUSE or FUSE-T

Closes #2786
Armin Schrenk 2 年之前
父節點
當前提交
fdc0d2d6b5

+ 12 - 0
src/main/java/org/cryptomator/common/ObservableUtil.java

@@ -2,7 +2,9 @@ package org.cryptomator.common;
 
 import javafx.beans.binding.Bindings;
 import javafx.beans.value.ObservableValue;
+import java.util.Objects;
 import java.util.function.Function;
+import java.util.function.Supplier;
 
 public class ObservableUtil {
 
@@ -15,4 +17,14 @@ public class ObservableUtil {
 			}
 		}, observable);
 	}
+
+	public static <T, U> ObservableValue<U> mapWithDefault(ObservableValue<T> observable, Function<? super T, ? extends U> mapper, Supplier<U> defaultValue) {
+		return Bindings.createObjectBinding(() -> {
+			if (observable.getValue() == null) {
+				return defaultValue.get();
+			} else {
+				return mapper.apply(observable.getValue());
+			}
+		}, observable);
+	}
 }

+ 28 - 18
src/main/java/org/cryptomator/common/mount/MountModule.java

@@ -2,50 +2,60 @@ package org.cryptomator.common.mount;
 
 import dagger.Module;
 import dagger.Provides;
+import org.cryptomator.common.ObservableUtil;
 import org.cryptomator.common.settings.Settings;
 import org.cryptomator.integrations.mount.MountService;
 
 import javax.inject.Singleton;
-import javafx.beans.property.SimpleObjectProperty;
 import javafx.beans.value.ObservableValue;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
 
 @Module
 public class MountModule {
 
+	private static final AtomicReference<MountService> formerSelectedMountService = new AtomicReference<>(null);
+
 	@Provides
 	@Singleton
 	static List<MountService> provideSupportedMountServices() {
 		return MountService.get().toList();
 	}
 
-	//currently not used, because macFUSE and FUSE-T cannot be used in the same JVM
-	/*
 	@Provides
 	@Singleton
 	static ObservableValue<ActualMountService> provideMountService(Settings settings, List<MountService> serviceImpls) {
 		var fallbackProvider = serviceImpls.stream().findFirst().orElse(null);
-		return ObservableUtil.mapWithDefault(settings.mountService(), //
+
+		var observableMountService = ObservableUtil.mapWithDefault(settings.mountService(), //
 				desiredServiceImpl -> { //
-					var desiredService = serviceImpls.stream().filter(serviceImpl -> serviceImpl.getClass().getName().equals(desiredServiceImpl)).findAny(); //
-					return new ActualMountService(desiredService.orElse(fallbackProvider), desiredService.isPresent()); //
+					var serviceFromSettings = serviceImpls.stream().filter(serviceImpl -> serviceImpl.getClass().getName().equals(desiredServiceImpl)).findAny(); //
+					var targetedService = serviceFromSettings.orElse(fallbackProvider);
+					return applyWorkaroundForFuseTMacFuse(targetedService, serviceFromSettings.isPresent());
 				}, //
-				new ActualMountService(fallbackProvider, true));
+				() -> { //
+					return applyWorkaroundForFuseTMacFuse(fallbackProvider, true);
+				});
+		return observableMountService;
 	}
-	 */
 
-	@Provides
-	@Singleton
-	static ActualMountService provideActualMountService(Settings settings, List<MountService> serviceImpls) {
-		var fallbackProvider = serviceImpls.stream().findFirst().orElse(null);
-		var desiredService = serviceImpls.stream().filter(serviceImpl -> serviceImpl.getClass().getName().equals(settings.mountService().getValue())).findFirst(); //
-		return new ActualMountService(desiredService.orElse(fallbackProvider), desiredService.isPresent()); //
+
+	//see https://github.com/cryptomator/cryptomator/issues/2786
+	private static ActualMountService applyWorkaroundForFuseTMacFuse(MountService targetedService, boolean isDesired) {
+		if (isFUSETOrMacFUSE(targetedService) && isFUSETOrMacFUSE(formerSelectedMountService.get()) && !targetedService.equals(formerSelectedMountService.get())) {
+			return new ActualMountService(formerSelectedMountService.get(), false); //
+		} else {
+			formerSelectedMountService.set(targetedService);
+			return new ActualMountService(targetedService, isDesired); //
+		}
 	}
 
-	@Provides
-	@Singleton
-	static ObservableValue<ActualMountService> provideMountService(ActualMountService service) {
-		return new SimpleObjectProperty<>(service);
+	private static boolean isFUSETOrMacFUSE(MountService service) {
+		if (service == null) {
+			return false;
+		} else {
+			return List.of("org.cryptomator.frontend.fuse.mount.MacFuseMountProvider", "org.cryptomator.frontend.fuse.mount.FuseTMountProvider").contains(service.getClass().getName());
+		}
 	}
 
 }