198 lines
7.1 KiB
Swift
198 lines
7.1 KiB
Swift
import SwiftUI
|
||
|
||
struct ActiveSessionsView: View {
|
||
@State private var sessions: [SessionViewData] = []
|
||
@State private var isLoading = false
|
||
@State private var loadError: String?
|
||
|
||
private let sessionsService = SessionsService()
|
||
|
||
var body: some View {
|
||
List {
|
||
if isLoading && sessions.isEmpty {
|
||
loadingState
|
||
} else if let loadError, sessions.isEmpty {
|
||
errorState(loadError)
|
||
} else if sessions.isEmpty {
|
||
emptyState
|
||
} else {
|
||
Section(header: Text(NSLocalizedString("Сессии", comment: "Активные сессии - заголовок секции"))) {
|
||
ForEach(sessions) { session in
|
||
sessionRow(for: session)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.navigationTitle(NSLocalizedString("Активные сессии", comment: "Заголовок экрана активных сессий"))
|
||
.navigationBarTitleDisplayMode(.inline)
|
||
.task {
|
||
await loadSessions()
|
||
}
|
||
.refreshable {
|
||
await loadSessions()
|
||
}
|
||
}
|
||
|
||
private var loadingState: some View {
|
||
Section {
|
||
ProgressView()
|
||
.frame(maxWidth: .infinity, alignment: .center)
|
||
}
|
||
}
|
||
|
||
private func errorState(_ message: String) -> some View {
|
||
Section {
|
||
VStack(spacing: 12) {
|
||
Image(systemName: "exclamationmark.triangle")
|
||
.font(.system(size: 40))
|
||
.foregroundColor(.orange)
|
||
Text(message)
|
||
.font(.body)
|
||
.multilineTextAlignment(.center)
|
||
}
|
||
.frame(maxWidth: .infinity)
|
||
.padding(.vertical, 24)
|
||
}
|
||
}
|
||
|
||
private var emptyState: some View {
|
||
Section {
|
||
VStack(spacing: 12) {
|
||
Image(systemName: "iphone")
|
||
.font(.system(size: 40))
|
||
.foregroundColor(.secondary)
|
||
Text(NSLocalizedString("Активные сессии не найдены", comment: "Пустой список активных сессий"))
|
||
.font(.headline)
|
||
.multilineTextAlignment(.center)
|
||
Text(NSLocalizedString("Войдите с другого устройства, чтобы увидеть его здесь.", comment: "Подсказка при отсутствии активных сессий"))
|
||
.font(.subheadline)
|
||
.foregroundColor(.secondary)
|
||
.multilineTextAlignment(.center)
|
||
}
|
||
.frame(maxWidth: .infinity)
|
||
.padding(.vertical, 24)
|
||
}
|
||
.listRowSeparator(.hidden)
|
||
}
|
||
|
||
private func sessionRow(for session: SessionViewData) -> some View {
|
||
VStack(alignment: .leading, spacing: 10) {
|
||
HStack(alignment: .top) {
|
||
VStack(alignment: .leading, spacing: 6) {
|
||
Text(session.clientTypeDisplay)
|
||
.font(.headline)
|
||
if let ip = session.ipAddress, !ip.isEmpty {
|
||
Label(ip, systemImage: "globe")
|
||
.font(.subheadline)
|
||
.foregroundColor(.secondary)
|
||
}
|
||
}
|
||
Spacer()
|
||
if session.isCurrent {
|
||
Text(NSLocalizedString("Текущая", comment: "Маркер текущей сессии"))
|
||
.font(.caption2.weight(.semibold))
|
||
.padding(.horizontal, 10)
|
||
.padding(.vertical, 4)
|
||
.background(Color.accentColor.opacity(0.15))
|
||
.foregroundColor(.accentColor)
|
||
.clipShape(Capsule())
|
||
}
|
||
}
|
||
|
||
if let userAgent = session.userAgent, !userAgent.isEmpty {
|
||
Text(userAgent)
|
||
.font(.footnote)
|
||
.foregroundColor(.secondary)
|
||
.lineLimit(3)
|
||
}
|
||
|
||
VStack(alignment: .leading, spacing: 6) {
|
||
Label(session.createdAtText, systemImage: "calendar")
|
||
.font(.caption)
|
||
.foregroundColor(.secondary)
|
||
Label(session.lastRefreshText, systemImage: "arrow.clockwise")
|
||
.font(.caption)
|
||
.foregroundColor(.secondary)
|
||
}
|
||
}
|
||
.padding(.vertical, 6)
|
||
}
|
||
|
||
@MainActor
|
||
private func loadSessions() async {
|
||
if isLoading {
|
||
return
|
||
}
|
||
|
||
isLoading = true
|
||
loadError = nil
|
||
|
||
do {
|
||
let payloads = try await sessionsService.fetchSessions()
|
||
sessions = payloads.map(SessionViewData.init)
|
||
} catch {
|
||
loadError = error.localizedDescription
|
||
if AppConfig.DEBUG {
|
||
print("[ActiveSessionsView] load sessions failed: \(error)")
|
||
}
|
||
}
|
||
|
||
isLoading = false
|
||
}
|
||
}
|
||
|
||
private struct SessionViewData: Identifiable, Equatable {
|
||
let id: UUID
|
||
let ipAddress: String?
|
||
let userAgent: String?
|
||
let clientType: String
|
||
let createdAt: Date
|
||
let lastRefreshAt: Date
|
||
let isCurrent: Bool
|
||
|
||
init(payload: UserSessionPayload) {
|
||
self.id = payload.id
|
||
self.ipAddress = payload.ipAddress
|
||
self.userAgent = payload.userAgent
|
||
self.clientType = payload.clientType
|
||
self.createdAt = payload.createdAt
|
||
self.lastRefreshAt = payload.lastRefreshAt
|
||
self.isCurrent = payload.isCurrent
|
||
}
|
||
|
||
var clientTypeDisplay: String {
|
||
let normalized = clientType.lowercased()
|
||
switch normalized {
|
||
case "mobile":
|
||
return NSLocalizedString("Мобильное приложение", comment: "Тип сессии — мобильное приложение")
|
||
case "web":
|
||
return NSLocalizedString("Веб", comment: "Тип сессии — веб")
|
||
case "desktop":
|
||
return NSLocalizedString("Десктоп", comment: "Тип сессии — десктоп")
|
||
case "bot":
|
||
return NSLocalizedString("Бот", comment: "Тип сессии — бот")
|
||
default:
|
||
return clientType.capitalized
|
||
}
|
||
}
|
||
|
||
var createdAtText: String {
|
||
let formatted = Self.dateFormatter.string(from: createdAt)
|
||
return String(format: NSLocalizedString("Создана: %@", comment: "Дата создания сессии"), formatted)
|
||
}
|
||
|
||
var lastRefreshText: String {
|
||
let formatted = Self.dateFormatter.string(from: lastRefreshAt)
|
||
return String(format: NSLocalizedString("Последнее обновление: %@", comment: "Дата последнего обновления сессии"), formatted)
|
||
}
|
||
|
||
private static let dateFormatter: DateFormatter = {
|
||
let formatter = DateFormatter()
|
||
formatter.locale = Locale.current
|
||
formatter.timeZone = .current
|
||
formatter.dateStyle = .medium
|
||
formatter.timeStyle = .short
|
||
return formatter
|
||
}()
|
||
}
|