carson-katri / swift-request

Declarative HTTP networking, designed for SwiftUI
MIT License
727 stars 41 forks source link

Add 'update' methods, which allow for repeated calls to Requests. #24

Closed ezraberch closed 4 years ago

ezraberch commented 4 years ago

This PR adds functionality to Request which allows for the Request to be repeated after the initial call. An example use case is an automatically-updating RequestView.

The following SwiftUI view is an example of this. The RequestView will call the request to load the content once when the view loads. Thereafter, the view will automatically update every 10 seconds. The view will also be updated whenever the user taps the button.

let subject = PassthroughSubject<Void, Never>()

    struct ContentView: View {
        var body: some View {
            VStack {
                Button(action: {
                    subject.send()
                }, label: {
                    Text("Refresh")
                })
                RequestView (Request {
                    Url("https://worldtimeapi.org/api/ip.txt")
                }.update(publisher: subject).update(every: 10)) {data in
                    Text(String(decoding: data ?? Data(), as: UTF8.self))
                    Text("placeholder")
                }
            }
        }
    }
codecov-commenter commented 4 years ago

Codecov Report

Merging #24 into master will increase coverage by 0.25%. The diff coverage is 92.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #24      +/-   ##
==========================================
+ Coverage   85.77%   86.03%   +0.25%     
==========================================
  Files          21       21              
  Lines         682      716      +34     
==========================================
+ Hits          585      616      +31     
- Misses         97      100       +3     
Impacted Files Coverage Δ
Sources/Request/Request/Request.swift 93.26% <89.28%> (-1.86%) :arrow_down:
Tests/RequestTests/RequestTests.swift 93.28% <100.00%> (+0.33%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update db1db61...0db9e72. Read the comment docs.

carson-katri commented 4 years ago

This looks great!

I have two notes:

  1. Lets make public func update(every seconds: Double) -> Self use TimeInterval instead of Double.
  2. Does it need combineIdentifier if the class doesn't conform to CustomCombineIdentifierConvertible? Also, could it be a constant instead of a variable?
ezraberch commented 4 years ago
  1. Done.
  2. The class does conform to CustomCombineIdentifierConvertible as Subscriber inherits from it. Also note from https://developer.apple.com/documentation/combine/customcombineidentifierconvertible:

If you create a custom Subscription or Subscriber type, implement this protocol so that development tools can uniquely identify publisher chains in your app. If your type is a class, Combine provides an implementation of combineIdentifier for you. If your type is a structure, set up the identifier as follows: let combineIdentifier = CombineIdentifier()

You're right that it should be a constant; that has been fixed.