Skip to content

Commit

Permalink
seperated into modules and made the strategies generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Karachnor committed Dec 1, 2023
1 parent fbcfb91 commit 6158abb
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 46 deletions.
20 changes: 13 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@ import 'package:weekplanner/blocs/auth_bloc.dart';
import 'package:weekplanner/bootstrap.dart';
import 'package:weekplanner/di.dart';
import 'package:weekplanner/providers/environment_provider.dart' as environment;
import 'package:weekplanner/roles/citizen_strategy.dart';
import 'package:weekplanner/roles/guardian_strategy.dart';
import 'package:weekplanner/roles/roles.dart';
import 'package:weekplanner/roles/superuser_strategy.dart';
import 'package:weekplanner/roles/trustee_strategy.dart';
import 'package:weekplanner/routes.dart';
import 'package:weekplanner/screens/choose_citizen_screen.dart';
import 'package:weekplanner/screens/login_screen.dart';
import 'package:weekplanner/widgets/giraf_notify_dialog.dart';
import 'package:weekplanner/roles/roles.dart';


final Api _api = di.get<Api>();

void main() {
// Create different role strategies
/// Create different role strategies
var citizenStrategy = CitizenStrategy();
var trusteeStrategy = TrusteeStrategy();
var guardianStrategy = GuardianStrategy();
var superuserStrategy = SuperuserStrategy();

// Create user contexts with different roles
var citizen = UserContext(citizenStrategy);
var trustee = UserContext(trusteeStrategy);
var guardian = UserContext(guardianStrategy);
var superuser = UserContext(superuserStrategy);
///Create context for each strategies
var user = UserContext<CitizenStrategy>(citizenStrategy);
var trustee = UserContext<TrusteeStrategy>(trusteeStrategy);
var guardian = UserContext<GuardianStrategy>(guardianStrategy);
var superuser = UserContext<SuperuserStrategy>(superuserStrategy);

// Register all dependencies for injector
Bootstrap().register();
WidgetsFlutterBinding.ensureInitialized();
Expand Down
10 changes: 10 additions & 0 deletions lib/roles/citizen_strategy.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:weekplanner/roles/roles.dart';

/// Concrete strategy for Citizen role
class CitizenStrategy implements RoleStrategy<CitizenStrategy> {
@override
void performRoleAction() {
// Implement actions specific to Citizen role
print("Performing actions as a Citizen");
}
}
10 changes: 10 additions & 0 deletions lib/roles/guardian_strategy.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:weekplanner/roles/roles.dart';

/// Concrete strategy for Guardian role
class GuardianStrategy implements RoleStrategy<GuardianStrategy> {
@override
void performRoleAction() {
// Implement actions specific to Guardian role
print("Performing actions as a Guardian");
}
}
Empty file removed lib/roles/roles
Empty file.
57 changes: 18 additions & 39 deletions lib/roles/roles.dart
Original file line number Diff line number Diff line change
@@ -1,54 +1,33 @@
// Define a strategy interface
abstract class RoleStrategy {
/// Define a strategy interface
abstract class RoleStrategy<T> {
void performRoleAction();
}

// Concrete strategy for Citizen role
class CitizenStrategy implements RoleStrategy {
@override
void performRoleAction() {
// Implement actions specific to Citizen role
print("Performing actions as a Citizen");
}
}

// Concrete strategy for Trustee role
class TrusteeStrategy implements RoleStrategy {
@override
void performRoleAction() {
// Implement actions specific to Trustee role
print("Performing actions as a Trustee");
}
}

// Concrete strategy for Guardian role
class GuardianStrategy implements RoleStrategy {
@override
void performRoleAction() {
// Implement actions specific to Guardian role
print("Performing actions as a Guardian");
}
}

// Concrete strategy for Superuser role
class SuperuserStrategy implements RoleStrategy {
@override
void performRoleAction() {
// Implement actions specific to Superuser role
print("Performing actions as a Superuser");
}
}
// Context class that uses the strategy
class UserContext {
RoleStrategy _roleStrategy;





/// Context class that uses the strategy
class UserContext<T extends RoleStrategy<T>> {
RoleStrategy<T> _roleStrategy;

UserContext(this._roleStrategy);

void setRoleStrategy(RoleStrategy strategy) {
void setRoleStrategy(RoleStrategy<T> strategy) {
_roleStrategy = strategy;
}

void executeRoleAction() {
_roleStrategy.performRoleAction();
}
}
}






10 changes: 10 additions & 0 deletions lib/roles/superuser_strategy.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:weekplanner/roles/roles.dart';

/// Concrete strategy for Superuser role
class SuperuserStrategy implements RoleStrategy<SuperuserStrategy> {
@override
void performRoleAction() {
// Implement actions specific to Superuser role
print("Performing actions as a Superuser");
}
}
11 changes: 11 additions & 0 deletions lib/roles/trustee_strategy.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:weekplanner/roles/roles.dart';


/// Concrete strategy for Trustee role
class TrusteeStrategy implements RoleStrategy<TrusteeStrategy> {
@override
void performRoleAction() {
// Implement actions specific to Trustee role
print("Performing actions as a Trustee");
}
}

0 comments on commit 6158abb

Please sign in to comment.