Skip to content

Commit 4c4da95

Browse files
committed
leaderboard update up
1 parent 98f06e5 commit 4c4da95

File tree

4 files changed

+95
-63
lines changed

4 files changed

+95
-63
lines changed

lib/screens/games/select_the_area/select_the_area_screen.dart

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
4141

4242
User myuser = UserManager.instance.user;
4343

44-
// color_map = {
45-
// 0: (0, 0, 0), # Unknown
46-
// 1: (0, 0, 255), # Carcinoma
47-
// 2: (255, 0, 0), # Necrosis
48-
// 3: (0, 255, 0), # Tumor Stroma
49-
// 4: (0, 255, 255), # Others
50-
// }
44+
late int indexTissueToFind;
45+
Map<int, String> tissueTypes = <int, String>{
46+
1: 'Carcinoma',
47+
2: 'Necrosis',
48+
3: 'Tumor Stroma',
49+
4: 'Others',
50+
};
5151

5252
@override
5353
void initState() {
5454
super.initState();
55-
5655
_loadImages();
56+
checkImages();
5757
}
5858

5959
void _getPixelsTypeCount() {
@@ -75,49 +75,67 @@ class _CircleImageComparisonScreenState extends State<SelectTheAreaGame> {
7575
}
7676
}
7777

78+
void _getTissueToFind() {
79+
// 1: (0, 0, 255), # Carcinoma
80+
// 2: (255, 0, 0), # Necrosis
81+
// 3: (0, 255, 0), # Tumor Stroma
82+
// 4: (0, 255, 255), # Others
83+
84+
// Select randomly one of the tissues pixelCount exept 0
85+
final List<int> keysTissueTypes = pixelCount.keys.toList()..remove(0);
86+
final int randomIndex = Random().nextInt(tissueTypes.length - 1);
87+
indexTissueToFind = keysTissueTypes[randomIndex];
88+
89+
if (kDebugMode) {
90+
final String tissueName = tissueTypes[indexTissueToFind]!;
91+
print('Tissue Index to Find: $indexTissueToFind');
92+
print('Tissue to Find: $tissueName');
93+
}
94+
}
95+
7896
Future<void> _loadImages() async {
7997
final http.Response response = await http.get(
8098
Uri.parse('https://microcosm-backend.gmichele.com/get/low/random/'));
8199

82100
final Map<String, dynamic> jsonImageResponse =
83101
jsonDecode(response.body) as Map<String, dynamic>;
84102

85-
setState(() {
86-
imageBytes = base64Decode(jsonImageResponse['rows']![0][1] as String);
87-
maskImageBytes = base64Decode(jsonImageResponse['rows']![0][2] as String);
88-
cmappedMaskImageBytes =
89-
base64Decode(jsonImageResponse['rows']![0][3] as String);
90-
91-
fullImage = img.decodeImage(imageBytes);
92-
maskImage = img.decodeImage(maskImageBytes);
93-
cmappedMaskImage = img.decodeImage(cmappedMaskImageBytes);
94-
95-
displayedFullImage = Image.memory(
96-
imageBytes,
97-
fit: BoxFit.cover,
98-
width: 600,
99-
height: 600,
100-
);
101-
102-
displayedCmappedMaskImage = Image.memory(
103-
cmappedMaskImageBytes,
104-
fit: BoxFit.cover,
105-
width: 600,
106-
height: 600,
107-
);
108-
109-
// print all lenghts
110-
// if (kDebugMode) {
111-
// print('imageBytes: ${imageBytes.length}');
112-
// print('maskImageBytes: ${maskImageBytes.length}');
113-
// print('cmappedMaskImageBytes: ${cmappedMaskImageBytes.length}');
114-
// print('fullImage: ${fullImage.data.length}');
115-
// print('maskImage: ${maskImage.data.length}');
116-
// print('cmappedMaskImage: ${cmappedMaskImage.length}');
117-
// }
103+
imageBytes = base64Decode(jsonImageResponse['rows']![0][1] as String);
104+
maskImageBytes = base64Decode(jsonImageResponse['rows']![0][2] as String);
105+
cmappedMaskImageBytes =
106+
base64Decode(jsonImageResponse['rows']![0][3] as String);
118107

119-
_getPixelsTypeCount();
120-
});
108+
fullImage = img.decodeImage(imageBytes);
109+
maskImage = img.decodeImage(maskImageBytes);
110+
cmappedMaskImage = img.decodeImage(cmappedMaskImageBytes);
111+
112+
displayedFullImage = Image.memory(
113+
imageBytes,
114+
fit: BoxFit.cover,
115+
width: 600,
116+
height: 600,
117+
);
118+
119+
displayedCmappedMaskImage = Image.memory(
120+
cmappedMaskImageBytes,
121+
fit: BoxFit.cover,
122+
width: 600,
123+
height: 600,
124+
);
125+
}
126+
127+
void checkImages() {
128+
// If there is only one type of tissue, reload the images
129+
_getPixelsTypeCount();
130+
131+
if (pixelCount.length == 1) {
132+
_loadImages();
133+
}
134+
135+
if (fullImage == null || maskImage == null || cmappedMaskImage == null) {
136+
_loadImages();
137+
}
138+
_getTissueToFind();
121139
}
122140

