Skip to content

Instantly share code, notes, and snippets.

@andeeliao
Created December 11, 2023 01:38
Show Gist options
  • Save andeeliao/4f26f6f409714c224cd155452c8ced04 to your computer and use it in GitHub Desktop.
Save andeeliao/4f26f6f409714c224cd155452c8ced04 to your computer and use it in GitHub Desktop.
MVVM protocols setup for swift ios project
//
// MVVM.swift
// ios-app
//
// Created by xue qi liao on 9/28/23.
//
// FIXME: this entire protocol stack needs to be related, as in ONE set of MVVM should be
// related and we shouldn't be passing unrelated Models to unrelated ViewModels or Views
// but that is beyond the scope of MVP
// actually now I'm not super sure, models are definitely sharable
// maybe ViewModels will be to some extent urghhh
// RULE: I think these protocols are more for documentation than anything else
// since I can't figure out how to make the vars in the protocols the specific
// instance of MVVM instead of generic protocols
protocol CustomModelProtocol: Equatable {
// TODO: same todo as in the extension, we should be using this id
// to test equality but it's not working for some reason
var id: Int { get }
// TODO: is there anything all models should implement???
// RULE: in theory only relevant data from AppState should
// added to the models when initilized from AppState
// this might look more generic, the init might be optional
// this used to init from AppState but I think that's too heavy handled
// limit the scope helps with conceptual thinking and breaking down
// the work, so leave this commented out for now
// for larger views it definitely makes sense to initialize from AppState though
// init(state: AppState)
}
// MARK: Equitable conformance
//extension CustomModelProtocol {
// func isEqual(to other: CustomModelProtocol) -> Bool {
// return self == other
// }
//}
// TODO: figure out why this isn't working
extension CustomModelProtocol {
static func == (lhs: any CustomModelProtocol, rhs: any CustomModelProtocol) -> Bool {
return lhs.id == rhs.id
}
}
protocol CustomViewModelProtocol {
// RULE: in theory viewModels should only contain relevant data
// TODO: I think we actually always have a 1:1 mapping for instances of model:viewModel
// so it should be init and not update?
// can't make inits generic? how to do???
// init(model: CustomModelProtocol)
}
protocol CustomViewProtocol {
// RULE: this should also update the models of subviews
// was debating between having the view or viewmodel do this
// but passing a callback down and then getting it called in view
// because view holds subviews, seems like a lot?
// just let the view do it for now and see what happens
func update(viewModel: CustomViewModelProtocol)
// TODO: this feels like it could be more generic but HOW
// func updateState(oldState: AppState, newState: AppState)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment