summaryrefslogtreecommitdiff
path: root/Rune/Views/Domains/DomainListView.swift
blob: d728bd7ed4f77a024b35a3049bb6a14dd72933c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import SwiftUI

struct DomainListView: View {
    @ObservedObject var viewModel: DomainViewModel
    let client: NjallaClient?

    var body: some View {
        NavigationStack {
            Group {
                if let client {
                    content(client: client)
                } else {
                    ContentUnavailableView("Sign in required", systemImage: "key.fill", description: Text("Add a valid Njalla API token to load domains."))
                }
            }
            .navigationTitle("Domains")
        }
        .alert("API Error", isPresented: errorBinding) {
            Button("OK", role: .cancel) {}
        } message: {
            Text(viewModel.errorMessage ?? "")
        }
    }

    @ViewBuilder
    private func content(client: NjallaClient) -> some View {
        if viewModel.isLoadingDomains && viewModel.domains.isEmpty {
            ProgressView()
        } else if viewModel.domains.isEmpty {
            ContentUnavailableView("No Domains", systemImage: "globe", description: Text("No domains found on this account."))
        } else {
            List(viewModel.domains) { domain in
                NavigationLink {
                    DomainDetailView(domainName: domain.name, viewModel: viewModel, client: client)
                } label: {
                    DomainRow(domain: domain)
                }
            }
            .listStyle(.insetGrouped)
            .refreshable {
                await viewModel.loadDomains(client: client)
            }
        }
    }

    private var errorBinding: Binding<Bool> {
        Binding(
            get: { viewModel.errorMessage != nil },
            set: { newValue in
                if !newValue {
                    viewModel.errorMessage = nil
                }
            }
        )
    }
}

private struct DomainRow: View {
    let domain: Domain

    var body: some View {
        VStack(alignment: .leading, spacing: 6) {
            Text(domain.name)
                .font(.headline)

            HStack {
                if let status = domain.status {
                    Text(status)
                }

                if let expiry = domain.expiry {
                    Text("Expiry: \(expiry.formattedExpiry())")
                }
            }
            .font(.subheadline)
            .foregroundStyle(.secondary)

            if let autorenew = domain.autorenew {
                Text(autorenew ? "Autorenew On" : "Autorenew Off")
                    .font(.subheadline)
                    .foregroundStyle(.secondary)
            }
        }
        .padding(.vertical, 4)
    }
}