Claimed by Argentina but annexed by Brazil in 1821 , Uruguay declared its independence four years later and secured its freedom in 1828 after a three-year struggle .