123141
void _onPanStart(DragStartDetails details) {

lib/screens/main/components/leaderboard.dart

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class Leaderboard extends StatefulWidget {
1212
});
1313

1414
@override
15-
State<Leaderboard> createState() => _LeaderboardState();
15+
State<Leaderboard> createState() => LeaderboardState();
1616
}
1717

18-
class _LeaderboardState extends State<Leaderboard> {
18+
class LeaderboardState extends State<Leaderboard> {
1919
late List<AccountInfoCard> accountInfoCards;
2020

2121
@override
@@ -24,8 +24,9 @@ class _LeaderboardState extends State<Leaderboard> {
2424
accountInfoCards = refreshLeaderboard();
2525
}
2626

27-
void _updateLeaderboard() {
27+
void updateLeaderboard() {
2828
setState(() {
29+
print('update leaderboard state');
2930
accountInfoCards = refreshLeaderboard();
3031
});
3132
}
@@ -57,7 +58,7 @@ class _LeaderboardState extends State<Leaderboard> {
5758
ElevatedButton(
5859
onPressed: () {
5960
user.addScore(10);
60-
_updateLeaderboard();
61+
updateLeaderboard();
6162
},
6263
child: const Text('Debug addScore(10)'),
6364
),
@@ -77,11 +78,16 @@ class _LeaderboardState extends State<Leaderboard> {
7778
// <String, String>{'name': 'Phyllis Montes', 'score': '20'},
7879
];
7980

80-
accounts.add(<String, String>{'name': user.name, 'score': user.score.toString()});
81+
accounts.add(
82+
<String, String>{'name': user.name, 'score': user.score.toString()});
8183

82-
accounts.sort((Map<String, String> a, Map<String, String> b) => int.parse(b['score']!).compareTo(int.parse(a['score']!)));
84+
accounts.sort((Map<String, String> a, Map<String, String> b) =>
85+
int.parse(b['score']!).compareTo(int.parse(a['score']!)));
8386

84-
final List<AccountInfoCard> accountInfoCards = accounts.asMap().entries.map((MapEntry<int, Map<String, String>> entry) {
87+
final List<AccountInfoCard> accountInfoCards = accounts
88+
.asMap()
89+
.entries
90+
.map((MapEntry<int, Map<String, String>> entry) {
8591
final int index = entry.key;
8692
final Map<String, String> account = entry.value;
8793
final String rank = (index + 1).toString();
@@ -133,4 +139,4 @@ class AccountInfoCard extends StatelessWidget {
133139
),
134140
);
135141
}
136-
}
142+
}

lib/screens/main/main_screen.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@ class MainScreen extends StatefulWidget {
2424

2525
class _MainScreenState extends State<MainScreen> {
2626
int _selectedPage = 0;
27+
final GlobalKey<LeaderboardState> _childKey = GlobalKey<LeaderboardState>();
2728

2829
void upNavBarId(int index) {
2930
setState(() {
3031
_selectedPage = index;
3132
});
3233
}
3334

35+
void updateLeaderboardState() {
36+
setState(() {
37+
_childKey.currentState!.updateLeaderboard();
38+
});
39+
}
40+
3441
@override
3542
Widget build(BuildContext context) {
3643
Widget page;
@@ -39,7 +46,7 @@ class _MainScreenState extends State<MainScreen> {
3946
page = RoadmapScreen(onNavButtonPressed: upNavBarId);
4047
break;
4148
case 1:
42-
page = ProfileScreen();
49+
page = ProfileScreen(onTestButtonPressed: updateLeaderboardState);
4350
break;
4451
case 2:
4552
page = const SettingsScreen();
@@ -80,14 +87,12 @@ class _MainScreenState extends State<MainScreen> {
8087
flex: 4,
8188
child: page,
8289
),
83-
const Expanded(
90+
Expanded(
8491
child: Column(
8592
children: <Widget>[
86-
SizedBox(height: 2 * defaultPadding),
87-
Leaderboard(),
88-
SizedBox(height: defaultPadding),
89-
//TO BE CHANGED WITH ChatApp
90-
Flexible(child: Leaderboard()),
93+
const SizedBox(height: 2 * defaultPadding),
94+
Leaderboard(key: _childKey),
95+
const SizedBox(height: defaultPadding),
9196
],
9297
),
9398
),

lib/screens/profile/profile_screen.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import '../../utils.dart';
77
import '../main/components/progress_levels.dart' as pl;
88

99
class ProfileScreen extends StatefulWidget {
10-
ProfileScreen({super.key});
10+
Function() onTestButtonPressed;
11+
12+
ProfileScreen({super.key, required this.onTestButtonPressed});
13+
1114
final UserManager userManager = UserManager.instance;
12-
15+
1316
@override
1417
_ProfileScreenState createState() => _ProfileScreenState();
1518
}
@@ -70,8 +73,8 @@ class _ProfileScreenState extends State<ProfileScreen> {
7073
),
7174
const SizedBox(height: 30),
7275
ElevatedButton(
73-
onPressed: () => _addScore(10),
74-
76+
onPressed: () =>
77+
<void>{_addScore(10), widget.onTestButtonPressed()},
7578
child: const Text('Debug addScore(10)'),
7679
),
7780
],
@@ -196,4 +199,4 @@ class Header extends StatelessWidget {
196199
],
197200
);
198201
}
199-
}
202+
}

0 commit comments

Comments
 (0)