Skip to content

Commit

Permalink
optimized and v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bipinkrish committed Sep 17, 2023
1 parent 9df2f9e commit d034ef6
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 107 deletions.
6 changes: 3 additions & 3 deletions lib/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
final ScrollController _scrollController = ScrollController();
final TextEditingController _messageController = TextEditingController();
final FocusNode _messageNode = FocusNode();
Color meColor = Colors.blue;
Color youColor = Colors.green;
bool markdown = true;
Color meColor = defaultmeColor;
Color youColor = defaultyouColor;
bool markdown = defaultMarkdown;
final notifier = providerContainer.read(chatMessagesProvider.notifier);
bool isComputer = Platform.isWindows || Platform.isLinux || Platform.isMacOS;
bool isMobile = Platform.isAndroid || Platform.isIOS;
Expand Down
29 changes: 27 additions & 2 deletions lib/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ const String youColorKey = 'YouColor';
const String meColorKey = 'MeColor';
const String markdownKey = 'MarkDown';

final String defaultMeColor = Colors.blue.hashCode.toString();
final String defaultYouColor = Colors.green.hashCode.toString();
const Color defaultmeColor = Colors.blue;
const Color defaultyouColor = Colors.green;
final String defaultMeColor = defaultmeColor.hashCode.toString();
final String defaultYouColor = defaultyouColor.hashCode.toString();
const bool defaultMarkdown = false;

///////////////////////////////////////////////////////////// Custom Classes
Expand Down Expand Up @@ -158,6 +160,29 @@ SnackBar snackbar(String content) {
);
}

// check valid ip
bool isValidIPAddress(String input) {
final RegExp ipv4RegExp = RegExp(
r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$',
caseSensitive: false,
);

final RegExpMatch? match = ipv4RegExp.firstMatch(input);

if (match == null) {
return false;
}

for (int i = 1; i <= 4; i++) {
final int octet = int.parse(match[i]!);
if (octet < 0 || octet > 255) {
return false;
}
}

return true;
}

/////////////////////////////////////////////////// Shared Prefrences
void save(String key, String value) async {
Expand Down
177 changes: 79 additions & 98 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class _HomePageState extends State<HomePage> {
),
IconButton(
onPressed: () async {
isAvailable = false;
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
Expand All @@ -236,6 +237,7 @@ class _HomePageState extends State<HomePage> {
),
);
if (!mounted) return;
isAvailable = true;
initiateLocalName();
},
icon: const Icon(Icons.settings_outlined),
Expand Down Expand Up @@ -369,7 +371,6 @@ class _HomePageState extends State<HomePage> {
client.write("REJECTED");
client.close();
Navigator.of(context).pop();

isAvailable = true;
},
),
Expand All @@ -378,7 +379,6 @@ class _HomePageState extends State<HomePage> {
onPressed: () {
client.write("ACCEPTED");
client.close();

Navigator.of(context).pop();
acceptCallback(asking);
},
Expand All @@ -401,82 +401,84 @@ class _HomePageState extends State<HomePage> {
isDismissible: false,
context: context,
builder: (context) {
return Container(
padding: const EdgeInsets.all(10),
height: 150,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: !isRequesting
? [
Text(
"Do you want to start a chat with ${receiver.deviceName}?"),
const SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
child: const Text("Cancel"),
onPressed: () {
isRequesting = false;
isAvailable = true;

Navigator.of(context).pop();
},
),
ElevatedButton(
child: const Text("Ask"),
onPressed: () {
isRequesting = true;
isAvailable = false;
peer = receiver;

Navigator.of(context).pop();
showChatRequestPopup(receiver);
askAccept(receiver.ip, port, localName, setAccAns);
},
),
],
)
]
: [
Text("Waiting for ${receiver.deviceName} to accept"),
const SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
child: const Text("Cancel"),
onPressed: () {
sendCancel(receiver.ip, port);

isRequesting = false;
isAvailable = true;

Navigator.of(context).pop();
},
),
const Center(
child: Padding(
padding: EdgeInsets.all(12),
child: CircularProgressIndicator(),
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Container(
padding: const EdgeInsets.all(10),
height: 150,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: !isRequesting
? [
Text(
"Do you want to start a chat with ${receiver.deviceName}?"),
const SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
child: const Text("Cancel"),
onPressed: () {
isRequesting = false;
isAvailable = true;
Navigator.of(context).pop();
},
),
),
],
),
],
),
);
ElevatedButton(
child: const Text("Ask"),
onPressed: () {
isRequesting = true;
isAvailable = false;
peer = receiver;
askAccept(
receiver.ip, port, localName, setAccAns);
if (mounted) {
setState(() {});
}
},
),
],
)
]
: [
Text("Waiting for ${receiver.deviceName} to accept"),
const SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
child: const Text("Cancel"),
onPressed: () {
sendCancel(receiver.ip, port);
isRequesting = false;
isAvailable = true;
Navigator.of(context).pop();
},
),
const Center(
child: Padding(
padding: EdgeInsets.all(12),
child: CircularProgressIndicator(),
),
),
],
),
],
),
);
});
},
);
}

