captains-log

[TO-FIX] audiojournaling app
git clone git://git.figbert.com/captains-log.git
Log | Files | Refs

Elements.swift (7391B)


      1 //
      2 //  extensions.swift
      3 //  captainsLog
      4 //
      5 //  Created by Benjamin Welner on 10/18/19.
      6 //  Copyright © 2019 FIGBERT Industries. All rights reserved.
      7 //
      8 
      9 import Foundation
     10 import SwiftUI
     11 
     12 extension Double {
     13     func withCommas() -> String {
     14         let numberFormatter = NumberFormatter()
     15         numberFormatter.numberStyle = NumberFormatter.Style.decimal
     16         return numberFormatter.string(from: NSNumber(value:self))!
     17     }
     18 }
     19 extension Date {
     20     func stardate() -> [String] {
     21         let calendar: Calendar = Calendar.init(identifier: .gregorian)
     22         let year = calendar.component(.year, from: self)
     23         let month = calendar.component(.month, from: self)
     24         let day = calendar.component(.day, from: self)
     25         let leapyear = !(year % 100 == 0) && year % 4 == 0
     26         let months = [
     27             1: 0,
     28             2: 31,
     29             3: !leapyear ? 59 : 60,
     30             4: !leapyear ? 90 : 91,
     31             5: !leapyear ? 120 : 121,
     32             6: !leapyear ? 151 : 152,
     33             7: !leapyear ? 181 : 182,
     34             8: !leapyear ? 212 : 213,
     35             9: !leapyear ? 243 : 244,
     36             10: !leapyear ? 273 : 274,
     37             11: !leapyear ? 304 : 305,
     38             12: !leapyear ? 334 : 335
     39         ]
     40         let n = !leapyear ? 365 : 366
     41         let b = 2005
     42         let c = 58000.0
     43         let monthValue = Double(months[month] ?? 0)
     44         let stageOne = Double(1000*(year-b))
     45         let stageTwo = (1000.0/Double(n))*(monthValue + Double(day) - 1.0)
     46         let stageThree = c + stageOne + stageTwo
     47         let stardate = String(Double(round(100*stageThree)/100).withCommas())
     48         return ["stardate", stardate]
     49     }
     50     func multi(type: String) -> [String] {
     51         let calendar_list: [String: [String]] = [
     52             "Jewish": [
     53                 "Tishrei",
     54                 "Cheshvan",
     55                 "Kislev",
     56                 "Tevet",
     57                 "Shevat",
     58                 "Adar",
     59                 "Adar II",
     60                 "Nisan",
     61                 "Iyar",
     62                 "Sivan",
     63                 "Tamuz",
     64                 "Av",
     65                 "Elul"
     66             ],
     67             "Islamic": [
     68                 "Muharram",
     69                 "Safar",
     70                 "Rabi al-Awwal",
     71                 "Rabi al-Thani",
     72                 "Jumada al-Ula",
     73                 "Jumada al-Akhirah",
     74                 "Rajab",
     75                 "Sha'ban",
     76                 "Ramadan",
     77                 "Shawwal",
     78                 "Zulqiddah",
     79                 "Zulhijjah"
     80             ],
     81             "Persian": [
     82                 "Farvardin",
     83                 "Ordibehesht",
     84                 "Khordad",
     85                 "Tir",
     86                 "Mordad",
     87                 "Shahrivar",
     88                 "Mehr",
     89                 "Aban",
     90                 "Azar",
     91                 "Dey",
     92                 "Bahman",
     93                 "Esfand"
     94             ],
     95             "Indian": [
     96                 "Vaisakha",
     97                 "Jyeshtha",
     98                 "Ashadha",
     99                 "Shraavana",
    100                 "Bhadra",
    101                 "Ashvin",
    102                 "Kartik",
    103                 "Agahana",
    104                 "Pausha",
    105                 "Magha",
    106                 "Phalguna",
    107                 "Chaitra"
    108             ],
    109             "Coptic": [
    110                 "Thout",
    111                 "Paopi",
    112                 "Hathor",
    113                 "Koiak",
    114                 "Tobi",
    115                 "Meshir",
    116                 "Paremhat",
    117                 "Parmouti",
    118                 "Pashons",
    119                 "Paoni",
    120                 "Epip",
    121                 "Mesori",
    122                 "Pi Kogi Enavot"
    123             ],
    124             "Chinese": [
    125                 "yínyuè",
    126                 "mǎoyuè",
    127                 "chényuè",
    128                 "sìyuè",
    129                 "wǔyuè",
    130                 "wèiyuè",
    131                 "shēnyuè",
    132                 "yǒuyuè",
    133                 "xūyuè",
    134                 "hàiyuè",
    135                 "zǐyuè",
    136                 "chǒuyuè"
    137             ],
    138             "Japanese": [
    139                 "Mutsuki",
    140                 "Kisaragi",
    141                 "Yayoi",
    142                 "Uzuki",
    143                 "Satsuki",
    144                 "Minazuki",
    145                 "Fumizuki",
    146                 "Hazuki",
    147                 "Nagatsuki",
    148                 "Kannazuki",
    149                 "Shimotsuki",
    150                 "Shiwasu"
    151             ],
    152             "Ethiopian": [
    153                 "Mäskäräm",
    154                 "Ṭəqəmt",
    155                 "Ḫədar",
    156                 "Taḫśaś",
    157                 "Ṭərr",
    158                 "Yäkatit",
    159                 "Mägabit",
    160                 "Miyazya",
    161                 "Gənbo",
    162                 "Säne",
    163                 "Ḥamle",
    164                 "Nähase",
    165                 "Ṗagume"
    166             ],
    167             "Gregorian": [
    168                 "January",
    169                 "February",
    170                 "March",
    171                 "April",
    172                 "May",
    173                 "June",
    174                 "July",
    175                 "August",
    176                 "September",
    177                 "October",
    178                 "November",
    179                 "December"
    180             ]
    181         ]
    182         var calendar: Calendar
    183         switch type {
    184         case "Jewish":
    185             calendar = Calendar.init(identifier: .hebrew)
    186         case "Islamic":
    187             calendar = Calendar.init(identifier: .islamic)
    188         case "Persian":
    189             calendar = Calendar.init(identifier: .persian)
    190         case "Indian":
    191             calendar = Calendar.init(identifier: .indian)
    192         case "Coptic":
    193             calendar = Calendar.init(identifier: .coptic)
    194         case "Ethiopian":
    195             calendar = Calendar.init(identifier: .ethiopicAmeteAlem)
    196         case "Chinese":
    197             calendar = Calendar.init(identifier: .chinese)
    198         case "Japanese":
    199             calendar = Calendar.init(identifier: .japanese)
    200         default:
    201             calendar = Calendar.init(identifier: .gregorian)
    202         }
    203         let year = String(calendar.component(.year, from: self))
    204         let numMonth = calendar.component(.month, from: self)
    205         let month = calendar_list["\(type)"]?[numMonth-1] ?? "month"
    206         let dayNum = calendar.component(.day, from: self)
    207         let numberFormatter = NumberFormatter()
    208         numberFormatter.numberStyle = .ordinal
    209         let day = String(numberFormatter.string(from: NSNumber(value: dayNum)) ?? "day")
    210         return [day, " of ", month, ", ", year]
    211     }
    212     func toString( dateFormat format  : String ) -> String {
    213         let dateFormatter = DateFormatter()
    214         dateFormatter.dateFormat = format
    215         return dateFormatter.string(from: self)
    216     }
    217 }
    218 
    219 struct imgTextButtonStyle: View {
    220     var sysImg: String
    221     var imgSize: Int
    222     var buttonText: String
    223     @Environment(\.colorScheme) var colorScheme: ColorScheme
    224     
    225     var body: some View {
    226         HStack {
    227             Image(
    228                 uiImage: UIImage(
    229                     systemName: sysImg,
    230                     withConfiguration: UIImage.SymbolConfiguration(
    231                         pointSize: CGFloat.init(imgSize),
    232                         weight: .medium,
    233                         scale: .large
    234                     )
    235                 )!
    236             )
    237             Text(buttonText)
    238         }
    239         .padding()
    240         .background(Color.primary)
    241         .foregroundColor(colorScheme == .light ? Color.white : Color.black)
    242         .cornerRadius(20)
    243     }
    244 }