TradableList works

This commit is contained in:
Mike Rosseel 2017-05-08 21:46:17 +02:00
parent d99cab1d3c
commit 423e5c8e05
8 changed files with 125 additions and 10 deletions

View File

@ -1109,7 +1109,7 @@ message User {
}
message ViewPath {
// TODO BALLOONS INTO 40 classes !!!!!!!!!!!!!!!!
repeated string view_path = 1;
}
message VoteItem {

View File

@ -5,10 +5,14 @@ import io.bisq.common.locale.*;
import io.bisq.common.persistence.ListPersistable;
import io.bisq.common.persistence.Persistable;
import io.bisq.common.proto.PersistenceProtoResolver;
import io.bisq.common.storage.Storage;
import io.bisq.core.btc.AddressEntry;
import io.bisq.core.btc.AddressEntryList;
import io.bisq.core.btc.wallet.BtcWalletService;
import io.bisq.core.dao.compensation.CompensationRequestPayload;
import io.bisq.core.offer.OpenOffer;
import io.bisq.core.payment.PaymentAccount;
import io.bisq.core.trade.*;
import io.bisq.core.trade.statistics.TradeStatistics;
import io.bisq.core.user.BlockChainExplorer;
import io.bisq.core.user.Preferences;
@ -44,12 +48,31 @@ public class CoreDiskProtoResolver implements PersistenceProtoResolver {
private Provider<AddressEntryList> addressEntryListProvider;
private Provider<Preferences> preferencesProvider;
private Storage<TradableList<OpenOffer>> openOfferStorage;
private Storage<TradableList<BuyerAsMakerTrade>> buyerAsMakerTradeStorage;
private Storage<TradableList<BuyerAsTakerTrade>> buyerAsTakerTradeStorage;
private Storage<TradableList<SellerAsMakerTrade>> sellerAsMakerTradeStorage;
private Storage<TradableList<SellerAsTakerTrade>> sellerAsTakerTradeStorage;
private Provider<BtcWalletService> btcWalletService;
@Inject
public CoreDiskProtoResolver(Provider<Preferences> preferencesProvider,
Provider<AddressEntryList> addressEntryListProvider
Provider<AddressEntryList> addressEntryListProvider,
Storage<TradableList<OpenOffer>> openOfferStorage,
Storage<TradableList<BuyerAsMakerTrade>> buyerAsMakerTradeStorage,
Storage<TradableList<BuyerAsTakerTrade>> buyerAsTakerTradeStorage,
Storage<TradableList<SellerAsMakerTrade>> sellerAsMakerTradeStorage,
Storage<TradableList<SellerAsTakerTrade>> sellerAsTakerTradeStorage,
Provider<BtcWalletService> btcWalletService
) {
this.preferencesProvider = preferencesProvider;
this.addressEntryListProvider = addressEntryListProvider;
this.openOfferStorage = openOfferStorage;
this.buyerAsMakerTradeStorage = buyerAsMakerTradeStorage;
this.buyerAsTakerTradeStorage = buyerAsTakerTradeStorage;
this.sellerAsMakerTradeStorage = sellerAsMakerTradeStorage;
this.sellerAsTakerTradeStorage = sellerAsTakerTradeStorage;
this.btcWalletService = btcWalletService;
}
@Override
@ -68,9 +91,12 @@ public class CoreDiskProtoResolver implements PersistenceProtoResolver {
break;
/*
case NAVIGATION:
result = getPing(envelope);
result = PB.Navigation.fromp(envelope);
break;
*/
case TRADABLE_LIST:
result = getTradableList(envelope.getTradableList());
break;
case PEERS_LIST:
result = getPeersList(envelope.getPeersList());
break;
@ -94,8 +120,9 @@ public class CoreDiskProtoResolver implements PersistenceProtoResolver {
return Optional.ofNullable(result);
}
private Persistable getTradableList(PB.TradableList tradableList) {
return TradableList.fromProto(tradableList, openOfferStorage, buyerAsMakerTradeStorage, buyerAsTakerTradeStorage, sellerAsMakerTradeStorage, sellerAsTakerTradeStorage, btcWalletService.get());
}
private Persistable getTradeStatisticsList(PB.TradeStatisticsList tradeStatisticsList) {
return new ListPersistable<>(tradeStatisticsList.getTradeStatisticsList().stream()

View File

@ -69,7 +69,7 @@ public final class SellerAsMakerTrade extends SellerTrade implements MakerTrade
.setSellerAsMakerTrade(PB.SellerAsMakerTrade.newBuilder().setTrade((PB.Trade) super.toProto())).build();
}
public static Tradable fromProto(PB.BuyerAsTakerTrade proto, Storage<? extends TradableList> storage,
public static Tradable fromProto(PB.SellerAsMakerTrade proto, Storage<? extends TradableList> storage,
BtcWalletService btcWalletService) {
PB.Trade trade = proto.getTrade();
return new SellerAsMakerTrade(Offer.fromProto(trade.getOffer()),

View File

@ -76,7 +76,7 @@ public final class SellerAsTakerTrade extends SellerTrade implements TakerTrade
.setSellerAsTakerTrade(PB.SellerAsTakerTrade.newBuilder().setTrade((PB.Trade) super.toProto())).build();
}
public static Tradable fromProto(PB.BuyerAsTakerTrade proto, Storage<? extends TradableList> storage,
public static Tradable fromProto(PB.SellerAsTakerTrade proto, Storage<? extends TradableList> storage,
BtcWalletService btcWalletService) {
PB.Trade trade = proto.getTrade();
return new SellerAsTakerTrade(Offer.fromProto(trade.getOffer()), Coin.valueOf(trade.getTradeAmountAsLong()),

View File

@ -18,7 +18,12 @@
package io.bisq.core.trade;
import io.bisq.common.persistence.Persistable;
import io.bisq.common.storage.Storage;
import io.bisq.core.btc.wallet.BtcWalletService;
import io.bisq.core.offer.Offer;
import io.bisq.core.offer.OpenOffer;
import io.bisq.generated.protobuffer.PB;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
@ -30,4 +35,6 @@ public interface Tradable extends Persistable {
String getId();
String getShortId();
}

View File

@ -17,10 +17,11 @@
package io.bisq.core.trade;
import com.google.protobuf.Message;
import io.bisq.common.app.Version;
import io.bisq.common.persistence.Persistable;
import io.bisq.common.storage.Storage;
import io.bisq.core.btc.wallet.BtcWalletService;
import io.bisq.core.offer.OpenOffer;
import io.bisq.generated.protobuffer.PB;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
@ -31,6 +32,7 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class TradableList<T extends Tradable> implements Persistable {
@ -62,6 +64,12 @@ public final class TradableList<T extends Tradable> implements Persistable {
observableList = FXCollections.observableArrayList(list);
}
public TradableList(Storage<TradableList<T>> storage, List<T> list) {
this.storage = storage;
list.addAll(list);
observableList = FXCollections.observableArrayList(list);
}
public boolean add(T tradable) {
boolean changed = list.add(tradable);
getObservableList().add(tradable);
@ -101,4 +109,40 @@ public final class TradableList<T extends Tradable> implements Persistable {
return list.contains(thing);
}
public static TradableList fromProto(PB.TradableList proto, Storage<TradableList<OpenOffer>> openOfferStorage,
Storage<TradableList<BuyerAsMakerTrade>> buyerAsMakerTradeStorage,
Storage<TradableList<BuyerAsTakerTrade>> buyerAsTakerTradeStorage,
Storage<TradableList<SellerAsMakerTrade>> sellerAsMakerTradeStorage,
Storage<TradableList<SellerAsTakerTrade>> sellerAsTakerTradeStorage,
BtcWalletService btcWalletService) {
List collect = proto.getTradableList().stream().map(tradable -> {
switch (tradable.getMessageCase()) {
case OPEN_OFFER:
return OpenOffer.fromProto(tradable.getOpenOffer(), openOfferStorage);
case BUYER_AS_MAKER_TRADE:
return BuyerAsMakerTrade.fromProto(tradable.getBuyerAsMakerTrade(), buyerAsMakerTradeStorage, btcWalletService);
case BUYER_AS_TAKER_TRADE:
return BuyerAsTakerTrade.fromProto(tradable.getBuyerAsTakerTrade(), buyerAsTakerTradeStorage, btcWalletService);
case SELLER_AS_MAKER_TRADE:
return SellerAsMakerTrade.fromProto(tradable.getSellerAsMakerTrade(), sellerAsMakerTradeStorage, btcWalletService);
case SELLER_AS_TAKER_TRADE:
return SellerAsTakerTrade.fromProto(tradable.getSellerAsTakerTrade(), sellerAsTakerTradeStorage, btcWalletService);
}
return null;
}).collect(Collectors.toList());
switch (collect.get(0).getClass().getSimpleName()) {
case "OpenOffer":
return new TradableList<OpenOffer>(openOfferStorage, collect);
case "BuyerAsMakerTrade":
return new TradableList<BuyerAsMakerTrade>(buyerAsMakerTradeStorage, collect);
case "BuyerAsTakerTrade":
return new TradableList<BuyerAsTakerTrade>(buyerAsTakerTradeStorage, collect);
case "SellerAsMakerTrade":
return new TradableList<SellerAsMakerTrade>(sellerAsMakerTradeStorage, collect);
case "SellerAsTakerTrade":
return new TradableList<SellerAsTakerTrade>(sellerAsTakerTradeStorage, collect);
}
return null;
}
}

View File

@ -38,6 +38,7 @@ import io.bisq.core.btc.wallet.BtcWalletService;
import io.bisq.core.btc.wallet.TradeWalletService;
import io.bisq.core.filter.FilterManager;
import io.bisq.core.offer.Offer;
import io.bisq.core.offer.OpenOffer;
import io.bisq.core.offer.OpenOfferManager;
import io.bisq.core.trade.protocol.ProcessModel;
import io.bisq.core.trade.protocol.TradeProtocol;
@ -864,6 +865,7 @@ public abstract class Trade implements Tradable, Model {
.setErrorMessage(errorMessage)
.build();
}
@Override
public String toString() {
return "Trade{" +

View File

@ -17,19 +17,26 @@
package io.bisq.gui;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.protobuf.Message;
import io.bisq.common.app.Version;
import io.bisq.common.persistence.Persistable;
import io.bisq.common.storage.Storage;
import io.bisq.generated.protobuffer.PB;
import io.bisq.gui.common.view.View;
import io.bisq.gui.common.view.ViewPath;
import io.bisq.gui.main.MainView;
import io.bisq.gui.main.market.MarketView;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;
public final class Navigation implements Persistable {
// That object is saved to disc. We need to take care of changes to not break deserialization.
@ -53,6 +60,8 @@ public final class Navigation implements Persistable {
transient private ViewPath returnPath;
// Persisted fields
@Getter
@Setter
private ViewPath previousPath;
@ -71,6 +80,16 @@ public final class Navigation implements Persistable {
currentPath = null;
}
/** used for deserialisation/fromProto */
public Navigation(List<Class<? extends View>> classes) {
previousPath = new ViewPath(Lists.newArrayList());
previousPath.addAll(classes);
// need to be null initially and not DEFAULT_VIEW_PATH to navigate through all items
currentPath = null;
storage = null;
}
@SuppressWarnings("unchecked")
public void navigateTo(Class<? extends View>... viewClasses) {
navigateTo(ViewPath.to(viewClasses));
@ -133,7 +152,23 @@ public final class Navigation implements Persistable {
this.returnPath = returnPath;
}
private ViewPath getPreviousPath() {
return previousPath;
@Override
public Message toProto() {
return PB.Navigation.newBuilder().setPreviousPath(PB.ViewPath.newBuilder()
.addAllViewPath(previousPath.stream()
.map(aClass -> aClass.getName()).collect(Collectors.toList()))).build();
}
public static Navigation fromProto(PB.Navigation proto) {
List<Class<? extends View>> classStream = proto.getPreviousPath().getViewPathList().stream().map(s -> {
try {
return ((Class<? extends View>) Class.forName(s));
} catch (ClassNotFoundException e) {
log.warn("Could not find the Viewpath class {}; exception: {}", s, e);
}
return null;
}).collect(Collectors.toList());
return new Navigation(classStream);
}
}