mirrored_repositories/future_of_workout/packages/local_storage_workout_api/lib/src
import 'package:hive_flutter/hive_flutter.dart';
import 'package:workout_api/workout_api.dart';

/// Custom [TypeAdapter] for writing and reading [WorkoutExercise]
/// from local database.
class WorkoutExerciseAdapter extends TypeAdapter<WorkoutExercise> {
  @override
  final int typeId = 13;

  @override
  WorkoutExercise read(BinaryReader reader) {
    final numOfFields = reader.readByte();

    final fields = <int, dynamic>{};

    for (var i = 0; i < numOfFields; i++) {
      final field = {reader.readByte(): reader.read()};
      fields.addAll(field);
    }

    return WorkoutExercise(
      id: fields[1] as String,
      index: fields[2] as int,
      exercise: fields[3] as Exercise,
      exerciseSeries: (fields[4] as List).cast<ExerciseSeries>(),
    );
  }

  @override
  void write(BinaryWriter writer, WorkoutExercise obj) {
    writer
      ..writeByte(4)
      ..writeByte(1)
      ..write(obj.id)
      ..writeByte(2)
      ..write(obj.index)
      ..writeByte(3)
      ..write(obj.exercise)
      ..writeByte(4)
      ..write(obj.exerciseSeries);
  }
}