// Function to show the IP address entry dialog
void showIpAddressDialog() {
isAvailable = false;
List<String> ipAddress = localIP.split('.');
final TextEditingController ip3 = TextEditingController();
final TextEditingController ip4 = TextEditingController();
Expand All @@ -499,17 +501,17 @@ class _HomePageState extends State<HomePage> {
context: context,
builder: (BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 50, vertical: 25),
padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 25),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
children: [
Text(ipAddress[0], style: textstyle),
const Text(".", style: textstyle),
const Text(" . ", style: textstyle),
Text(ipAddress[1], style: textstyle),
const Text(".", style: textstyle),
const Text(" . ", style: textstyle),
Expanded(
child: TextField(
textAlign: TextAlign.center,
Expand All @@ -528,7 +530,7 @@ class _HomePageState extends State<HomePage> {
},
),
),
const Text(".", style: textstyle),
const Text(" . ", style: textstyle),
Expanded(
child: TextField(
textAlign: TextAlign.center,
Expand All @@ -555,12 +557,14 @@ class _HomePageState extends State<HomePage> {
child: const Text("Cancel"),
onPressed: () {
Navigator.of(context).pop();
isAvailable = true;
},
),
ElevatedButton(
child: const Text("Discover"),
onPressed: () {
Navigator.of(context).pop();
isAvailable = true;
String enteredIpAddress =
"${ipAddress[0]}.${ipAddress[1]}.${ip3.text}.${ip4.text}";
if (isValidIPAddress(enteredIpAddress)) {
Expand All @@ -578,27 +582,4 @@ class _HomePageState extends State<HomePage> {
},
);
}

// check valid ip
bool isValidIPAddress(String input) {
final RegExp ipv4RegExp = RegExp(
r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$',
caseSensitive: false,
);

final RegExpMatch? match = ipv4RegExp.firstMatch(input);

if (match == null) {
return false;
}

for (int i = 1; i <= 4; i++) {
final int octet = int.parse(match[i]!);
if (octet < 0 || octet > 255) {
return false;
}
}

return true;
}
}
8 changes: 4 additions & 4 deletions lib/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class _SettingsState extends State<Settings> {
TextEditingController(text: "4321");

// colors
Color meColor = Colors.blue;
Color youColor = Colors.green;
Color meColor = defaultmeColor;
Color youColor = defaultyouColor;
late Color tempMe;
late Color tempYou;

// markdown
bool markdown = true;
bool markdown = defaultMarkdown;

// initial device name
void initailizeDevicename() {
Expand Down Expand Up @@ -295,7 +295,7 @@ class _SettingsState extends State<Settings> {
youColor = value;
}
},
colorPickerWidth: 250,
colorPickerWidth: 200,
labelTypes: const [ColorLabelType.rgb],
),
Row(
Expand Down

0 comments on commit d034ef6

Please sign in to comment.