Vasilis

fix for navigating back from coupons and more

This diff could not be displayed because it is too large.
......@@ -7,6 +7,9 @@
#if canImport(SwiftUI)
import SwiftUI
import Combine
import Foundation
class DataModel {
var data: Array<NSDictionary> = []
......@@ -50,63 +53,42 @@ class DataModel {
}
}
struct UrlImageView: View {
@ObservedObject var urlImageModel: UrlImageModel
init(urlString: String?) {
urlImageModel = UrlImageModel(urlString: urlString)
class ImageLoader: ObservableObject {
var didChange = PassthroughSubject<Data, Never>()
var data = Data() {
didSet {
didChange.send(data)
}
}
var body: some View {
Image(uiImage: urlImageModel.image ?? UrlImageView.defaultImage!)
.resizable()
.scaledToFill()
init(urlString:String) {
guard let url = URL(string: urlString) else { return }
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { return }
DispatchQueue.main.async {
self.data = data
}
}
task.resume()
}
static var defaultImage = UIImage(systemName: "photo")
}
class UrlImageModel: ObservableObject {
@Published var image: UIImage?
var urlString: String?
struct ImageView: View {
@ObservedObject var imageLoader:ImageLoader
@State var image:UIImage = UIImage()
init(urlString: String?) {
self.urlString = urlString
loadImage()
init(withURL url:String) {
imageLoader = ImageLoader(urlString:url)
}
func loadImage() {
loadImageFromUrl()
}
func loadImageFromUrl() {
guard let urlString = urlString else {
return
}
let url = URL(string: urlString)!
let task = URLSession.shared.dataTask(with: url, completionHandler:
getImageFromResponse(data:response:error:))
task.resume()
}
func getImageFromResponse(data: Data?, response: URLResponse?, error: Error?)
{
guard error == nil else {
print("Error: \(error!)")
return
}
guard let data = data else {
print("No data found")
return
}
var body: some View {
DispatchQueue.main.async {
guard let loadedImage = UIImage(data: data) else {
return
}
self.image = loadedImage
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fit)
// .frame(width:100, height:100)
.onReceive(imageLoader.didChange) { data in
self.image = UIImage(data: data) ?? UIImage()
}
}
}
......@@ -117,41 +99,61 @@ extension CouponsView {
var uiscreen = UIScreen.main.bounds
var body: some View {
ZStack {
HStack {
Image("ic_back", bundle: Bundle(for: MyEmptyClass.self))
.resizable()
.frame(width: self.uiscreen.height * 0.025, height: self.uiscreen.height * 0.02)
.offset(x: -self.uiscreen.width / 2 + self.uiscreen.width * 0.05, y: self.uiscreen.height * 0.07)
.frame(width: self.uiscreen.width * 0.025, height: self.uiscreen.height * 0.02)
.offset(x: -self.uiscreen.width / 2 + self.uiscreen.width * 0.05, y: self.uiscreen.height * 0.04)
Text("Όλα τα κουπόνια μου")
.frame(width: self.uiscreen.width * 0.8, height: self.uiscreen.height * 0.025, alignment: .center)
.offset( y: self.uiscreen.height * 0.07)
.frame(width: self.uiscreen.width)
.offset( y: self.uiscreen.height * 0.04)
}
.frame(width: self.uiscreen.width, height: self.uiscreen.height * 0.12)
}
}
struct couponView: View {
var result: NSDictionary
var index: Int
var uiscreen = UIScreen.main.bounds
static func localizedWithParameter(parameter: String) -> LocalizedStringKey {
return "\(parameter)"
}
var body: some View {
ZStack {
UrlImageView(urlString: result["img_preview"] as? String)
.frame(width: self.uiscreen.height * 0.25, height: self.uiscreen.height * 0.05, alignment: .leading)
.offset(x: self.uiscreen.width * 0.05, y: self.uiscreen.height * 0.06)
HStack(alignment: .center) {
HStack(alignment: .center) {
ImageView(withURL: result["img_preview"] as! String)
.frame(width: self.uiscreen.width * 0.2, height: self.uiscreen.height * 0.05)
// .offset(x: -self.uiscreen.width / 2 + self.uiscreen.width * 0.2, y: -self.uiscreen.height * 0.05 * 0.96)
// .padding(.leading, self.uiscreen.width * 0.05)
Rectangle()
.frame(width: 1, height: self.uiscreen.height * 0.1 * 0.93)
// .offset(x: -self.uiscreen.width / 2 + self.uiscreen.width * 0.205, y: -self.uiscreen.height * 0.05 * 0.93)
VStack {
Text(CouponsView.couponView.localizedWithParameter(parameter: result["admin_name"] as! String))
Text(CouponsView.couponView.localizedWithParameter(parameter: result["discount"] as! String))
Text(CouponsView.couponView.localizedWithParameter(parameter: "Ισχύει εώς " + (result["expiration"] as! String).components(separatedBy: [" "]).filter({!$0.isEmpty})[0]))
}
.frame(maxHeight: .infinity)
VStack {
Text(CouponsView.couponView.localizedWithParameter(parameter: result["short_description"] as! String))
}
.frame(maxWidth: self.uiscreen.width * 0.15, maxHeight: .infinity)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.background(
Image("coupons_container", bundle: Bundle(for: MyEmptyClass.self))
.resizable()
.offset(x: self.uiscreen.width * 0.05, y: (CGFloat(index + 1) * self.uiscreen.height * 0.06) + CGFloat(index) * self.uiscreen.height * 0.17)
.padding()
.frame(width: UIScreen.main.bounds.width * 0.9, height: UIScreen.main.bounds.height * 0.17, alignment: .topLeading)
)
// .padding(.top, self.uiscreen.height * 0.05)
.frame(width: self.uiscreen.width * 0.9, height: self.uiscreen.height * 0.1)
.background(Color.purple)
}
}
......@@ -174,10 +176,12 @@ struct CouponsView: View {
couponView(result: result, index: index)
}
}
}
.frame(width:self.uiscreen.width, height:self.uiscreen.height )
}
}
}.frame(width:self.uiscreen.width, height:self.uiscreen.height * 0.88 )
}
.frame(width:self.uiscreen.width, height:self.uiscreen.height )
.background(Color.red)
}
}
#endif
......
......@@ -11,8 +11,8 @@ import SwiftUI
@available(iOS 13.0.0, *)
@objc public class CouponsViewInterface : NSObject {
@objc static public func couponsViewController() -> UIViewController {
return UIHostingController(rootView: CouponsView())
@objc(couponsViewController:) static public func couponsViewController(parentView: UIView?) -> UIViewController {
return UIHostingController(rootView: CouponsView(parentView: parentView!))
}
}
......
......@@ -15,7 +15,7 @@
+ (void)init:(NSDictionary *)launchOptions uuid:(NSString*)uuid merchantId:(NSString*)merchantId lang:(NSString*)lang;
- (void) setToStage;
- (void) setLang:(NSString*) lang;
- (UIViewController *) openCoupons;
- (UIViewController *) openCoupons:(UIView*) parentView;
- (void) applicationDidEnterBackground:(UIApplication *)application;
- (void) applicationWillEnterForeground:(UIApplication *)application;
- (void) applicationDidBecomeActive:(UIApplication *)application;
......
......@@ -46,12 +46,8 @@ NSString *LANG;
LANG = lang;
}
- (UIViewController *) openCoupons{
UIViewController *couponsViewController = [CouponsViewInterface couponsViewController];
// controller = [[UINavigationController alloc]initWithRootViewController:profileViewController];
// [window makeKeyAndVisible];
- (UIViewController *) openCoupons:(UIView*) parentView{
UIViewController *couponsViewController = [CouponsViewInterface couponsViewController:parentView];
return couponsViewController;
}
......