Prove that it is a random variable iff it is constant on each partition

Let $\mathcal{G} = \{A_1, \ldots, A_n\}$ be a partition of a set $\Omega$, $\mathcal{F} = \sigma(\mathcal{G})$. Prove that $X : \Omega\to\mathbb{R}$ is a random variable if and only if it is constant on each of the partition elements $A_j$.

I have attempted both directions but faced some difficulties to continue writing the proof. I have also consulted some sources that have a not so clear proof.

Here is the proof:
We know that $$\sigma(\mathcal{G}) = \left\{\sum_{i\in I}A_i : I \subset \{1,\ldots,n\}\right\}.$$
($\Leftarrow$)
Let $B\in \mathcal{B}(\mathbb{R})$. In this case $X(\omega) = a_i$, $\forall\omega\in A_i$ for some constants $a_i$, (then I am not sure how to proceed to show that it is a random variable), the proof says that $X = \sum_{i\in I}^n a_i \mathcal{{1_A}_i}$ and
$\{X \in B\} = \sum_{i : a_i\in \mathcal{B}}A_i\in \mathcal{F}$, so $X$ is a random variable.

My question is why are we concluding or defining $X = \sum_{i\in I}^n a_i \mathcal{{1_A}_i}$ (what is the purpose), in other words I don't understand it's flow of reasoning.

($\Rightarrow$)
We use proof by contradiction: If $X$ is not constant on $A_i$, then $\exists i_0$ such that for some $\omega_1, \omega_2 \in A_{i_0}$ one has $a:= X(\omega_1) \not= X(\omega_2)$. Then I am not sure how to proceed from here.

Let me just use $\bigcup$ instead of $\sum$ to denote unions, i.e. $$\sigma(\mathcal{G})=\left\{\bigcup_{i\in I} A_i\mid I\subseteq \{1,\ldots,n\}\right\}.\tag{1}$$ Now, a function $X:\Omega\to \mathbb{R}$ is a random variable (i.e. it is measurable) if $$X^{-1}(B)\in \sigma(\mathcal{G}),\quad B\in\mathcal{B}(\mathbb{R}).$$
$\Longrightarrow:$ You already argued that there exists constants $a_1,\ldots,a_n\in\mathbb{R}$ such that $X(\omega)=a_i$ for all $\omega\in A_i$. But this is exactly saying that $X$ is of the form $$X(\omega)=\sum_{i=1}^n a_i1_{A_i}(\omega),\quad \omega\in\Omega.$$ as the hint suggests. If this is not obvious, just note that for any $\omega\in\Omega$, there is only one term in the sum on the right-hand that is non-zero. And remember that since $\mathcal{G}$ is a partition any $\omega$ must lie within exactly one of the $A_i$'s.
If $B\in\mathcal{B}(\mathbb{R})$ is any Borel set, then $$\{X\in B\}=X^{-1}(B)=\{\omega\in\Omega\mid X(\omega)\in B\}=\bigcup_{i=1}^n\{\omega\in A_i\mid X(\omega)\in B\}.$$ But $$\{\omega\in A_i\mid X(\omega)\in B\}= \begin{cases} A_i,\quad&\text{if }a_i\in B,\\ \varnothing,\quad &\text{if }a_i\notin B, \end{cases}$$ and hence $$\{X\in B\}=\bigcup_{i: a_i\in B} A_i\in\sigma(\mathcal{G}).$$
$\Longleftarrow:$ So you have assumed the existence of an $i_0$ and $\omega_1,\omega_2\in A_{i_0}$ such that $X(\omega_1)\neq X(\omega_2)$ and seek a contradiction. Let us denote $c:=X(\omega_1)$, then $\{c\}$ is a closed set and hence $\{c\}\in\mathcal{B}(\mathbb{R})$. Since $X$ is assumed to be a random variable we must have that $$\{X=c\}=X^{-1}(\{c\})\in\sigma(\mathcal{G}).$$ But this is a contradiction to $(1)$ because $\{X=c\}$ is a proper subset of $A_{i_0}$, i.e. $$\{X=c\}\subset A_{i_0}\quad\text{and}\quad \{X=c\}\neq A_{i_0}$$ showing that $\{X=c\}$ cannot be of the form $\bigcup_{i\in I}A_i$ as suggested.