It's always a good idea to read the documentation many times. From your description it sounds like Vim already does what you want, you just have to tell it to do it:
:q[uit] Quit the current window. Quit Vim if this is the last
window. This fails when changes have been made and
Vim refuses to |abandon| the current buffer, and when
the last file in the argument list has not been
edited.
:q[uit]! Quit without writing, also when the current buffer has
changes. The buffer is unloaded, also when it has
'hidden' set.
and
:[range]x[it][!] [++opt] [file]
Like ":wq", but write only when changes have been
made.
When 'hidden' is set and there are more windows, the
current buffer becomes hidden, after writing the file.
Similarly, you can write if the file has changed and quit:
ZZ Write current file, if modified, and quit (same as
":x"). (Note: If there are several windows for the
current file, the file is written if it was modified
and the window is closed).
Finally, this is significant:
Vim remembers whether you have changed the buffer. You are protected from
losing the changes you made. If you try to quit without writing, or want to
start editing another file, Vim will refuse this. In order to overrule this
protection, add a '!' to the command. The changes will then be lost. For
example: ":q" will not work if the buffer was changed, but ":q!" will. To see
whether the buffer was changed use the "CTRL-G" command. The message includes
the string "[Modified]" if the buffer has been changed.
If you want to automatically save the changes without asking, switch on the
'autowriteall' option. 'autowrite' is the associated Vi-compatible option
that does not work for all commands.
"What is your most productive shortcut with Vim?" will probably be useful too.