Skip to content

Commit

Permalink
Add a default URL parameter
Browse files Browse the repository at this point in the history
Setting a default URL in settings will allow for navigation to other
websites.

This change will be active on both the home button and app loading.

Also make the WebViewModel an EnvironmentObject since it's used
everywhere.

Signed-off-by: kingbri <bdashore3@gmail.com>
  • Loading branch information
bdashore3 committed Oct 3, 2021
1 parent 88d30f1 commit 01cba70
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 24 deletions.
Binary file not shown.
3 changes: 2 additions & 1 deletion Cubari/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ struct ContentView: View {

// Navigation bar view
if model.showNavigation {
NavigationBarView(model: model)
NavigationBarView()
.zIndex(1)
}
}
.environmentObject(model)
}
}

Expand Down
15 changes: 7 additions & 8 deletions Cubari/NavigationBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct NavigationBarView: View {
@ObservedObject var model: WebViewModel
@AppStorage("leftHandMode") var leftHandMode = false
@State private var showAbout = false

Expand All @@ -19,21 +18,21 @@ struct NavigationBarView: View {
// Sets button position depending on hand mode setting
HStack {
if leftHandMode {
ForwardBackButtonView(model: model)
ForwardBackButtonView()
Spacer()
SettingsButtonView(model: model)
SettingsButtonView()
Spacer()
HomeButtonView(model: model)
HomeButtonView()
Spacer()
AboutButtonView()
} else {
AboutButtonView()
Spacer()
HomeButtonView(model: model)
HomeButtonView()
Spacer()
SettingsButtonView(model: model)
SettingsButtonView()
Spacer()
ForwardBackButtonView(model: model)
ForwardBackButtonView()
}
}
.padding()
Expand All @@ -47,7 +46,7 @@ struct NavigationBarView: View {
#if DEBUG
struct NavigationBarView_Previews: PreviewProvider {
static var previews: some View {
NavigationBarView(model: WebViewModel())
NavigationBarView()
}
}
#endif
4 changes: 2 additions & 2 deletions Cubari/NavigationBarViews/ForwardBackButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct ForwardBackButtonView: View {
@ObservedObject var model: WebViewModel
@EnvironmentObject var model: WebViewModel

var body: some View {
Button(action: {
Expand All @@ -32,7 +32,7 @@ struct ForwardBackButtonView: View {
#if DEBUG
struct ForwardBackButtonView_Previews: PreviewProvider {
static var previews: some View {
ForwardBackButtonView(model: WebViewModel())
ForwardBackButtonView()
}
}
#endif
4 changes: 2 additions & 2 deletions Cubari/NavigationBarViews/HomeButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct HomeButtonView: View {
@ObservedObject var model: WebViewModel
@EnvironmentObject var model: WebViewModel

var body: some View {
Button(action: {
Expand All @@ -22,7 +22,7 @@ struct HomeButtonView: View {
#if DEBUG
struct HomeButtonView_Previews: PreviewProvider {
static var previews: some View {
HomeButtonView(model: WebViewModel())
HomeButtonView()
}
}
#endif
6 changes: 3 additions & 3 deletions Cubari/NavigationBarViews/SettingsButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import SwiftUI

struct SettingsButtonView: View {
@ObservedObject var model: WebViewModel
@EnvironmentObject var model: WebViewModel
@State private var showSettings = false

var body: some View {
Expand All @@ -18,15 +18,15 @@ struct SettingsButtonView: View {
Image(systemName: "gear")
})
.sheet(isPresented: $showSettings) {
SettingsView(model: model, showView: $showSettings)
SettingsView(showView: $showSettings)
}
}
}

#if DEBUG
struct SettingsButtonView_Previews: PreviewProvider {
static var previews: some View {
SettingsButtonView(model: WebViewModel())
SettingsButtonView()
}
}
#endif
9 changes: 7 additions & 2 deletions Cubari/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import SwiftUI

struct SettingsView: View {
@ObservedObject var model: WebViewModel
@EnvironmentObject var model: WebViewModel
@Binding var showView: Bool

// All settings here
@AppStorage("leftHandMode") var leftHandMode = false
@AppStorage("persistNavigation") var persistNavigation = false
@AppStorage("blockAds") var blockAds = false
@AppStorage("defaultUrl") var defaultUrl = ""

@State private var showAdblockAlert: Bool = false

Expand Down Expand Up @@ -53,6 +54,10 @@ struct SettingsView: View {
)
}
}
Section(header: Text("Default URL"), footer: Text("Sets the default URL when the app is launched. Https will be automatically added if you don't provide it")) {
TextField("https://...", text: $defaultUrl)
.textCase(.lowercase)
}
}
.navigationBarTitle("Settings")
.toolbar {
Expand All @@ -69,7 +74,7 @@ struct SettingsView: View {
#if DEBUG
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
SettingsView(model: WebViewModel(), showView: .constant(true))
SettingsView(showView: .constant(true))
}
}
#endif
21 changes: 15 additions & 6 deletions Cubari/WebViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ class WebViewModel: ObservableObject {

// All Settings go here
@AppStorage("blockAds") var blockAds = false

// TODO: Change this to a default URL
private var homeUrl = URL(string: "https://cubari.moe")!

@AppStorage("defaultUrl") var defaultUrl = ""

init() {
let prefs = WKWebpagePreferences()
prefs.allowsContentJavaScript = true
Expand Down Expand Up @@ -95,13 +93,24 @@ class WebViewModel: ObservableObject {
// Force the home URL if the user wants to go home
// Otherwise, use the current URL with the home URL as a fallback
func loadUrl(goHome: Bool = false) {
let url = goHome ? homeUrl : webView.url ?? homeUrl
let url = goHome ? buildHomeUrl() : webView.url ?? buildHomeUrl()

let urlRequest = URLRequest(url: url)

self.webView.load(urlRequest)
}

// Builds homepage URL from settings
func buildHomeUrl() -> URL {
if defaultUrl == "" {
return URL(string: "https://cubari.moe")!
} else if defaultUrl.hasPrefix("https://") || defaultUrl.hasPrefix("http://") {
return URL(string: defaultUrl)!
} else {
return URL(string: "https://\(defaultUrl)")!
}
}

func goForward() {
webView.goForward()
}
Expand Down

0 comments on commit 01cba70

Please sign in to comment.