Fakultas Ilmu Komputer UI

Commit e925c9a9 authored by Tsamara Esperanti Erwin's avatar Tsamara Esperanti Erwin 🦄
Browse files

Merge branch 'coldfix-pbi7' into 'staging'

Coldfix pbi7

See merge request ppl-fasilkom-ui/itproject-2020/group-b/pilar-mobile!95
parents 72cfda3f 2ef18212
......@@ -3,9 +3,11 @@ import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:home_industry/Pages/Program/model/program.dart';
import 'package:home_industry/Pages/Program/repositories/program_repository.dart';
import 'package:stream_transform/stream_transform.dart';
import '../program_page.dart';
import './bloc.dart';
class ProgramBloc extends Bloc<ProgramEvent, ProgramState> {
......@@ -63,16 +65,16 @@ class ProgramBloc extends Bloc<ProgramEvent, ProgramState> {
final programs =
await programRepository.fetchProgram(page: currentState.page + 1);
if (currentState is ListProgramsLoaded || currentState is InitialListProgramState) {
programs.results.sort((a,b) => b.endDateTime.compareTo(a.endDateTime));
final sort = sortPrograms(programs.results);
yield ListProgramsSortedLoaded(
programs: programs.results,
programs: sort,
hasReachedMax: programs.next == null,
page: currentState.page + 1);
return;
} else if (currentState is ListProgramsSortedLoaded) {
programs.results.sort((a,b) => b.endDateTime.compareTo(a.endDateTime));
final sort = sortPrograms(programs.results);
yield ListProgramsSortedLoaded(
programs: currentState.programs + programs.results,
programs: currentState.programs + sort,
hasReachedMax: programs.next == null,
page: currentState.page + 1);
return;
......@@ -97,3 +99,24 @@ class ProgramBloc extends Bloc<ProgramEvent, ProgramState> {
);
}
}
List<Program> sortPrograms(List programs) {
final programFinished = [];
final programInProgress = [];
final programNotStarted = [];
for (var i = 0; i < programs.length; i++) {
final program = programs.elementAt(i);
if (program.endDateTime != null &&
program.endDateTime.
compareTo(CustomizableDateTime.current) < 1) {
programFinished.add(program);
} else if (program.startDateTime != null &&
CustomizableDateTime.current.
compareTo(program.startDateTime) < 1) {
programNotStarted.add(program);
} else {
programInProgress.add(program);
}
}
return [...programNotStarted, ...programInProgress, ...programFinished];
}
......@@ -381,12 +381,7 @@ class _ProgramDescription extends StatelessWidget {
fontSize: 16,
),
),
if ( program.endDateTime != null &&
program.endDateTime.compareTo(CustomizableDateTime.current) < 1)
const Text(
'Selesai',
style: TextStyle(fontWeight: FontWeight.bold),
)
statusText()
]),
),
Expanded(
......@@ -398,6 +393,28 @@ class _ProgramDescription extends StatelessWidget {
],
));
}
Widget statusText() {
if (program.startDateTime != null &&
CustomizableDateTime.current.compareTo(program.startDateTime) < 1) {
return const Text(
'Belum Mulai',
style: TextStyle(fontWeight: FontWeight.bold),
);
}
else if ( program.endDateTime != null &&
program.endDateTime.compareTo(CustomizableDateTime.current) < 1) {
return const Text(
'Selesai',
style: TextStyle(fontWeight: FontWeight.bold),
);
} else {
return const Text(
'Dalam Progress',
style: TextStyle(fontWeight: FontWeight.bold),
);
}
}
}
class _DonasiButton extends StatelessWidget {
......@@ -429,7 +446,7 @@ class _DonasiButton extends StatelessWidget {
class FilterButton extends StatelessWidget {
final String sort;
FilterButton({Key key, @required this.sort})
const FilterButton({Key key, @required this.sort})
: assert(sort != null),
super(key: key);
......
......@@ -32,6 +32,7 @@ void main() {
Program programFinished;
Program programNotFinished;
Program program;
Program programNotStarted;
setUpAll(() async {
Intl.defaultLocale = 'id';
......@@ -39,7 +40,7 @@ void main() {
});
setUp(() {
program = programFinished = Program(
program = Program(
id: 'yha',
name: 'Rumah Belajar',
description: 'Belajar Komputer',
......@@ -78,10 +79,23 @@ void main() {
totalDonationAmount: '0',
speaker: 'Sherlock Holmes',
posterImage: null);
programNotStarted = Program(
id: 'yha',
name: 'Rumah Belajar',
description: 'Belajar Komputer',
startDateTime: DateTime.parse('2021-07-20 20:18:04'),
endDateTime: DateTime.parse('2021-10-20 20:18:04'),
location: 'Rumah saya',
code: '123',
openDonation: true,
programMinutes: null,
totalDonationAmount: '0',
speaker: 'Sherlock Holmes',
posterImage: null);
listProgram =
ListProgram(count: 1, next: null, previous: null, results: [program]);
listProgramWithDate =
ListProgram(count: 1, next: null, previous: null, results: [programNotFinished, programFinished]);
ListProgram(count: 1, next: null, previous: null, results: [programNotFinished, programFinished, programNotStarted]);
dio = DioMock();
dependenciesRepositories = DependenciesRepositories(dio: dio);
widgetTested = MaterialApp(
......@@ -90,6 +104,11 @@ void main() {
value: dependenciesRepositories, child: Programs()));
});
test('sort program function', () {
var sortedProgram = sortPrograms(listProgramWithDate.results);
expect(sortedProgram.elementAt(0), listProgramWithDate.results.elementAt(2));
});
testWidgets('Load one program', (WidgetTester tester) async {
when(dio.get(urlProgram, queryParameters: queryParamProgram)).thenAnswer(
(_) async => Response<Map<dynamic, dynamic>>(
......@@ -114,8 +133,12 @@ void main() {
expect(find.text('Status'), findsOneWidget);
expect(find.text('Tanggal'), findsOneWidget);
await tester.tap(find.text('Status'));
await tester.pump(Duration(seconds: 5));
expect(find.byType(CustomCircularIndicator), findsOneWidget);
await tester.pump(Duration(seconds: 5));
expect(find.text('Selesai'), findsOneWidget);
expect(find.text('Belum Mulai'), findsOneWidget);
expect(find.text('Dalam Progress'), findsOneWidget);
});
testWidgets('Load programs by date', (WidgetTester tester) async {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment